Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
dataground authored Oct 4, 2017
2 parents 4ac80a0 + babd4bb commit 0f76cd8
Show file tree
Hide file tree
Showing 47 changed files with 1,630 additions and 1,714 deletions.
14 changes: 5 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
/curl.xml
/serialized.xml
/out.json
/parse.php
/index.php
composer.phar
vendor/
composer.lock
/vendor
/composer.lock
/tmp
/class.Diff.php
*.iml
.idea/
/.svn
/serialized.json
/curl.json
/curl.json
/*.iml
/.idea
22 changes: 9 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ require __DIR__.'/vendor/autoload.php';

$xsdPath = 'path to wherever you un-zipped the xsd files';

$generator = new \DCarbone\PHPFHIR\ClassGenerator\Generator($xsdPath);
$config = new \DCarbone\PHPFHIR\ClassGenerator\Config(['xsdPath' => $xsdPath]);
$generator = new \DCarbone\PHPFHIR\ClassGenerator\Generator($config);

$generator->generate();
```
Expand Down Expand Up @@ -103,14 +104,6 @@ $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);
```
Expand All @@ -128,17 +121,20 @@ $sxe = $object->xmlSerialize(true);
XML Serialization utilizes [SimpleXMLElement](http://php.net/manual/en/class.simplexmlelement.php).

## Custom XML Serialization
In some cases, vendors may deviate from the FHIR spec and require some properties of a class to be
In some cases, vendors may deviate from the FHIR spec and require some properties of a class to be
serialized as xml attributes instead of a child. The generator supports this through the following configuration.

```php
require __DIR__.'/vendor/autoload.php';

$xsdPath = 'path to wherever you un-zipped the xsd files';

\DCarbone\PHPFHIR\ClassGenerator\Generator\MethodGenerator::addXmlSerializationAttributeOverride('SomeFHIRModel', 'somePropertyName');
$config = new \DCarbone\PHPFHIR\ClassGenerator\Config([
'xsdPath' => $xsdPath,
'xmlSerializationAttributeOverrides' => ['SomeFHIRModel' => 'somePropertyName']
);

$generator = new \DCarbone\PHPFHIR\ClassGenerator\Generator($xsdPath);
$generator = new \DCarbone\PHPFHIR\ClassGenerator\Generator($config);

$generator->generate();

Expand All @@ -148,7 +144,7 @@ See the integration tests for a working example.

## Testing

Currently this library is being tested against v0.0.82 and v1.0.2. using the open server available here:
Currently this library is being tested against v0.0.82, v1.0.2, and v1.8.0 using the open server available here:
http://fhir2.healthintersections.com.au/open/ .

## TODO
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
],

"require": {
"php": ">=5.3.3",
"myclabs/php-enum": "1.4.*"
"php": ">=5.4.0",
"myclabs/php-enum": "1.4.*",
"psr/log": "1.0.*"
},

"require-dev": {
"myena/default-logger": "@stable",
"phpunit/phpunit": "^5.6"
},

Expand Down
178 changes: 178 additions & 0 deletions src/ClassGenerator/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<?php namespace DCarbone\PHPFHIR\ClassGenerator;

/*
* Copyright 2016-2017 Daniel Carbone ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

use DCarbone\PHPFHIR\ClassGenerator\Utilities\NameUtils;
use DCarbone\PHPFHIR\Logger;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

/**
* Class Config
* @package DCarbone\PHPFHIR\ClassGenerator
*/
class Config implements LoggerAwareInterface {

use LoggerAwareTrait;

/** @var string */
private $xsdPath;
/** @var string */
private $outputPath = PHPFHIR_DEFAULT_OUTPUT_DIR;
/** @var string */
private $outputNamespace = PHPFHIR_DEFAULT_NAMESPACE;

/** @var array */
private $xmlSerializationAttributeOverrides = [];

/**
* Config constructor.
* @param array $conf
* @param \Psr\Log\LoggerInterface|null $logger
*/
public function __construct(array $conf = [], LoggerInterface $logger = null) {
if ($logger) {
$this->logger = new Logger($logger);
} else {
$this->logger = new Logger(new NullLogger());
}

foreach ($conf as $k => $v) {
$this->{'set' . ucfirst($k)}($v);
}

// be lazy...
$this->setXsdPath(isset($this->xsdPath) ? $this->xsdPath : null);
}

/**
* @return \DCarbone\PHPFHIR\Logger
*/
public function getLogger() {
return $this->logger;
}

/**
* @return string
*/
public function getXsdPath() {
return $this->xsdPath;
}

/**
* @param string $xsdPath
* @return \DCarbone\PHPFHIR\ClassGenerator\Config
*/
public function setXsdPath($xsdPath) {
// Bunch'o validation
if (false === is_dir($xsdPath)) {
throw new \RuntimeException('Unable to locate XSD dir "' . $xsdPath . '"');
}
if (false === is_readable($xsdPath)) {
throw new \RuntimeException('This process does not have read access to directory "' . $xsdPath . '"');
}
$this->xsdPath = rtrim($xsdPath, "/\\");
return $this;
}

/**
* @return string
*/
public function getOutputPath() {
return $this->outputPath;
}

/**
* @param string $outputPath
* @return \DCarbone\PHPFHIR\ClassGenerator\Config
*/
public function setOutputPath($outputPath) {
if (!is_dir($outputPath)) {
throw new \RuntimeException('Unable to locate output dir "' . $outputPath . '"');
}
if (!is_writable($outputPath)) {
throw new \RuntimeException(sprintf('Specified output path "%s" is not writable by this process.', $outputPath));
}
if (!is_readable($outputPath)) {
throw new \RuntimeException(sprintf('Specified output path "%s" is not readable by this process.', $outputPath));
}
$this->outputPath = $outputPath;
return $this;
}

/**
* @return array
*/
public function getXmlSerializationAttributeOverrides() {
return $this->xmlSerializationAttributeOverrides;
}

/**
* @param string $elementName
* @param string $attributeName
* @return bool
*/
public function getXmlSerializationAttributeOverride($elementName, $attributeName) {
return isset($this->xmlSerializationAttributeOverrides[$elementName]) && $this->xmlSerializationAttributeOverrides[$elementName] === $attributeName;
}

/**
* @param string $elementName
* @param string $propertyName
* @return \DCarbone\PHPFHIR\ClassGenerator\Config
*/
public function setXmlSerializationAttributeOverride($elementName, $propertyName) {
$this->xmlSerializationAttributeOverrides[$elementName] = $propertyName;
return $this;
}

/**
* @param array $xmlSerializationAttributeOverrides
* @return \DCarbone\PHPFHIR\ClassGenerator\Config
*/
public function setXmlSerializationAttributeOverrides(array $xmlSerializationAttributeOverrides) {
$this->xmlSerializationAttributeOverrides = [];
foreach($xmlSerializationAttributeOverrides as $k => $v) {
$this->setXmlSerializationAttributeOverride($k, $v);
}
return $this;
}

/**
* @return string
*/
public function getOutputNamespace() {
return $this->outputNamespace;
}

/**
* @param string $outputNamespace
* @return \DCarbone\PHPFHIR\ClassGenerator\Config
*/
public function setOutputNamespace($outputNamespace) {
if (null === $outputNamespace) {
$outputNamespace = PHPFHIR_DEFAULT_NAMESPACE;
}
if (false === NameUtils::isValidNSName($outputNamespace)) {
throw new \InvalidArgumentException(sprintf('Specified root namespace "%s" is not a valid PHP namespace.', $outputNamespace));
}
$this->outputNamespace = trim($outputNamespace, "\\;");
return $this;
}
}
5 changes: 2 additions & 3 deletions src/ClassGenerator/Enum/BaseObjectTypeEnum.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php namespace DCarbone\PHPFHIR\ClassGenerator\Enum;

/*
* Copyright 2016 Daniel Carbone ([email protected])
* Copyright 2016-2017 Daniel Carbone ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,8 +22,7 @@
* Class BaseObjectTypeEnum
* @package DCarbone\PHPFHIR\ClassGenerator\Enum
*/
class BaseObjectTypeEnum extends Enum
{
class BaseObjectTypeEnum extends Enum {
const ELEMENT = 'Element';
const BACKBONE_ELEMENT = 'BackboneElement';
const RESOURCE = 'Resource';
Expand Down
5 changes: 2 additions & 3 deletions src/ClassGenerator/Enum/ComplexClassTypesEnum.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php namespace DCarbone\PHPFHIR\ClassGenerator\Enum;

/*
* Copyright 2016 Daniel Carbone ([email protected])
* Copyright 2016-2017 Daniel Carbone ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,8 +22,7 @@
* Class ComplexClassTypesEnum
* @package DCarbone\PHPFHIR\ClassGenerator\Enum
*/
class ComplexClassTypesEnum extends Enum
{
class ComplexClassTypesEnum extends Enum {
const DOMAIN_RESOURCE = 'DomainResource';
const RESOURCE = 'Resource';
const ELEMENT = 'Element';
Expand Down
5 changes: 2 additions & 3 deletions src/ClassGenerator/Enum/ElementTypeEnum.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php namespace DCarbone\PHPFHIR\ClassGenerator\Enum;

/*
* Copyright 2016 Daniel Carbone ([email protected])
* Copyright 2016-2017 Daniel Carbone ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,8 +22,7 @@
* Class ElementTypeEnum
* @package DCarbone\PHPFHIR\ClassGenerator\Enum
*/
class ElementTypeEnum extends Enum
{
class ElementTypeEnum extends Enum {
const COMPLEX_TYPE = 'complextype';
const COMPLEX_CONTENT = 'complexcontent';

Expand Down
5 changes: 2 additions & 3 deletions src/ClassGenerator/Enum/PHPScopeEnum.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php namespace DCarbone\PHPFHIR\ClassGenerator\Enum;

/*
* Copyright 2016 Daniel Carbone ([email protected])
* Copyright 2016-2017 Daniel Carbone ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,8 +22,7 @@
* Class PHPScopeEnum
* @package DCarbone\PHPFHIR\ClassGenerator\Enum
*/
class PHPScopeEnum extends Enum
{
class PHPScopeEnum extends Enum {
const _PRIVATE = 'private';
const _PROTECTED = 'protected';
const _PUBLIC = 'public';
Expand Down
5 changes: 2 additions & 3 deletions src/ClassGenerator/Enum/PrimitivePropertyTypesEnum.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php namespace DCarbone\PHPFHIR\ClassGenerator\Enum;

/*
* Copyright 2016 Daniel Carbone ([email protected])
* Copyright 2016-2017 Daniel Carbone ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,8 +22,7 @@
* Class PrimitivePropertyTypesEnum
* @package DCarbone\PHPFHIR\ClassGenerator\Enum
*/
class PrimitivePropertyTypesEnum extends Enum
{
class PrimitivePropertyTypesEnum extends Enum {
const POSITIVE_INTEGER = 'positiveint';
const NEGATIVE_INTEGER = 'negativeint';
const UNSIGNED_INTEGER = 'unsignedint';
Expand Down
5 changes: 2 additions & 3 deletions src/ClassGenerator/Enum/SimpleClassTypesEnum.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php namespace DCarbone\PHPFHIR\ClassGenerator\Enum;

/*
* Copyright 2016 Daniel Carbone ([email protected])
* Copyright 2016-2017 Daniel Carbone ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,8 +22,7 @@
* Class SimpleClassTypesEnum
* @package DCarbone\PHPFHIR\ClassGenerator\Enum
*/
class SimpleClassTypesEnum extends Enum
{
class SimpleClassTypesEnum extends Enum {
const PRIMITIVE = 'primitive';
const _LIST = 'list';
const BASE = '';
Expand Down
Loading

0 comments on commit 0f76cd8

Please sign in to comment.