fivelab / object-mapper
Map array data to objects
Installs: 1 143
Dependents: 0
Suggesters: 1
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=5.4
- fivelab/cache: ~1.0
- fivelab/exception: ~1.0
- fivelab/reflection: ~1.0
Requires (Dev)
- doctrine/annotations: ~1.0
- phpunit/phpunit: 4.*
Suggests
- doctrine/annotations: For read metadata from annotations
This package is not auto-updated.
Last update: 2017-08-16 12:52:02 UTC
README
With this package, you can map array
data to object
instances.
Installation
Add FiveLab/ObjectMapper in your composer.json:
{ "require": { "fivelab/object-mapper": "~1.0" } }
Now tell composer to download the library by running the command:
$ php composer.phar update fivelab/object-mapper
Basic usage
Before use ObjectMapper you must configure instance:
- Create a metadata factory for loads metadata from objects
- Create a strategy manager
use Doctrine\Common\Annotations\AnnotationReader; use FiveLab\Component\ObjectMapper\Strategy\StrategyManager; use FiveLab\Component\ObjectMapper\Metadata\MetadataFactory; use FiveLab\Component\ObjectMapper\Metadata\Loader\AnnotationLoader; use FiveLab\Component\ObjectMapper\ObjectMapper; use FiveLab\Component\ObjectMapper\Metadata\ObjectMetadata; $strategyManager = StrategyManager::createDefault(); $annotationLoader = new AnnotationLoader(new AnnotationReader()); $metadataFactory = new MetadataFactory($annotationLoader); $objectMapper = new ObjectMapper($metadataFactory, $strategyManager); // Or create default object mapper $objectMapper = ObjectMapper::createDefault(); // Used AnnotationLoader for load metadata
Attention: now supported only annotation metadata loader.
After configure and create instance, you can use ObjectMapper
map function.
Example object for maps:
use FiveLab\Component\ObjectMapper\Annotation\Object; use FiveLab\Component\ObjectMapper\Annotation\Property; /** * @Object() */ class MyClass { /** * @Property() */ public $id; /** * @Property() */ public $name; }
And map array data:
$object = new MyClass(); $objectMapper->map($object, [ 'id' => 1, 'name' => 'Foo Bar' ]);
If you want map all properties in object, you can set attribute allProperties for @Object, then this indicate for load all properties from class.
use FiveLab\Component\ObjectMapper\Annotation\Object; /** * @Object(allProperties=true) */ class MyClass { public $id; public $name; }
If key of array not equals to property name of object, you can set attribute fieldName for @Property
use FiveLab\Component\ObjectMapper\Annotation\Object; use FiveLab\Component\ObjectMapper\Annotation\Property; /** * @Object() */ class MyClass { /** * @Property() */ public $id; /** * @Property(fieldName="first_name") */ public $firstName; } $object = new MyClass(); $objectMapper->map($object, [ 'id' => 1, 'first_name' => 'Foo Bar' ]);
Recursive mapping
You can recursive map data to object.
With simple object:
use FiveLab\Component\ObjectMapper\Annotation\Object; use FiveLab\Component\ObjectMapper\Annotation\Property; class MyClass { /** * @DataMapping\Property(class="Tag") */ protected $tag; } /** * @DataMapping\Object(allProperties=true) */ class Tag { protected $name; } $object = new MyClass(); $objectMapper->map($object, [ 'tag' => [ 'name' => 'Foo Bar' ] ]);
With collection:
use FiveLab\Component\ObjectMapper\Annotation\Object; use FiveLab\Component\ObjectMapper\Annotation\Property; class MyClass { /** * @DataMapping\Property(collection=true, class="Tag") */ protected $tag; } /** * @DataMapping\Object(allProperties=true) */ class Tag { protected $name; } $object = new MyClass(); $objectMapper->map($object, [ 'tag' => [ ['name' => 'Foo Bar'], ['name' => 'Bar Foo'] ] ]);
And you can set the collection class if necessary, to attribute collection collection="MyCollectionClass"
All keys as default will be saved.