public function testShouldSkip() { $this->propertyMetadata->setExpose(true); $this->assertFalse($this->propertySkipper->shouldSkip($this->propertyMetadata)); $skipExclusionGroupSpecification = new GroupsSpecification(array('test1')); $this->propertySkipper->registerSpecification($skipExclusionGroupSpecification); $this->assertTrue($this->propertySkipper->shouldSkip($this->propertyMetadata)); $nonSkipExclusionGroupSpecification = new GroupsSpecification(array('test')); $this->propertySkipper->registerSpecification($nonSkipExclusionGroupSpecification); $this->assertTrue($this->propertySkipper->shouldSkip($this->propertyMetadata)); $this->propertySkipper->cleanUpSpecifications(); $this->propertySkipper->registerSpecification($nonSkipExclusionGroupSpecification); $this->assertFalse($this->propertySkipper->shouldSkip($this->propertyMetadata)); $skipExclusionVersionSpecification = new VersionSpecification('5.5'); $this->propertySkipper->registerSpecification($skipExclusionVersionSpecification); $this->assertTrue($this->propertySkipper->shouldSkip($this->propertyMetadata)); $nonSkipExclusionVersionSpecification = new VersionSpecification('5.1'); $this->propertySkipper->registerSpecification($nonSkipExclusionVersionSpecification); $this->assertTrue($this->propertySkipper->shouldSkip($this->propertyMetadata)); $this->propertySkipper->cleanUpSpecifications(); $this->propertySkipper->registerSpecification($nonSkipExclusionGroupSpecification); $this->assertFalse($this->propertySkipper->shouldSkip($this->propertyMetadata)); $this->propertySkipper->registerSpecification($skipExclusionGroupSpecification); $this->assertTrue($this->propertySkipper->shouldSkip($this->propertyMetadata)); $this->propertySkipper->cleanUpSpecifications(); $this->assertFalse($this->propertySkipper->shouldSkip($this->propertyMetadata)); $this->propertyMetadata->setExpose(false); $this->assertTrue($this->propertySkipper->shouldSkip($this->propertyMetadata)); }
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; }
/** * {@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; }
/** * @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 * @depends testSettersGetters */ public function testSerializeUnserialize() { $unitUnderTest = new PropertyMetadata('test'); $unitUnderTest->setExpose(true)->setSerializedName('serialize')->setType('string')->setGroups(array('test'))->setSinceVersion('1.0')->setUntilVersion('2.0'); $serializedString = serialize($unitUnderTest); $unserializedObject = unserialize($serializedString); $this->assertInstanceOf('Opensoft\\SimpleSerializer\\Metadata\\PropertyMetadata', $unserializedObject); $this->assertEquals('test', $unserializedObject->getName()); $this->assertTrue($unserializedObject->isExpose()); $this->assertEquals('string', $unserializedObject->getType()); $this->assertEquals('serialize', $unserializedObject->getSerializedName()); $this->assertEquals(array('test'), $unserializedObject->getGroups()); $this->assertEquals('1.0', $unserializedObject->getSinceVersion()); $this->assertEquals('2.0', $unserializedObject->getUntilVersion()); }
/** * @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; }