/** * {@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; }
/** * @param PropertyMetadata $property * @return PropertyMetadata */ private function makeItemProperty(PropertyMetadata $property) { $itemProperty = new PropertyMetadata($property->getName()); $itemProperty->setExpose(true)->setSerializedName($property->getSerializedName()); if (preg_match('/array<(?<type>[a-zA-Z\\\\]+)>/', $property->getType(), $matches)) { $itemProperty->setType($matches['type']); } return $itemProperty; }
/** * @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()); }
public function testAddClassMetadata() { $metadata = new ClassMetadata('testClass'); sleep(2); $unitUnderTest = new ClassHierarchyMetadata(); $property = new PropertyMetadata('property'); $property->setType('string'); $metadata->addFileResource('/tmp/fileNotExists')->addPropertyMetadata($property); $unitUnderTest->addFileResource('/tmp/fileNotExists2'); $property = new PropertyMetadata('property2'); $property->setType('integer'); $unitUnderTest->addPropertyMetadata($property); $unitUnderTest->addClassMetadata($metadata); $this->assertEquals($metadata->getName(), $unitUnderTest->getName()); $this->assertEquals($metadata->getCreatedAt(), $unitUnderTest->getCreatedAt()); $this->assertCount(2, $unitUnderTest->getFileResources()); $this->assertCount(2, $unitUnderTest->getProperties()); $this->assertEquals(array('/tmp/fileNotExists2', '/tmp/fileNotExists'), $unitUnderTest->getFileResources()); $this->assertArrayHasKey('property', $unitUnderTest->getProperties()); $this->assertArrayHasKey('property2', $unitUnderTest->getProperties()); }
/** * @depends testConstructor * @depends testSettersGetters */ public function testSerializeUnserialize() { $unitUnderTest = new ClassMetadata('testClass'); $property = new PropertyMetadata('property'); $property->setType('string'); $unitUnderTest->addFileResource('/tmp/fileNotExists')->addPropertyMetadata($property); $serializedString = serialize($unitUnderTest); $unserializedObject = unserialize($serializedString); $this->assertInstanceOf('Opensoft\\SimpleSerializer\\Metadata\\ClassMetadata', $unserializedObject); $this->assertEquals('testClass', $unserializedObject->getName()); $this->assertNotNull($unserializedObject->getCreatedAt()); $fileResources = $unserializedObject->getFileResources(); $this->assertCount(1, $fileResources); $this->assertEquals('/tmp/fileNotExists', $fileResources[0]); $properties = $unserializedObject->getProperties(); $this->assertCount(1, $properties); $this->assertArrayHasKey('property', $properties); $this->assertInstanceOf('Opensoft\\SimpleSerializer\\Metadata\\PropertyMetadata', $properties['property']); $this->assertEquals('property', $properties['property']->getName()); $this->assertEquals('string', $properties['property']->getType()); }
/** * @param $value * @param PropertyMetadata $property * @param $direct * @param null|mixed $object * @param bool $inner * @throws \Opensoft\SimpleSerializer\Exception\InvalidArgumentException * @return array|bool|float|int|string|null */ private function handleValue($value, $property, $direct, $object = null, $inner = false) { $type = $property->getType(); if ($value !== null) { if ($type === 'string') { $value = (string) $value; } elseif ($type === 'boolean') { $value = (bool) $value; } elseif ($type === 'integer') { $value = (int) $value; } elseif ($type === 'double') { $value = (double) $value; } elseif ($type === 'DateTime' || $type[0] === 'D' && strpos($type, 'DateTime<') === 0) { if ($direct == self::DIRECTION_SERIALIZE) { $dateTimeFormat = $this->extractDateTimeFormat($type, DateTime::ISO8601); $value = $value->format($dateTimeFormat); } elseif ($direct == self::DIRECTION_UNSERIALIZE) { $originalValue = trim($value); $dateTimeFormat = $this->extractDateTimeFormat($type); // we should not allow empty string as date time argument. //It can lead us to unexpected results //Only 'null' is possible empty value if (!$originalValue) { throw new InvalidArgumentException('DateTime argument should be well formed string'); } try { $value = new DateTime($value); } catch (\Exception $e) { throw new InvalidArgumentException(sprintf('Invalid DateTime argument "%s"', $value), $e->getCode(), $e); } // if format was specified in metadata - format and compare parsed DateTime object with original string if (isset($dateTimeFormat) && $value->format($dateTimeFormat) !== $originalValue) { throw new InvalidArgumentException(sprintf('Invalid DateTime argument "%s"', $originalValue)); } } } elseif ($type === 'array' || $type[0] === 'a' && strpos($type, 'array<') === 0) { $tmpResult = array(); $tmpType = new PropertyMetadata($property->getName()); $tmpType->setExpose(true)->setSerializedName($property->getSerializedName()); if (preg_match('/array<(?<type>[a-zA-Z\\\\]+)>/', $type, $matches)) { $tmpType->setType($matches['type']); } if ($direct == self::DIRECTION_UNSERIALIZE) { $existsData = $this->exposeValue($object, $property); } foreach ($value as $k => $v) { $tmpObject = $object; if ($direct == self::DIRECTION_UNSERIALIZE && isset($existsData[$k]) && is_object($existsData[$k])) { $tmpObject = $existsData[$k]; $inner = true; } $v = $this->handleValue($v, $tmpType, $direct, $tmpObject, $inner); $tmpResult[$k] = $v; unset($tmpObject); } $value = $tmpResult; unset($tmpResult, $tmpType); } elseif (is_object($value) && $direct == self::DIRECTION_SERIALIZE) { $value = $this->toArray($value); } elseif (is_array($value) && $direct == self::DIRECTION_UNSERIALIZE) { if ($inner) { $innerObject = $object; } else { $innerObject = $this->exposeValue($object, $property); } if (!is_object($innerObject) || !$innerObject instanceof $type) { if (PHP_VERSION_ID >= 50400) { $rc = new \ReflectionClass($type); $innerObject = $rc->newInstanceWithoutConstructor(); } else { $innerObject = unserialize(sprintf('O:%d:"%s":0:{}', strlen($type), $type)); } } $value = $this->toObject($value, $innerObject); } elseif ($type !== null) { throw new InvalidArgumentException(sprintf('Unsupported type: %s', $type)); } } return $value; }
/** * @param string $propertyName * @param string|null $propertyType * @return PropertyMetadata */ protected function makeSimpleProperty($propertyName, $propertyType = null) { $simpleProperty = new PropertyMetadata($propertyName); $simpleProperty->setType($propertyType); return $simpleProperty; }
/** * @dataProvider dateTimeProvider * @expectedException \Opensoft\SimpleSerializer\Exception\InvalidArgumentException */ public function testDenormalizationExceptionWrongFormat(DateTime $testDateTime) { $property = new PropertyMetadata('dateTime'); $property->setType('DateTime<Y-m-d H:i>'); $this->datetimeTransformer->denormalize($testDateTime->format(DateTime::COOKIE), $property, new \stdClass()); }