@@ -12,8 +12,9 @@ It's a bit like the native SOAP parameter mapping PHP's ``SoapClient``
1212gives you, but for JSON.
1313It does not rely on any schema, only your PHP class definitions.
1414
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.
1718
1819You do not have to modify your model classes by adding JSON specific code;
1920it works automatically by parsing already-existing docblocks.
5556
5657Basic usage
5758===========
58- #. Register an autoloader that can load ` PSR-0 `__ compatible classes.
59+ #. ` Install `__ `` netresearch/jsonmapper `` with composer
5960#. Create a ``JsonMapper `` object instance
6061#. Call the ``map `` or ``mapArray `` method, depending on your data
6162
@@ -69,7 +70,6 @@ Map a normal object:
6970 $contactObject = $mapper->map($jsonContact, new Contact());
7071 // or as classname
7172 $contactObject = $mapper->map($jsonContact, Contact::class);
72- ?>
7373
7474 Map an array of objects:
7575
@@ -81,12 +81,11 @@ Map an array of objects:
8181 $contactsArray = $mapper->mapArray(
8282 $jsonContacts, array(), 'Contact'
8383 );
84- ?>
8584
86- Instead of ``array() `` you may also use ``ArrayObject `` and descending classes
85+ Instead of ``array() `` you may also use ``ArrayObject `` and derived classes,
8786as well as classes implementing ``ArrayAccess ``.
8887
89- __ http://www.php-fig.org/psr/psr-0/
88+ .. __ : #installation
9089
9190
9291Example
@@ -96,10 +95,10 @@ JSON from an address book web service:
9695.. code :: javascript
9796
9897 {
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"
103102 }
104103 }
105104
@@ -112,16 +111,11 @@ Your local ``Contact`` class:
112111 {
113112 /**
114113 * Full name
115- * @var string
116114 */
117- public $name;
115+ public string $name;
118116
119- /**
120- * @var Address
121- */
122- public $address;
117+ public ?Address $address;
123118 }
124- ?>
125119
126120 Your local ``Address `` class:
127121
@@ -138,7 +132,6 @@ Your local ``Address`` class:
138132 //do something with $street and $city
139133 }
140134 }
141- ?>
142135
143136 Your application code:
144137
@@ -151,15 +144,14 @@ Your application code:
151144
152145 echo "Geo coordinates for " . $contact->name . ": "
153146 . var_export($contact->address->getGeoCoords(), true);
154- ?>
155147
156148
157149 Property type mapping
158150=====================
159151``JsonMapper `` uses several sources to detect the correct type of
160- a property:
152+ a property in the following order :
161153
162- #. The setter method (``set `` + ``ucwords($propertyname) ``) is inspected.
154+ #. Setter method (``set `` + ``ucwords($propertyname) ``)
163155
164156 Underscores "``_ ``" and hyphens "``- ``" make the next letter uppercase.
165157 Property ``foo_bar-baz `` leads to setter method ``setFooBarBaz ``.
@@ -268,6 +260,13 @@ __ http://phpdoc.org/docs/latest/references/phpdoc/types.html
268260
269261Simple type mapping
270262-------------------
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+
271270When an object shall be created but the JSON contains a simple type
272271only (e.g. string, float, boolean), this value is passed to
273272the classes' constructor. Example:
@@ -276,10 +275,7 @@ PHP code:
276275
277276.. code :: php
278277
279- /**
280- * @var DateTime
281- */
282- public $date;
278+ public DateTime $date;
283279
284280 JSON:
285281
@@ -430,6 +426,12 @@ from the ``$undefinedPropertyHandler`` which will be used as property name.
430426
431427 Missing properties
432428------------------
429+
430+ .. note ::
431+ This only works when `$bStrictObjectTypeChecking `__ stays enabled.
432+
433+ .. __ : #prop-bstrictobjecttypechecking
434+
433435Properties in your PHP classes can be marked as "required" by
434436putting ``@required `` in their docblock:
435437
@@ -442,7 +444,7 @@ putting ``@required`` in their docblock:
442444 public $someDatum;
443445
444446 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:
446448
447449.. code :: php
448450
@@ -474,6 +476,8 @@ setter methods by setting ``$bIgnoreVisibility`` to true:
474476 $jm->map(...);
475477
476478
479+ .. _prop-bstrictobjecttypechecking :
480+
477481Simple types instead of objects
478482===============================
479483When 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:
549553 $jm->map(...);
550554
551555
556+ .. _installation :
557+
552558============
553559Installation
554560============
0 commit comments