Skip to content

Commit

Permalink
more small steps
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Nov 20, 2024
1 parent 7efb8af commit ba9140b
Show file tree
Hide file tree
Showing 17 changed files with 48 additions and 99 deletions.
12 changes: 6 additions & 6 deletions src/Version/Definition/Properties.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public function hasProperty(string $name): bool
*
* @return \DCarbone\PHPFHIR\Version\Definition\Property[]
*/
public function allPropertiesIterator(): iterable
public function getAllPropertiesIterator(): iterable
{
return SplFixedArray::fromArray($this->properties, preserveKeys: false);
}
Expand All @@ -168,7 +168,7 @@ public function allPropertiesIterator(): iterable
*
* @return \DCarbone\PHPFHIR\Version\Definition\Property[]
*/
public function allSortedPropertiesIterator(): iterable
public function getAllSortedPropertiesIterator(): iterable
{
$this->_buildLocalCaches();
return SplFixedArray::fromArray($this->_sortedProperties, preserveKeys: false);
Expand All @@ -179,7 +179,7 @@ public function allSortedPropertiesIterator(): iterable
*
* @return \DCarbone\PHPFHIR\Version\Definition\Property[]
*/
public function localPropertiesIterator(): iterable
public function getLocalPropertiesIterator(): iterable
{
$this->_buildLocalCaches();
return SplFixedArray::fromArray($this->_localProperties, preserveKeys: false);
Expand All @@ -190,7 +190,7 @@ public function localPropertiesIterator(): iterable
*
* @return \DCarbone\PHPFHIR\Version\Definition\Property[]
*/
public function localSortedPropertiesIterator(): iterable
public function getLocalSortedPropertiesIterator(): iterable
{
$this->_buildLocalCaches();
return SplFixedArray::fromArray($this->_localSortedProperties, preserveKeys: false);
Expand All @@ -200,10 +200,10 @@ public function localSortedPropertiesIterator(): iterable
* @param \DCarbone\PHPFHIR\Enum\TypeKind|null ...$kinds
* @return \DCarbone\PHPFHIR\Version\Definition\Property[]
*/
public function localPropertiesOfTypeKinds(bool $includeCollections, null|TypeKind... $kinds): iterable
public function getLocalPropertiesOfTypeKinds(bool $includeCollections, null|TypeKind... $kinds): iterable
{
$out = [];
foreach ($this->localPropertiesIterator() as $property) {
foreach ($this->getLocalPropertiesIterator() as $property) {
if (!$includeCollections && $property->isCollection()) {
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Version/Definition/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ public function hasLocalProperties(): bool
*/
public function hasLocalPropertiesWithValidations(): bool
{
foreach($this->getlocalProperties()->allSortedPropertiesIterator() as $property) {
foreach($this->getlocalProperties()->getAllSortedPropertiesIterator() as $property) {
if ([] !== $property->buildValidationMap()) {
return true;
}
Expand All @@ -366,7 +366,7 @@ public function hasLocalPropertiesWithValidations(): bool
public function getAllPropertiesIterator(): iterable
{
$properties = [];
foreach($this->getLocalProperties()->localPropertiesIterator() as $property) {
foreach($this->getLocalProperties()->getLocalPropertiesIterator() as $property) {
$properties[$property->getName()] = $property;
}
foreach($this->getParentTypes() as $parentType) {
Expand Down
2 changes: 1 addition & 1 deletion src/Version/Definition/TypeDecorationValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static function validateDecoration(Config $config, Version $version, Type
throw ExceptionUtils::createContainedTypeFlagMismatchException($types->isContainedType($versionName, $type), $type);
}

foreach ($type->getLocalProperties()->allPropertiesIterator() as $property) {
foreach ($type->getLocalProperties()->getAllPropertiesIterator() as $property) {
$name = $property->getName();
if (null === $name || '' === $name) {
throw ExceptionUtils::createPropertyMissingNameException($type, $property);
Expand Down
2 changes: 1 addition & 1 deletion src/Version/Definition/TypeDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ public static function parseUnionMemberTypes(Config $config, Types $types): void
$utype->getFHIRName()
)
);
foreach ($utype->getLocalProperties()->allPropertiesIterator() as $property) {
foreach ($utype->getLocalProperties()->getAllPropertiesIterator() as $property) {
$type->getLocalProperties()->addProperty(clone $property);
}
} else {
Expand Down
93 changes: 21 additions & 72 deletions src/Version/Definition/TypeImports.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@
* limitations under the License.
*/

use Countable;
use DCarbone\PHPFHIR\Enum\TypeKind;
use Iterator;

/**
* Class TypeImports
* @package DCarbone\PHPFHIR\Definition
*/
class TypeImports implements Iterator, Countable
class TypeImports implements \Countable
{
/** @var \DCarbone\PHPFHIR\Version\Definition\Type */
private Type $type;
Expand All @@ -48,12 +46,10 @@ public function __construct(Type $type)
/**
* @return \DCarbone\PHPFHIR\Version\Definition\TypeImport[]
*/
public function getImportMap(): array
public function getIterator(): \Iterator
{
if (!$this->parsed) {
$this->buildImports();
}
return $this->imports;
$this->buildImports();
return new \ArrayIterator($this->imports, \ArrayIterator::STD_PROP_LIST);
}

/**
Expand All @@ -62,9 +58,7 @@ public function getImportMap(): array
*/
public function getImportByType(Type $type): ?TypeImport
{
if (!$this->parsed) {
$this->buildImports();
}
$this->buildImports();
$fqn = $type->getFullyQualifiedClassName(false);
foreach ($this->imports as $import) {
if ($import->getFullyQualifiedClassname(false) === $fqn) {
Expand All @@ -81,9 +75,7 @@ public function getImportByType(Type $type): ?TypeImport
*/
public function getImportByClassAndNamespace(string $classname, string $namespace): ?TypeImport
{
if (!$this->parsed) {
$this->buildImports();
}
$this->buildImports();
foreach ($this->imports as $import) {
if ($import->getNamespace() === $namespace && $import->getClassname() === $classname) {
return $import;
Expand All @@ -98,69 +90,16 @@ public function getImportByClassAndNamespace(string $classname, string $namespac
*/
public function getImportByAlias(string $aliasName): ?TypeImport
{
if (!$this->parsed) {
$this->buildImports();
}
$this->buildImports();
return $this->imports[$aliasName] ?? null;
}

/**
* @return \DCarbone\PHPFHIR\Version\Definition\TypeImport
*/
public function current(): TypeImport
{
if (!$this->parsed) {
$this->buildImports();
}
return current($this->imports);
}

public function next(): void
{
if (!$this->parsed) {
$this->buildImports();
}
next($this->imports);
}

/**
* @return string|null
*/
public function key(): ?string
{
if (!$this->parsed) {
$this->buildImports();
}
return key($this->imports);
}

/**
* @return bool
*/
public function valid(): bool
{
if (!$this->parsed) {
$this->buildImports();
}
return null !== key($this->imports);
}

public function rewind(): void
{
if (!$this->parsed) {
$this->buildImports();
}
reset($this->imports);
}

/**
* @return int
*/
public function count(): int
{
if (!$this->parsed) {
$this->buildImports();
}
$this->buildImports();
return count($this->imports);
}

Expand All @@ -187,7 +126,9 @@ private function findNextAliasName(string $classname, string $namespace): string
*/
private function addImport(string $classname, string $namespace): void
{
$requiresImport = !str_starts_with($classname, '\\') && $namespace !== $this->type->getFullyQualifiedNamespace(false);
$requiresImport = !str_starts_with($classname, '\\') &&
ltrim($namespace, '\\') !== $this->type->getFullyQualifiedNamespace(false);

if (isset($this->imports[$classname])) {
// if we have already seen this type, move on.
if ($this->imports[$classname]->getNamespace() === $namespace) {
Expand All @@ -214,6 +155,10 @@ private function addImport(string $classname, string $namespace): void

private function buildImports(): void
{
if ($this->parsed) {
return;
}

// immediately set to true so we don't recurse ourselves to death.
$this->parsed = true;

Expand All @@ -224,7 +169,7 @@ private function buildImports(): void
$configNS = $this->type->getConfig()->getFullyQualifiedName(false);
$versionNS = $this->type->getVersion()->getFullyQualifiedName(false);

$sortedProperties = $this->type->getAllPropertiesIterator();
$allProperties = $this->type->getAllPropertiesIterator();

// non-abstract types must import config and xml writer
if (!$this->type->isAbstract()) {
Expand Down Expand Up @@ -264,14 +209,18 @@ private function buildImports(): void
}

// add property types to import statement
foreach ($sortedProperties as $property) {
foreach ($allProperties as $property) {
$propertyType = $property->getValueFHIRType();
if (null === $propertyType) {
continue;
}

$ptk = $propertyType->getKind();

if ($property->isOverloaded() && !$ptk->isOneOf(TypeKind::PRIMITIVE, TypeKind::LIST)) {
continue;
}

if ($ptk->isOneOf(TypeKind::RESOURCE_CONTAINER, TypeKind::RESOURCE_INLINE) &&
$typeNS !== $configNS) {
$this->addImport(PHPFHIR_INTERFACE_CONTAINED_TYPE, $versionNS);
Expand Down
8 changes: 4 additions & 4 deletions src/Version/Definition/TypePropertyDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public static function findPropertyType(Config $config, Types $types, Type $type
public static function findPropertyTypes(Config $config, Types $types): void
{
foreach ($types->getIterator() as $type) {
foreach ($type->getLocalProperties()->allPropertiesIterator() as $property) {
foreach ($type->getLocalProperties()->getAllPropertiesIterator() as $property) {
self::findPropertyType($config, $types, $type, $property);
}
}
Expand All @@ -137,9 +137,9 @@ public static function findOverloadedProperties(Config $config, Types $types): v
}
$parent = $type->getParentType();
while (null !== $parent) {
foreach ($type->getLocalProperties()->allPropertiesIterator() as $property) {
foreach ($type->getLocalProperties()->getAllPropertiesIterator() as $property) {
$propertyName = $property->getName();
foreach ($parent->getLocalProperties()->allPropertiesIterator() as $parentProperty) {
foreach ($parent->getLocalProperties()->getAllPropertiesIterator() as $parentProperty) {
if ($propertyName === $parentProperty->getName()) {
$logger->debug(
sprintf(
Expand Down Expand Up @@ -167,7 +167,7 @@ public static function setMissingPropertyNames(Config $config, Types $types): vo
{
$log = $config->getLogger();
foreach ($types->getIterator() as $type) {
foreach ($type->getLocalProperties()->allPropertiesIterator() as $property) {
foreach ($type->getLocalProperties()->getAllPropertiesIterator() as $property) {
$propName = $property->getName();
if ('' === $propName || null === $propName) {
$ref = $property->getRef();
Expand Down
2 changes: 1 addition & 1 deletion src/Version/Definition/Types.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public function isContainedType(string $version, Type $type): bool
if (null === $container) {
return false;
}
foreach ($container->getLocalProperties()->allPropertiesIterator() as $property) {
foreach ($container->getLocalProperties()->getAllPropertiesIterator() as $property) {
if (($ptype = $property->getValueFHIRType()) && $ptype->getFHIRName() === $type->getFHIRName()) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion template/versions/core/classes/class_version_type_map.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

/** @var \DCarbone\PHPFHIR\Version\Definition\Type[] $innerTypes */
$innerTypes = [];
foreach ($containerType->getLocalProperties()->allPropertiesIterator() as $property) {
foreach ($containerType->getLocalProperties()->getAllPropertiesIterator() as $property) {
if ($ptype = $property->getValueFHIRType()) {
$innerTypes[$ptype->getFHIRName()] = $ptype;
}
Expand Down
2 changes: 1 addition & 1 deletion template/versions/types/class_default.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
$typeClassname = $type->getClassName();
$typeKind = $type->getKind();
$parentType = $type->getParentType();
$localProperties = $type->getLocalProperties()->localPropertiesIterator();
$localProperties = $type->getLocalProperties()->getLocalPropertiesIterator();
$classDocumentation = $type->getDocBlockDocumentationFragment(1, true);

ob_start();
Expand Down
2 changes: 1 addition & 1 deletion template/versions/types/header_type.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

if (!isset($skipImports) || !$skipImports) {
$imported = 0;
foreach ($type->getImports() as $import) {
foreach ($type->getImports()->getIterator() as $import) {
if ($import->isRequiresImport()) {
echo $import->getUseStatement();
$imported++;
Expand Down
2 changes: 1 addition & 1 deletion template/versions/types/serialization/json.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/** @var \DCarbone\PHPFHIR\Version $version */
/** @var \DCarbone\PHPFHIR\Version\Definition\Type $type */

$localProperties = $type->getLocalProperties()->localPropertiesIterator();
$localProperties = $type->getLocalProperties()->getLocalPropertiesIterator();
$typeKind = $type->getKind();

ob_start();
Expand Down
2 changes: 1 addition & 1 deletion template/versions/types/serialization/xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
/** @var string $typeClassName */

$xmlName = NameUtils::getTypeXMLElementName($type);
$localProperties = $type->getLocalProperties()->localPropertiesIterator();
$localProperties = $type->getLocalProperties()->getLocalPropertiesIterator();
$properties = $type->getAllPropertiesIterator();

ob_start();
Expand Down
4 changes: 2 additions & 2 deletions template/versions/types/serialization/xml/serialize/body.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@

// this is only used in primitive types. they have no other fields, and I am just going to assume you want it
// as an attribute if marshalled directly.
foreach ($type->getLocalProperties()->localPropertiesOfTypeKinds(includeCollections: false, kinds: null) as $property) : ?>
foreach ($type->getLocalProperties()->getLocalPropertiesOfTypeKinds(includeCollections: false, kinds: null) as $property) : ?>
$xw->writeAttribute(self::FIELD_VALUE, $this->getFormattedValue());
<?php endforeach;

foreach ($type->getLocalProperties()->localPropertiesIterator() as $property) :
foreach ($type->getLocalProperties()->getLocalPropertiesIterator() as $property) :
$pt = $property->getValueFHIRType();
if (null === $pt) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

$config = $version->getConfig();
$namespace = $version->getFullyQualifiedName(false);
$localProperties = $type->getLocalProperties()->localPropertiesIterator();
$localProperties = $type->getLocalProperties()->getLocalPropertiesIterator();

ob_start(); ?>
/**
Expand Down
2 changes: 1 addition & 1 deletion template/versions/types/tests/unit/body_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

$typeClassname = $type->getClassName();
$allProperties = $type->getLocalProperties();
$localProperties = $allProperties->localSortedPropertiesIterator();
$localProperties = $allProperties->getLocalSortedPropertiesIterator();

ob_start();
?>
Expand Down
2 changes: 1 addition & 1 deletion template/versions/types/validation/field_map.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

/** @var \DCarbone\PHPFHIR\Version\Definition\Type $type */

$properties = $type->getLocalProperties()->allSortedPropertiesIterator();
$properties = $type->getLocalProperties()->getAllSortedPropertiesIterator();

ob_start(); ?>
/**
Expand Down
Loading

0 comments on commit ba9140b

Please sign in to comment.