/** * {@inheritDoc} * * @param string $className * @param string $file * @return null|ClassMetadata * @throws RuntimeException */ protected function loadMetadataFromFile($className, $file) { $config = Yaml::parse(file_get_contents($file)); if (!isset($config[$name = $className])) { throw new RuntimeException(sprintf('Expected metadata for class %s to be defined in %s.', $className, $file)); } $config = $config[$name]; $metadata = new ClassMetadata($name); $metadata->addFileResource($file); foreach ($config['properties'] as $propertyName => $propertyOptions) { $pMetadata = new PropertyMetadata($propertyName); if (isset($propertyOptions['expose'])) { $pMetadata->setExpose((bool) $propertyOptions['expose']); } if (isset($propertyOptions['serialized_name'])) { $pMetadata->setSerializedName((string) $propertyOptions['serialized_name']); } if (isset($propertyOptions['since_version'])) { $pMetadata->setSinceVersion((string) $propertyOptions['since_version']); } if (isset($propertyOptions['until_version'])) { $pMetadata->setUntilVersion((string) $propertyOptions['until_version']); } if (isset($propertyOptions['type'])) { $pMetadata->setType((string) $propertyOptions['type']); } if (isset($propertyOptions['groups'])) { $pMetadata->setGroups($propertyOptions['groups']); } $metadata->addPropertyMetadata($pMetadata); } return $metadata; }
public function loadMetadataForClass($className) { $metadata = new ClassMetadata($className); $reflectionClass = new \ReflectionClass($className); $reflectionProperties = $reflectionClass->getProperties(); foreach ($reflectionProperties as $reflectionProperty) { $name = $reflectionProperty->getName(); $pMetadata = new PropertyMetadata($name); $annotations = $this->reader->getPropertyAnnotations($reflectionProperty); foreach ($annotations as $annotation) { if ($annotation instanceof Annotations\Expose) { $pMetadata->setExpose((bool) $annotation->value); } elseif ($annotation instanceof Annotations\Type) { $pMetadata->setType((string) $annotation->value); } elseif ($annotation instanceof Annotations\Groups) { $pMetadata->setGroups($annotation->value); } elseif ($annotation instanceof Annotations\SerializedName) { $pMetadata->setSerializedName((string) $annotation->value); } elseif ($annotation instanceof Annotations\SinceVersion) { $pMetadata->setSinceVersion((string) $annotation->value); } elseif ($annotation instanceof Annotations\UntilVersion) { $pMetadata->setUntilVersion((string) $annotation->value); } } $metadata->addPropertyMetadata($pMetadata); } return $metadata; }
/** * @depends testConstructor */ public function testSettersGetters() { $unitUnderTest = new PropertyMetadata('testProperty'); $this->assertEquals(false, $unitUnderTest->isExpose()); $unitUnderTest->setExpose(true); $this->assertEquals(true, $unitUnderTest->isExpose()); $unitUnderTest->setSerializedName('serialized'); $this->assertEquals('serialized', $unitUnderTest->getSerializedName()); $unitUnderTest->setType('string'); $this->assertEquals('string', $unitUnderTest->getType()); $unitUnderTest->setGroups(array('string', 'test')); $this->assertCount(2, $unitUnderTest->getGroups()); $this->assertEquals(array('string', 'test'), $unitUnderTest->getGroups()); }