public function onBeforeGenerate(AbstractMetaSpec $spec, MetaSpecMatcher $matcher, Type $type) { $hasDefaultElement = false; foreach ($type->getAnnotations("Skrz\\Meta\\XML\\XmlElement") as $xmlElement) { /** @var XmlElement $xmlElement */ if ($xmlElement->name === null) { $xmlElement->name = $type->getShortName(); } if ($xmlElement->group === XmlElement::DEFAULT_GROUP) { $hasDefaultElement = true; } } if (!$hasDefaultElement) { $annotations = $type->getAnnotations(); $annotations[] = $xmlElement = new XmlElement(); $xmlElement->name = $type->getShortName(); $type->setAnnotations($annotations); } foreach ($type->getProperties() as $property) { if ($property->hasAnnotation("Skrz\\Meta\\Transient")) { continue; } if ($property->isPrivate()) { throw new MetaException("Private property '{$type->getName()}::\${$property->getName()}'. " . "Either make the property protected/public if you need to process it, " . "or mark it using @Transient annotation."); } if (get_class($property->getType()) === "Skrz\\Meta\\Reflection\\MixedType") { throw new MetaException("Property {$type->getName()}::\${$property->getName()} of type mixed. " . "Either add @var annotation with non-mixed type, " . "or mark it using @Transient annotation."); } $hasDefaultGroup = false; foreach ($property->getAnnotations("Skrz\\Meta\\XML\\XmlAnnotationInterface") as $annotation) { /** @var XmlAnnotationInterface $annotation */ if ($annotation->getGroup() === XmlElement::DEFAULT_GROUP) { $hasDefaultGroup = true; } if ($annotation instanceof XmlElement) { /** @var XmlElement $annotation */ if ($annotation->name === null) { $annotation->name = $property->getName(); } } elseif ($annotation instanceof XmlAttribute) { /** @var XmlAttribute $annotation */ if ($annotation->name === null) { $annotation->name = $property->getName(); } } } if (!$hasDefaultGroup) { $annotations = $property->getAnnotations(); $annotations[] = $arrayOffset = new XmlElement(); $arrayOffset->name = $property->getName(); $property->setAnnotations($annotations); } } }
public function onBeforeGenerate(AbstractMetaSpec $spec, MetaSpecMatcher $matcher, Type $type) { $annotations = $type->getAnnotations(); foreach ($type->getAnnotations("Skrz\\Meta\\JSON\\JsonDiscriminatorMap") as $jsonDiscriminatorMap) { /** @var JsonDiscriminatorMap $jsonDiscriminatorMap */ $annotations[] = $phpDiscriminatorMap = new PhpDiscriminatorMap(); $phpDiscriminatorMap->map = $jsonDiscriminatorMap->map; $phpDiscriminatorMap->group = "json:" . $jsonDiscriminatorMap->group; } foreach ($type->getAnnotations("Skrz\\Meta\\JSON\\JsonDiscriminatorProperty") as $jsonDiscriminatorProperty) { /** @var JsonDiscriminatorProperty $jsonDiscriminatorProperty */ $annotations[] = $phpDiscriminatorOffset = new PhpDiscriminatorOffset(); $phpDiscriminatorOffset->offset = $jsonDiscriminatorProperty->name; $phpDiscriminatorOffset->group = "json:" . $jsonDiscriminatorProperty->group; } $type->setAnnotations($annotations); foreach ($type->getProperties() as $property) { if ($property->hasAnnotation("Skrz\\Meta\\Transient")) { continue; } $hasDefaultGroup = false; foreach ($property->getAnnotations("Skrz\\Meta\\JSON\\JsonProperty") as $annotation) { /** @var JsonProperty $annotation */ if ($annotation->group === JsonProperty::DEFAULT_GROUP) { $hasDefaultGroup = true; } if ($annotation->name === null) { $annotation->name = $property->getName(); } } $annotations = $property->getAnnotations(); if (!$hasDefaultGroup) { $annotations[] = $jsonProperty = new JsonProperty(); $jsonProperty->name = $property->getName(); } $property->setAnnotations($annotations); foreach ($property->getAnnotations("Skrz\\Meta\\JSON\\JsonProperty") as $jsonProperty) { /** @var JsonProperty $jsonProperty */ $annotations[] = $arrayOffset = new PhpArrayOffset(); $arrayOffset->offset = $jsonProperty->name; $arrayOffset->group = "json:" . $jsonProperty->group; $arrayOffset->ignoreNull = $jsonProperty->ignoreNull; } $property->setAnnotations($annotations); } }