public function getMap() { $entity = clone $this->entity; $annotations = new Annotations($this->reflectionClass); foreach ($this->data as $attribute => $value) { $type = $annotations->getAttributeType($attribute); $value = $this->getFieldValue($value, $type); $this->setEntityValue($entity, $attribute, $value); } return $entity; }
/** * @return mixed */ public function getMap() { $reflectionClass = new \ReflectionClass($this->entity); $properties = $reflectionClass->getProperties(); $annotations = new Annotations($reflectionClass); $data = []; foreach ($properties as $property) { $type = $annotations->getAttributeType($property->getName()); $value = $this->getEntityValue($property->getName()); $data[$property->getName()] = $this->mappingFactory->make($type)->toArray($value); } return $data; }
class AnnotationsTest extends \PHPUnit_Framework_TestCase { public function testShouldGetAttributeTypes() { $fakeClass = new class { /** * @var int $attribute1 */ public $attribute1 = 1; /** * @var string $attribute2 */ public $attribute2 = 'test'; /** * @var array $attribute3 */ public $attribute3 = [1, 2, 3]; }; $annotations = new Annotations(new \ReflectionClass($fakeClass)); $this->assertEquals('int', $annotations->getAttributeType('attribute1')); $this->assertEquals('string', $annotations->getAttributeType('attribute2')); $this->assertEquals('array', $annotations->getAttributeType('attribute3')); } public function testShouldThrowAnnotationNotFound() { $this->expectException(AnnotationNotFound::class); $fakeClass = new class { /** * @foo */ public $attribute1 = 1; }; $annotations = new Annotations(new \ReflectionClass($fakeClass)); $annotations->getAttributeType('attribute1'); } }