Skip to content

Commit

Permalink
Merge pull request #4 from dcarbone/0.4.x
Browse files Browse the repository at this point in the history
0.4.x
  • Loading branch information
dcarbone committed Mar 14, 2016
2 parents 64fb54e + f9ef00a commit 2c1df67
Show file tree
Hide file tree
Showing 27 changed files with 1,237 additions and 448 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/curl.xml
/serialized.xml
/out.json
/parse.php
/index.php
composer.phar
Expand All @@ -8,3 +11,5 @@ composer.lock
*.iml
.idea/
/.svn
/serialized.json
/curl.json
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This library requires the use of [Composer](https://getcomposer.org/)

Require entry:
```json
"dcarbone/php-fhir": "0.3.*"
"dcarbone/php-fhir": "0.4.*"
```

# Basic Workflow
Expand Down Expand Up @@ -76,6 +76,32 @@ $object = $parser->parse($yourResponseData);

```

## JSON Serialization

### PHP 5.3.x example:

```php
$json = json_encode($object->jsonSerialize());
```

### PHP \>= 5.4.0

```php
$json = json_encode($object);
```

## XML Serialization

```php
// To get an XML string back...
$xml = $object->xmlSerialize();

// To get back an instance of \SimpleXMLElement...
$sxe = $object->xmlSerialize(true);
```

XML Serialization utilizes [SimpleXMLElement](http://php.net/manual/en/class.simplexmlelement.php).

## Testing

Currently this library is being tested against v0.0.82 and v1.0.2. using the open server available here:
Expand Down
3 changes: 2 additions & 1 deletion files/php_fhir_constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
define('PHPFHIR_ROOT_DIR', realpath(dirname(__DIR__)));
define('PHPFHIR_DEFAULT_OUTPUT_DIR', realpath(PHPFHIR_ROOT_DIR.'/output'));
define('PHPFHIR_TEMPLATE_DIR', realpath(PHPFHIR_ROOT_DIR.'/templates'));
define('PHPFHIR_DEFAULT_NAMESPACE', 'PHPFHIRGenerated');
define('PHPFHIR_DEFAULT_NAMESPACE', 'PHPFHIRGenerated');
define('FHIR_XMLNS', 'http://hl7.org/fhir');
25 changes: 18 additions & 7 deletions src/ClassGenerator/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@

use DCarbone\PHPFHIR\ClassGenerator\Generator\ClassGenerator;
use DCarbone\PHPFHIR\ClassGenerator\Generator\XSDMapGenerator;
use DCarbone\PHPFHIR\ClassGenerator\Template\AutoloaderTemplate;
use DCarbone\PHPFHIR\ClassGenerator\Template\ParserMapTemplate;
use DCarbone\PHPFHIR\ClassGenerator\Template\ResponseParserTemplate;
use DCarbone\PHPFHIR\ClassGenerator\Template\PHPFHIR\AutoloaderTemplate;
use DCarbone\PHPFHIR\ClassGenerator\Template\PHPFHIR\JsonSerializableInterfaceTemplate;
use DCarbone\PHPFHIR\ClassGenerator\Template\PHPFHIR\ParserMapTemplate;
use DCarbone\PHPFHIR\ClassGenerator\Template\PHPFHIR\ResponseParserTemplate;
use DCarbone\PHPFHIR\ClassGenerator\Utilities\CopyrightUtils;
use DCarbone\PHPFHIR\ClassGenerator\Utilities\FileUtils;
use DCarbone\PHPFHIR\ClassGenerator\Utilities\NameUtils;
Expand All @@ -43,6 +44,9 @@ class Generator
/** @var ParserMapTemplate */
private $_mapTemplate;

/** @var JsonSerializableInterfaceTemplate */
private $_jsonSerializableInterface;

/**
* Constructor
*
Expand Down Expand Up @@ -97,12 +101,15 @@ protected function beforeGeneration()
$this->_autoloadMap = new AutoloaderTemplate($this->outputPath, $this->outputNamespace);

$this->_autoloadMap->addEntry(
sprintf('%s\\%s', $this->outputNamespace, 'PHPFHIRParserMap'),
sprintf('%s/%s', $this->outputPath, 'PHPFHIRParserMap.php')
$this->_mapTemplate->getClassName(),
$this->_mapTemplate->getClassPath()
);

$this->_jsonSerializableInterface = new JsonSerializableInterfaceTemplate($this->outputPath, $this->outputNamespace);
$this->_jsonSerializableInterface->writeToFile();
$this->_autoloadMap->addEntry(
sprintf('%s\\%s', $this->outputNamespace, 'PHPFHIRResponseParser'),
sprintf('%s/%s', $this->outputPath, 'PHPFHIRResponseParser.php')
$this->_jsonSerializableInterface->getClassName(),
$this->_jsonSerializableInterface->getClassPath()
);
}

Expand All @@ -115,6 +122,10 @@ protected function afterGeneration()
$this->_autoloadMap->writeToFile();

$responseParserTemplate = new ResponseParserTemplate($this->outputPath, $this->outputNamespace);
$this->_autoloadMap->addEntry(
$responseParserTemplate->getClassName(),
$responseParserTemplate->getClassPath()
);
$responseParserTemplate->writeToFile();
}

Expand Down
45 changes: 44 additions & 1 deletion src/ClassGenerator/Generator/ClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
*/

use DCarbone\PHPFHIR\ClassGenerator\Enum\ElementTypeEnum;
use DCarbone\PHPFHIR\ClassGenerator\Enum\PHPScopeEnum;
use DCarbone\PHPFHIR\ClassGenerator\Template\ClassTemplate;
use DCarbone\PHPFHIR\ClassGenerator\Template\PropertyTemplate;
use DCarbone\PHPFHIR\ClassGenerator\Utilities\ClassTypeUtils;
use DCarbone\PHPFHIR\ClassGenerator\Utilities\XMLUtils;
use DCarbone\PHPFHIR\ClassGenerator\XSDMap;

Expand All @@ -27,6 +30,7 @@
*/
abstract class ClassGenerator
{
/** @var string */
private static $_outputNamespace;

/**
Expand All @@ -47,7 +51,8 @@ public static function buildFHIRElementClassTemplate(XSDMap $XSDMap, XSDMap\XSDM
$classTemplate = new ClassTemplate(
$mapEntry->fhirElementName,
$mapEntry->className,
$mapEntry->namespace
$mapEntry->namespace,
ClassTypeUtils::getComplexClassType($mapEntry->sxe)
);

foreach($mapEntry->sxe->children('xs', true) as $_element)
Expand Down Expand Up @@ -76,14 +81,52 @@ public static function buildFHIRElementClassTemplate(XSDMap $XSDMap, XSDMap\XSDM
}
}

self::addBaseClassProperties($classTemplate, $mapEntry);

foreach($classTemplate->getProperties() as $propertyTemplate)
{
MethodGenerator::implementMethodsForProperty($classTemplate, $propertyTemplate);
}

self::addBaseClassInterfaces($classTemplate);
self::addBaseClassMethods($classTemplate);

return $classTemplate;
}

/**
* @param ClassTemplate $classTemplate
* @param XSDMap\XSDMapEntry $mapEntry
*/
public static function addBaseClassProperties(ClassTemplate $classTemplate, XSDMap\XSDMapEntry $mapEntry)
{
// Add the source element name to each class...
$property = new PropertyTemplate(new PHPScopeEnum(PHPScopeEnum::_PRIVATE), true, false);
$property->setDefaultValue($mapEntry->fhirElementName);
$property->setName('_fhirElementName');
$property->setPHPType('string');
$property->setPrimitive(true);
$classTemplate->addProperty($property);
}

/**
* @param ClassTemplate $classTemplate
*/
public static function addBaseClassMethods(ClassTemplate $classTemplate)
{
MethodGenerator::implementToString($classTemplate);
MethodGenerator::implementJsonSerialize($classTemplate);
MethodGenerator::implementXMLSerialize($classTemplate);
}

/**
* @param ClassTemplate $classTemplate
*/
public static function addBaseClassInterfaces(ClassTemplate $classTemplate)
{
$classTemplate->addImplementedInterface(sprintf('%s\\JsonSerializable', self::$_outputNamespace));
}

/**
* @param XSDMap $XSDMap
* @param \SimpleXMLElement $complexContent
Expand Down
Loading

0 comments on commit 2c1df67

Please sign in to comment.