@@ -12,8 +12,9 @@ It's a bit like the native SOAP parameter mapping PHP's ``SoapClient``
12
12
gives you, but for JSON.
13
13
It does not rely on any schema, only your PHP class definitions.
14
14
15
- Type detection works by parsing ``@var `` docblock annotations of
16
- class properties, as well as type hints in setter methods.
15
+ Type detection works by parsing type declarations and ``@var ``
16
+ docblock annotations of class properties,
17
+ as well as type hints in setter methods.
17
18
18
19
You do not have to modify your model classes by adding JSON specific code;
19
20
it works automatically by parsing already-existing docblocks.
55
56
56
57
Basic usage
57
58
===========
58
- #. Register an autoloader that can load ` PSR-0 `__ compatible classes.
59
+ #. ` Install `__ `` netresearch/jsonmapper `` with composer
59
60
#. Create a ``JsonMapper `` object instance
60
61
#. Call the ``map `` or ``mapArray `` method, depending on your data
61
62
@@ -69,7 +70,6 @@ Map a normal object:
69
70
$contactObject = $mapper->map($jsonContact, new Contact());
70
71
// or as classname
71
72
$contactObject = $mapper->map($jsonContact, Contact::class);
72
- ?>
73
73
74
74
Map an array of objects:
75
75
@@ -81,12 +81,11 @@ Map an array of objects:
81
81
$contactsArray = $mapper->mapArray(
82
82
$jsonContacts, array(), 'Contact'
83
83
);
84
- ?>
85
84
86
- Instead of ``array() `` you may also use ``ArrayObject `` and descending classes
85
+ Instead of ``array() `` you may also use ``ArrayObject `` and derived classes,
87
86
as well as classes implementing ``ArrayAccess ``.
88
87
89
- __ http://www.php-fig.org/psr/psr-0/
88
+ .. __ : #installation
90
89
91
90
92
91
Example
@@ -96,10 +95,10 @@ JSON from an address book web service:
96
95
.. code :: javascript
97
96
98
97
{
99
- ' name' : ' Sheldon Cooper' ,
100
- ' address' : {
101
- ' street' : ' 2311 N. Los Robles Avenue' ,
102
- ' city' : ' Pasadena'
98
+ " name" : " Sheldon Cooper" ,
99
+ " address" : {
100
+ " street" : " 2311 N. Los Robles Avenue" ,
101
+ " city" : " Pasadena"
103
102
}
104
103
}
105
104
@@ -112,16 +111,11 @@ Your local ``Contact`` class:
112
111
{
113
112
/**
114
113
* Full name
115
- * @var string
116
114
*/
117
- public $name;
115
+ public string $name;
118
116
119
- /**
120
- * @var Address
121
- */
122
- public $address;
117
+ public ?Address $address;
123
118
}
124
- ?>
125
119
126
120
Your local ``Address `` class:
127
121
@@ -138,7 +132,6 @@ Your local ``Address`` class:
138
132
//do something with $street and $city
139
133
}
140
134
}
141
- ?>
142
135
143
136
Your application code:
144
137
@@ -151,15 +144,14 @@ Your application code:
151
144
152
145
echo "Geo coordinates for " . $contact->name . ": "
153
146
. var_export($contact->address->getGeoCoords(), true);
154
- ?>
155
147
156
148
157
149
Property type mapping
158
150
=====================
159
151
``JsonMapper `` uses several sources to detect the correct type of
160
- a property:
152
+ a property in the following order :
161
153
162
- #. The setter method (``set `` + ``ucwords($propertyname) ``) is inspected.
154
+ #. Setter method (``set `` + ``ucwords($propertyname) ``)
163
155
164
156
Underscores "``_ ``" and hyphens "``- ``" make the next letter uppercase.
165
157
Property ``foo_bar-baz `` leads to setter method ``setFooBarBaz ``.
@@ -268,6 +260,13 @@ __ http://phpdoc.org/docs/latest/references/phpdoc/types.html
268
260
269
261
Simple type mapping
270
262
-------------------
263
+
264
+ .. note ::
265
+ This feature is disabled by default for security reasons since version 5.
266
+ See `$bStrictObjectTypeChecking `__ for details.
267
+
268
+ .. __ : #prop-bstrictobjecttypechecking
269
+
271
270
When an object shall be created but the JSON contains a simple type
272
271
only (e.g. string, float, boolean), this value is passed to
273
272
the classes' constructor. Example:
@@ -276,10 +275,7 @@ PHP code:
276
275
277
276
.. code :: php
278
277
279
- /**
280
- * @var DateTime
281
- */
282
- public $date;
278
+ public DateTime $date;
283
279
284
280
JSON:
285
281
@@ -430,6 +426,12 @@ from the ``$undefinedPropertyHandler`` which will be used as property name.
430
426
431
427
Missing properties
432
428
------------------
429
+
430
+ .. note ::
431
+ This only works when `$bStrictObjectTypeChecking `__ stays enabled.
432
+
433
+ .. __ : #prop-bstrictobjecttypechecking
434
+
433
435
Properties in your PHP classes can be marked as "required" by
434
436
putting ``@required `` in their docblock:
435
437
@@ -442,7 +444,7 @@ putting ``@required`` in their docblock:
442
444
public $someDatum;
443
445
444
446
When the JSON data do not contain this property, JsonMapper will throw
445
- an exception when ``$bExceptionOnMissingData `` is activated:
447
+ a `` JsonMapper_Exception `` when ``$bExceptionOnMissingData `` is activated:
446
448
447
449
.. code :: php
448
450
@@ -474,6 +476,8 @@ setter methods by setting ``$bIgnoreVisibility`` to true:
474
476
$jm->map(...);
475
477
476
478
479
+ .. _prop-bstrictobjecttypechecking :
480
+
477
481
Simple types instead of objects
478
482
===============================
479
483
When a variable's type is a class and JSON data is a simple type
@@ -549,6 +553,8 @@ You may pass additional arguments to the post-mapping callback:
549
553
$jm->map(...);
550
554
551
555
556
+ .. _installation :
557
+
552
558
============
553
559
Installation
554
560
============
0 commit comments