Example #1
0
 /** @test */
 public function itMapsPropertiesUsingGettersAndSetters()
 {
     $map = new Map();
     $map->map('foo', 'mappedFoo');
     $from = new Stubs\From('value for foo');
     $to = new Stubs\To();
     $mapper = new Mapper();
     $mapper->process($from, $to, $map);
     $this->assertEquals('value for foo', $to->getMappedFoo());
 }
 /**
  * Process the embedded collections of a map.
  *
  * @param object|array $from
  * @param object|array $to
  * @param MapInterface $map
  *
  * @return self
  */
 public function process($from, &$to, MapInterface $map)
 {
     $mapper = new Mapper($this->propertyAccess);
     foreach ($map->getEmbeddedCollections() as $collection) {
         // New value
         $value = [];
         // Source items
         $source = $this->propertyAccess->getValue($from, $collection->getFrom());
         foreach ($source as $item) {
             $newItem = $collection->getFactory()->factory($item);
             $value[] = $mapper->process($item, $newItem, $collection->getMap());
         }
         $this->propertyAccess->setValue($to, $collection->getTo(), $value);
     }
     return $this;
 }
 /** @test */
 public function itMapsEmbeddedCollections()
 {
     $from = new Stubs\From('foo');
     $from->setChildren([new Stubs\From('foo child 0'), new Stubs\From('foo child 1'), new Stubs\From('foo child 2')]);
     $to = new Stubs\To();
     $map = new Map();
     $map->map('foo', 'mappedFoo')->embedCollection('children', 'mappedChildren', (new Map())->map('foo', 'mappedFoo'), new CallbackFactory(function ($value) {
         return new Stubs\To();
     }, function ($value) {
         return [];
     }));
     $mapper = new Mapper();
     $mapper->process($from, $to, $map);
     $this->assertCount(3, $to->getMappedChildren());
     $count = 0;
     foreach ($to->getMappedChildren() as $child) {
         $this->assertInstanceOf(Stubs\To::class, $child);
         $this->assertEquals(sprintf('foo child %d', $count++), $child->getMappedFoo());
     }
 }