Пример #1
0
 /**
  * @param XSDMap $XSDMap
  * @param ClassTemplate $classTemplate
  * @param \SimpleXMLElement $element
  * @param string $documentation
  * @param string $maxOccurs
  * @return BasePropertyTemplate|null
  */
 public static function buildProperty(XSDMap $XSDMap, ClassTemplate $classTemplate, \SimpleXMLElement $element, $documentation = null, $maxOccurs = null)
 {
     $propertyTemplate = new BasePropertyTemplate();
     $attributes = $element->attributes();
     if (null === $documentation) {
         $propertyTemplate->setDocumentation(XMLUtils::getDocumentation($element));
     }
     if (null === $maxOccurs && isset($attributes['maxOccurs'])) {
         $maxOccurs = (string) $attributes['maxOccurs'];
     }
     if (null !== $maxOccurs && '' !== $maxOccurs) {
         $propertyTemplate->setCollection(self::determineIfCollection($maxOccurs));
     }
     $name = (string) $attributes['name'];
     $type = (string) $attributes['type'];
     $ref = (string) $attributes['ref'];
     if ('' === $name) {
         if ('' === $ref) {
             trigger_error(sprintf('Encountered property on FHIR object "%s" with no "name" or "ref" attribute, cannot create property for it.  Property definition: "%s"', $classTemplate->getElementName(), $element->saveXML()));
             return null;
         }
         if (0 === strpos($ref, 'xhtml')) {
             $propertyTemplate->setName(substr($ref, 6));
             $propertyTemplate->setFHIRElementType('html');
             $propertyTemplate->setHTML(true);
             $propertyTemplate->setPHPType('string');
             return $propertyTemplate;
         }
         $name = $ref;
         $type = $ref;
     }
     $propertyTemplate->setName($name);
     $propertyTemplate->setFHIRElementType($type);
     // TODO: Implement proper primitive types
     if (false !== strpos($type, '-primitive')) {
         $propertyTemplate->setPrimitive(true);
         $propertyTemplate->setPHPType('string');
     } else {
         if (false !== strpos($type, '-list')) {
             $propertyTemplate->setList(true);
             $propertyTemplate->setPHPType('string');
         } else {
             if (0 === strpos($type, 'xs:')) {
                 $propertyTemplate->setPHPType($XSDMap->getClassUseStatementForFHIRElementName(substr($type, 3)));
             } else {
                 $propertyTemplate->setPHPType($XSDMap->getClassUseStatementForFHIRElementName($type));
             }
         }
     }
     return $propertyTemplate;
 }
Пример #2
0
 /**
  * @param ClassTemplate $classTemplate
  */
 public function addEntry(ClassTemplate $classTemplate)
 {
     $fhirElementName = $classTemplate->getElementName();
     $extendedMapEntry = $classTemplate->getExtendedElementMapEntry();
     $this->_bigDumbMap[$fhirElementName] = array('fullClassName' => $classTemplate->compileFullyQualifiedClassName(true), 'extendedElementName' => $extendedMapEntry ? $extendedMapEntry->getFHIRElementName() : null, 'properties' => array());
     foreach ($classTemplate->getMethods() as $method) {
         if ($method instanceof SetterMethodTemplate) {
             /** @var \DCarbone\PHPFHIR\ClassGenerator\Template\Parameter\PropertyParameterTemplate $parameter */
             foreach ($method->getParameters() as $parameter) {
                 $property = $parameter->getProperty();
                 $this->_bigDumbMap[$fhirElementName]['properties'][$property->getName()] = array('setter' => $method->getName(), 'element' => $property->getFHIRElementType(), 'type' => $property->getPHPType());
             }
         }
     }
 }
Пример #3
0
 /**
  * @param ClassTemplate $classTemplate
  */
 public function addPHPFHIRClassEntry(ClassTemplate $classTemplate)
 {
     $this->addEntry($classTemplate->compileFullyQualifiedClassName(false), $classTemplate->compileFullOutputPath($this->outputPath));
 }
Пример #4
0
 /**
  * @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;');
 }
Пример #5
0
 /**
  * @param string $fhirElementName
  * @param XSDMap $XSDMap
  * @param ClassTemplate $classTemplate
  */
 public static function findParentElementXSDMapEntry($fhirElementName, XSDMap $XSDMap, ClassTemplate $classTemplate)
 {
     if (isset($XSDMap[$fhirElementName])) {
         $classTemplate->setExtendedElementMapEntry($XSDMap[$fhirElementName]);
     }
 }