Skip to content

Commit

Permalink
Getters that would have returned ResourceContainer(s) now attempt to
Browse files Browse the repository at this point in the history
return the contained object(s).
  • Loading branch information
dcarbone committed Aug 10, 2017
1 parent 4a0ffee commit df89fd0
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/ClassGenerator/Generator/MethodGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,30 @@ public static function implementConstructor(Config $config, ClassTemplate $class
*/
public static function implementMethodsForProperty(Config $config, ClassTemplate $classTemplate, BasePropertyTemplate $propertyTemplate) {
if ($propertyTemplate->requiresGetter()) {
$classTemplate->addMethod(self::createGetter($config, $propertyTemplate));
if ($propertyTemplate->getFHIRElementType() === 'ResourceContainer') {
$method = new BaseMethodTemplate($config, sprintf('get%s', NameUtils::getPropertyMethodName($propertyTemplate->getName())));
$method->setDocumentation($propertyTemplate->getDocumentation());
if ($propertyTemplate->isCollection()) {
$classTemplate->addImport(NSUtils::generateRootNamespace($config, 'FHIRResourceContainer'));
$method->addLineToBody("if (count(\$this->{$propertyTemplate->getName()}) > 0) { ");
$method->addLineToBody(' $resources = [];');
$method->addLineToBody(" foreach(\$this->{$propertyTemplate->getName()} as \$container) {");
$method->addLineToBody(' if ($container instanceof FHIRResourceContainer) {');
$method->addLineToBody(' $resources[] = $container->jsonSerialize();');
$method->addLineToBody(' }');
$method->addLineToBody(' }');
$method->addLineToBody(' return $resources;');
$method->addLineToBody('}');
$method->addLineToBody('return [];');
$method->setReturnValueType('array');
} else {
$method->addLineToBody("return isset(\$this->{$propertyTemplate->getName()}) ? \$this->{$propertyTemplate->getName()}->jsonSerialize() : null;");
$method->setReturnValueType('mixed');
}
$classTemplate->addMethod($method);
} else {
$classTemplate->addMethod(self::createGetter($config, $propertyTemplate));
}
}

if ($propertyTemplate->requireSetter()) {
Expand Down

0 comments on commit df89fd0

Please sign in to comment.