/** * Constructor * * @param \DCarbone\PHPFHIR\ClassGenerator\Template\Property\BasePropertyTemplate $propertyTemplate */ public function __construct(BasePropertyTemplate $propertyTemplate) { $name = sprintf('get%s', NameUtils::getPropertyMethodName($propertyTemplate->getName())); parent::__construct($name); $this->setDocumentation($propertyTemplate->getDocumentation()); $this->_property = $propertyTemplate; }
/** * Constructor * * @param \DCarbone\PHPFHIR\ClassGenerator\Template\Property\BasePropertyTemplate $propertyTemplate */ public function __construct(BasePropertyTemplate $propertyTemplate) { if ($propertyTemplate->isCollection()) { $name = sprintf('add%s', NameUtils::getPropertyMethodName($propertyTemplate->getName())); } else { $name = sprintf('set%s', NameUtils::getPropertyMethodName($propertyTemplate->getName())); } parent::__construct($name, new PHPScopeEnum(PHPScopeEnum::_PUBLIC)); $this->setDocumentation($propertyTemplate->getDocumentation()); $this->setReturnValueType('$this'); $this->setReturnStatement('$this'); }
/** * @param ClassTemplate $classTemplate */ public static function implementXMLSerialize(ClassTemplate $classTemplate) { $method = new BaseMethodTemplate('xmlSerialize'); $method->addParameter(new BaseParameterTemplate('returnSXE', 'boolean', 'false')); $method->addParameter(new BaseParameterTemplate('sxe', '\\SimpleXMLElement', 'null')); $method->setReturnStatement('$sxe->saveXML()'); $method->setReturnValueType('string|\\SimpleXMLElement'); $classTemplate->addMethod($method); $properties = $classTemplate->getProperties(); $simple = true; if (2 === count($properties)) { foreach ($properties as $property) { $name = $property->getName(); if ('_fhirElementName' === $name || 'value' === $name) { continue; } $simple = false; break; } } else { $simple = false; } $rootElementName = str_replace(NameUtils::$classNameSearch, NameUtils::$classNameReplace, $classTemplate->getElementName()); // If this is the root object... $method->addLineToBody(sprintf('if (null === $sxe) $sxe = new \\SimpleXMLElement(\'<%s xmlns="%s"></%s>\');', $rootElementName, FHIR_XMLNS, $rootElementName)); // For simple properties we need to simply add an attribute. if ($simple) { $method->addLineToBody('$sxe->addAttribute(\'value\', $this->value);'); } else { // Determine if this class is a child... if ($classTemplate->getExtendedElementMapEntry()) { $method->addLineToBody('parent::xmlSerialize(true, $sxe);'); } foreach ($properties as $property) { $name = $property->getName(); if ('_fhirElementName' === $name) { continue; } if (array_key_exists($rootElementName, self::$xmlSerializationAttributeOverrides) === true && self::$xmlSerializationAttributeOverrides[$rootElementName] === $name) { $method->addLineToBody(sprintf('if (null !== $this->%s) $sxe->addAttribute(\'%s\', (string)$this->%s);', $name, $name, $name)); continue; } if ($property->isCollection()) { $method->addLineToBody(sprintf('if (0 < count($this->%s)) {', $name)); $method->addLineToBody(sprintf(' foreach($this->%s as $%s) {', $name, $name)); $method->addLineToBody(sprintf(' $%s->xmlSerialize(true, $sxe->addChild(\'%s\'));', $name, $name)); $method->addLineToBody(' }'); $method->addLineToBody('}'); } else { if ($property->isPrimitive() || $property->isList() || $property->isHTML()) { $method->addLineToBody(sprintf('if (null !== $this->%s) {', $name)); $method->addLineToBody(sprintf(' $%sElement = $sxe->addChild(\'%s\');', $name, $name)); $method->addLineToBody(sprintf(' $%sElement->addAttribute(\'value\', (string)$this->%s);', $name, $name)); $method->addLineToBody('}'); } else { $method->addLineToBody(sprintf('if (null !== $this->%s) $this->%s->xmlSerialize(true, $sxe->addChild(\'%s\'));', $name, $name, $name)); } } } } $method->addLineToBody('if ($returnSXE) return $sxe;'); }
/** * @param BaseMethodTemplate $method */ public function addMethod(BaseMethodTemplate $method) { $this->_methods[$method->getName()] = $method; }