Skip to content

Commit

Permalink
using foreach loop in type constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Aug 17, 2024
1 parent 19a4222 commit 2cfa9f2
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 108 deletions.
11 changes: 10 additions & 1 deletion template/types/methods/constructors/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ public function __construct(null|array<?php if ($type->isValueContainer()) : ?>|
}
}<?php endif; ?>

<?php foreach($properties as $property) :
$_parsed = [];
foreach($data as $field => $value) {
<?php
$propertyIndex = 0;
foreach($properties as $property) :
if ($property->isOverloaded()) {
continue;
}
Expand All @@ -66,6 +70,7 @@ public function __construct(null|array<?php if ($type->isValueContainer()) : ?>|
[
'config' => $config,
'type' => $type,
'propertyIndex' => $propertyIndex,
'property' => $property,
]
);
Expand All @@ -75,10 +80,14 @@ public function __construct(null|array<?php if ($type->isValueContainer()) : ?>|
[
'config' => $config,
'type' => $type,
'propertyIndex' => $propertyIndex,
'property' => $property
]
);
endif;
$propertyIndex++;
endforeach; ?>

}
}
<?php return ob_get_clean();
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
/** @var \DCarbone\PHPFHIR\Config\VersionConfig $config */
/** @var \DCarbone\PHPFHIR\Definition\Type $type */
/** @var \DCarbone\PHPFHIR\Definition\Property $property */
/** @var int $propertyIndex */

$propertyType = $property->getValueFHIRType();
$fieldConstantName = $property->getFieldConstantName();

$requireArgs = [
'config' => $config,
'propertyIndex' => $propertyIndex,
'property' => $property
];

Expand Down
7 changes: 6 additions & 1 deletion template/types/methods/constructors/primitive_container.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ public function __construct(<?php echo TypeHintUtils::propertySetterTypeHint($co
$this->_addFHIRComment($data[PHPFHIRConstants::JSON_FIELD_FHIR_COMMENTS]);
}
}<?php endif; ?>
<?php foreach ($properties as $property) :
<?php
$propertyIndex = 0;
foreach ($properties as $property) :
if ($property->isOverloaded()) :
continue;
endif;
Expand All @@ -68,9 +70,12 @@ public function __construct(<?php echo TypeHintUtils::propertySetterTypeHint($co
[
'config' => $config,
'type' => $type,
'propertyIndex' => $propertyIndex,
'property' => $property,
]
);
$propertyIndex++;
endforeach; ?>

}
<?php return ob_get_clean();
42 changes: 22 additions & 20 deletions template/types/methods/constructors/property_setter_default.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,38 @@
* limitations under the License.
*/

/** @var \DCarbone\PHPFHIR\Config $config */
/** @var \DCarbone\PHPFHIR\Definition\Property $property */
/** @var int $propertyIndex */

$propertyTypeClassName = $property->getValueFHIRType()->getClassName();
$propertyFieldConst = $property->getFieldConstantName();
$propertyFieldConstExt = $property->getFieldConstantExtensionName();
$setter = $property->getSetterName();

ob_start(); ?>
if (array_key_exists(self::<?php echo $propertyFieldConst; ?>, $data)) {
ob_start();
if (0 === $propertyIndex) : ?>
if <?php else : ?> else if <?php endif; ?>(self::<?php echo $propertyFieldConst; ?> === $field) {
<?php if ($property->isCollection()) : ?>
if (is_array($data[self::<?php echo $propertyFieldConst; ?>])) {
foreach($data[self::<?php echo $propertyFieldConst; ?>] as $v) {
if ($v instanceof <?php echo $propertyTypeClassName; ?>) {
$this-><?php echo $setter; ?>($v);
} else {
$this-><?php echo $setter; ?>(new <?php echo $propertyTypeClassName; ?>($v));
if (is_array($value)) {
foreach($value as $v) {
if ($v instanceof <?php echo $propertyTypeClassName; ?>) {
$this-><?php echo $setter; ?>($v);
} else {
$this-><?php echo $setter; ?>(new <?php echo $propertyTypeClassName; ?>($v));
}
}
} elseif ($value instanceof <?php echo $propertyTypeClassName; ?>) {
$this-><?php echo $setter; ?>($value);
} else {
$this-><?php echo $setter; ?>(new <?php echo $propertyTypeClassName; ?>($value));
}
} elseif ($data[self::<?php echo $propertyFieldConst; ?>] instanceof <?php echo $propertyTypeClassName; ?>) {
$this-><?php echo $setter; ?>($data[self::<?php echo $propertyFieldConst; ?>]);
} else {
$this-><?php echo $setter; ?>(new <?php echo $propertyTypeClassName; ?>($data[self::<?php echo $propertyFieldConst; ?>]));
}
<?php else : ?>
if ($data[self::<?php echo $propertyFieldConst; ?>] instanceof <?php echo $propertyTypeClassName; ?>) {
$this-><?php echo $setter; ?>($data[self::<?php echo $propertyFieldConst; ?>]);
} else {
$this-><?php echo $setter; ?>(new <?php echo $propertyTypeClassName; ?>($data[self::<?php echo $propertyFieldConst; ?>]));
}
if ($value instanceof <?php echo $propertyTypeClassName; ?>) {
$this-><?php echo $setter; ?>($value);
} else {
$this-><?php echo $setter; ?>(new <?php echo $propertyTypeClassName; ?>($value));
}
<?php endif; ?>
}
<?php
}<?php
return ob_get_clean(); ?>
40 changes: 21 additions & 19 deletions template/types/methods/constructors/property_setter_primitive.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,35 @@
* limitations under the License.
*/

/** @var \DCarbone\PHPFHIR\Config $config */
/** @var \DCarbone\PHPFHIR\Definition\Property $property */
/** @var int $propertyIndex */

$propertyFieldConst = $property->getFieldConstantName();
$setter = $property->getSetterName();

ob_start(); ?>
if (array_key_exists(self::<?php echo $propertyFieldConst; ?>, $data)) {
ob_start();
if (0 === $propertyIndex) : ?>
if <?php else : ?> else if <?php endif; ?>(self::<?php echo $propertyFieldConst; ?> === $field) {
<?php if ($property->isCollection()) : ?>
if (is_array($data[self::<?php echo $propertyFieldConst; ?>])) {
foreach($data[self::<?php echo $propertyFieldConst; ?>] as $v) {
if (!($v instanceof <?php echo $property->getValueFHIRType()->getClassName(); ?>)) {
$v = new <?php echo $property->getValueFHIRType()->getClassName(); ?>($v);
if (is_array($value)) {
foreach($value as $v) {
if (!($v instanceof <?php echo $property->getValueFHIRType()->getClassName(); ?>)) {
$v = new <?php echo $property->getValueFHIRType()->getClassName(); ?>($v);
}
$this-><?php echo $setter; ?>($v);
}
$this-><?php echo $setter; ?>($v);
} else if ($value instanceof <?php echo $property->getValueFHIRType()->getClassName(); ?>) {
$this-><?php echo $setter; ?>($data[self::<?php echo $propertyFieldConst; ?>]);
} else {
$this-><?php echo $setter; ?>(new <?php echo $property->getValueFHIRType()->getClassName(); ?>($value));
}
} else if ($data[self::<?php echo $propertyFieldConst; ?>] instanceof <?php echo $property->getValueFHIRType()->getClassName(); ?>) {
$this-><?php echo $setter; ?>($data[self::<?php echo $propertyFieldConst; ?>]);
} else {
$this-><?php echo $setter; ?>(new <?php echo $property->getValueFHIRType()->getClassName(); ?>($data[self::<?php echo $propertyFieldConst; ?>]));
}
<?php else : ?>
if ($data[self::<?php echo $propertyFieldConst; ?>] instanceof <?php echo $property->getvalueFHIRType()->getClassName(); ?>) {
$this-><?php echo $setter; ?>($data[self::<?php echo $propertyFieldConst; ?>]);
} else {
$this-><?php echo $setter; ?>(new <?php echo $property->getValueFHIRType()->getClassName(); ?>($data[self::<?php echo $propertyFieldConst; ?>]));
}
if ($value instanceof <?php echo $property->getvalueFHIRType()->getClassName(); ?>) {
$this-><?php echo $setter; ?>($value);
} else {
$this-><?php echo $setter; ?>(new <?php echo $property->getValueFHIRType()->getClassName(); ?>($value));
}
<?php endif; ?>
}
<?php
}<?php
return ob_get_clean();
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
* limitations under the License.
*/

/** @var \DCarbone\PHPFHIR\Config\VersionConfig $configf */
/** @var \DCarbone\PHPFHIR\Config\VersionConfig $config */
/** @var \DCarbone\PHPFHIR\Definition\Property $property */
/** @var int $propertyIndex */

$propertyTypeClassName = $property->getValueFHIRType()->getClassName();
$propertyFieldConst = $property->getFieldConstantName();
Expand All @@ -26,42 +27,43 @@

// these types are a pain in the ass

ob_start(); ?>
if (array_key_exists(self::<?php echo $propertyFieldConst; ?>, $data) || array_key_exists(self::<?php echo $propertyFieldConstExt; ?>, $data)) {
$value = $data[self::<?php echo $propertyFieldConst; ?>] ?? null;
$ext = (isset($data[self::<?php echo $propertyFieldConstExt; ?>]) && is_array($data[self::<?php echo $propertyFieldConstExt; ?>])) ? $data[self::<?php echo $propertyFieldConstExt; ?>] : [];
if (null !== $value) {
if ($value instanceof <?php echo $propertyTypeClassName; ?>) {
$this-><?php echo $setter; ?>($value);
} else <?php if ($property->isCollection()) : ?>if (is_array($value)) {
foreach($value as $i => $v) {
if ($v instanceof <?php echo $propertyTypeClassName; ?>) {
$this-><?php echo $setter; ?>($v);
} else {
$iext = (isset($ext[$i]) && is_array($ext[$i])) ? $ext[$i] : [];
if (is_array($v)) {
$this-><?php echo $setter; ?>(new <?php echo $propertyTypeClassName; ?>(array_merge($v, $iext)));
ob_start();
if (0 === $propertyIndex) : ?>
if <?php else : ?> else if <?php endif; ?>((self::<?php echo $propertyFieldConst; ?> === $field || self::<?php echo $propertyFieldConstExt; ?> === $field) && !isset($_parsed[self::<?php echo $propertyFieldConst; ?>])) {
$value = $data[self::<?php echo $propertyFieldConst; ?>] ?? null;
$ext = (isset($data[self::<?php echo $propertyFieldConstExt; ?>]) && is_array($data[self::<?php echo $propertyFieldConstExt; ?>])) ? $data[self::<?php echo $propertyFieldConstExt; ?>] : [];
if (null !== $value) {
if ($value instanceof <?php echo $propertyTypeClassName; ?>) {
$this-><?php echo $setter; ?>($value);
} else <?php if ($property->isCollection()) : ?>if (is_array($value)) {
foreach($value as $i => $v) {
if ($v instanceof <?php echo $propertyTypeClassName; ?>) {
$this-><?php echo $setter; ?>($v);
} else {
$this-><?php echo $setter; ?>(new <?php echo $propertyTypeClassName; ?>([<?php echo $propertyTypeClassName; ?>::FIELD_VALUE => $v] + $iext));
$iext = (isset($ext[$i]) && is_array($ext[$i])) ? $ext[$i] : [];
if (is_array($v)) {
$this-><?php echo $setter; ?>(new <?php echo $propertyTypeClassName; ?>(array_merge($v, $iext)));
} else {
$this-><?php echo $setter; ?>(new <?php echo $propertyTypeClassName; ?>([<?php echo $propertyTypeClassName; ?>::FIELD_VALUE => $v] + $iext));
}
}
}
} else<?php endif; ?>if (is_array($value)) {
$this-><?php echo $setter; ?>(new <?php echo $propertyTypeClassName; ?>(array_merge($ext, $value)));
} else {
$this-><?php echo $setter; ?>(new <?php echo $propertyTypeClassName; ?>([<?php echo $propertyTypeClassName; ?>::FIELD_VALUE => $value] + $ext));
}
} else<?php endif; ?>if (is_array($value)) {
$this-><?php echo $setter; ?>(new <?php echo $propertyTypeClassName; ?>(array_merge($ext, $value)));
} else {
$this-><?php echo $setter; ?>(new <?php echo $propertyTypeClassName; ?>([<?php echo $propertyTypeClassName; ?>::FIELD_VALUE => $value] + $ext));
}
} elseif ([] !== $ext) {
} elseif ([] !== $ext) {
<?php if ($property->isCollection()) : ?>
foreach($ext as $iext) {
$this-><?php echo $setter; ?>(new <?php echo $propertyTypeClassName; ?>($iext));
}
foreach($ext as $iext) {
$this-><?php echo $setter; ?>(new <?php echo $propertyTypeClassName; ?>($iext));
}
<?php else : ?>
$this-><?php echo $setter; ?>(new <?php echo $propertyTypeClassName; ?>($ext));
$this-><?php echo $setter; ?>(new <?php echo $propertyTypeClassName; ?>($ext));
<?php endif; ?>
} else {
$this-><?php echo $setter; ?>(new <?php echo $propertyTypeClassName; ?>(null));
}
}
<?php
} else {
$this-><?php echo $setter; ?>(new <?php echo $propertyTypeClassName; ?>(null));
}
$_parsed[self::<?php echo $propertyFieldConst; ?>] = true;
}<?php
return ob_get_clean(); ?>
Original file line number Diff line number Diff line change
Expand Up @@ -18,58 +18,59 @@

/** @var \DCarbone\PHPFHIR\Definition\Type $type */
/** @var \DCarbone\PHPFHIR\Definition\Property $property */
/** @var int $propertyIndex */

$typeClassName = $type->getClassName();
$propertyName = $property->getName();
$propertyFieldConst = $property->getFieldConstantName();
$propertyType = $property->getValueFHIRType();
$setter = ($property->isCollection() ? 'add' : 'set') . ucfirst($propertyName);

ob_start(); ?>
if (isset($data[self::<?php echo $propertyFieldConst; ?>])) {
ob_start();
if (0 === $propertyIndex) : ?>
if <?php else : ?> else if <?php endif; ?>(self::<?php echo $propertyFieldConst; ?> === $field) {
<?php if ($property->isCollection()) : ?>
if (is_array($data[self::<?php echo $propertyFieldConst; ?>])) {
if (is_int(key($data[self::<?php echo $propertyFieldConst; ?>]))) {
$this->set<?php echo ucfirst($propertyName); ?>($data[self::<?php echo $propertyFieldConst; ?>]);
} else {
$typeClass = PHPFHIRTypeMap::getContainedTypeFromArray($data[self::<?php echo $propertyFieldConst; ?>]);
if (is_array($value)) {
if (is_int(key($value))) {
$this->set<?php echo ucfirst($propertyName); ?>($value);
} else {
$typeClass = PHPFHIRTypeMap::getContainedTypeFromArray($value);
if (null === $typeClass) {
throw new \InvalidArgumentException(sprintf(
'<?php echo $typeClassName; ?> - Unable to determine class for field "<?php echo $propertyName; ?>" from value: %s',
json_encode($value)
));
}
$this-><?php echo $setter; ?>(new $typeClass($value));
}
} elseif ($value instanceof <?php echo PHPFHIR_INTERFACE_CONTAINED_TYPE; ?>) {
$this-><?php echo $setter; ?>($value);
}
<?php else : ?>
if (is_object($value)) {
if ($value instanceof <?php echo PHPFHIR_INTERFACE_CONTAINED_TYPE; ?>) {
$this-><?php echo $setter; ?>($value);
} else {
throw new \InvalidArgumentException(sprintf(
'<?php echo $typeClassName; ?> - Field "<?php echo $propertyName; ?>" must be an object implementing <?php echo PHPFHIR_INTERFACE_CONTAINED_TYPE; ?>, object of type %s seen',
get_class($value)
));
}
} elseif (is_array($value)) {
$typeClass = PHPFHIRTypeMap::getContainedTypeFromArray($value);
if (null === $typeClass) {
throw new \InvalidArgumentException(sprintf(
'<?php echo $typeClassName; ?> - Unable to determine class for field "<?php echo $propertyName; ?>" from value: %s',
json_encode($data[self::<?php echo $propertyFieldConst; ?>])
json_encode($value)
));
}
$this-><?php echo $setter; ?>(new $typeClass($data[self::<?php echo $propertyFieldConst; ?>]));
}
} elseif ($data[self::<?php echo $propertyFieldConst; ?>] instanceof <?php echo PHPFHIR_INTERFACE_CONTAINED_TYPE; ?>) {
$this-><?php echo $setter; ?>($data[self::<?php echo $propertyFieldConst; ?>]);
}
<?php else : ?>
if (is_object($data[self::<?php echo $propertyFieldConst; ?>])) {
if ($data[self::<?php echo $propertyFieldConst; ?>] instanceof <?php echo PHPFHIR_INTERFACE_CONTAINED_TYPE; ?>) {
$this-><?php echo $setter; ?>($data[self::<?php echo $propertyFieldConst; ?>]);
$this-><?php echo $setter; ?>(new $typeClass($value));
} else {
throw new \InvalidArgumentException(sprintf(
'<?php echo $typeClassName; ?> - Field "<?php echo $propertyName; ?>" must be an object implementing <?php echo PHPFHIR_INTERFACE_CONTAINED_TYPE; ?>, object of type %s seen',
get_class($data[self::<?php echo $propertyFieldConst; ?>])
));
}
} elseif (is_array($data[self::<?php echo $propertyFieldConst; ?>])) {
$typeClass = PHPFHIRTypeMap::getContainedTypeFromArray($data[self::<?php echo $propertyFieldConst; ?>]);
if (null === $typeClass) {
throw new \InvalidArgumentException(sprintf(
'<?php echo $typeClassName; ?> - Unable to determine class for field "<?php echo $propertyName; ?>" from value: %s',
json_encode($data[self::<?php echo $propertyFieldConst; ?>])
json_encode($value)
));
}
$this-><?php echo $setter; ?>(new $typeClass($data[self::<?php echo $propertyFieldConst; ?>]));
} else {
throw new \InvalidArgumentException(sprintf(
'<?php echo $typeClassName; ?> - Unable to determine class for field "<?php echo $propertyName; ?>" from value: %s',
json_encode($data[self::<?php echo $propertyFieldConst; ?>])
));
}
<?php endif; ?>
}
<?php
}<?php
return ob_get_clean();

0 comments on commit 2cfa9f2

Please sign in to comment.