Skip to content

Commit

Permalink
Merge pull request #41 from TomHAnderson/feature/continued-9.0.0
Browse files Browse the repository at this point in the history
Continued 9.0.0
  • Loading branch information
TomHAnderson committed Dec 13, 2023
2 parents 0cd36cf + 0a5e7fd commit bd0d3d0
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 85 deletions.
39 changes: 17 additions & 22 deletions src/Hydrator/HydratorFactory.php
Expand Up @@ -14,7 +14,6 @@
use Laminas\Hydrator\Filter;
use Laminas\Hydrator\NamingStrategy\NamingStrategyEnabledInterface;
use Laminas\Hydrator\NamingStrategy\NamingStrategyInterface;
use Laminas\Hydrator\Strategy\StrategyEnabledInterface;
use Laminas\Hydrator\Strategy\StrategyInterface;

use function assert;
Expand Down Expand Up @@ -52,30 +51,26 @@ public function get(string $id): mixed
$hydrator = new DoctrineObject($this->entityManager, $config['byValue']);

// Create field strategy and assign to hydrator
if ($hydrator instanceof StrategyEnabledInterface) {
foreach ($config['fields'] as $fieldName => $fieldMetadata) {
assert(
in_array(StrategyInterface::class, class_implements($fieldMetadata['hydratorStrategy'])),
'Strategy must implement ' . StrategyInterface::class,
);

$hydrator->addStrategy($fieldName, $this->get($fieldMetadata['hydratorStrategy']));
}
foreach ($config['fields'] as $fieldName => $fieldMetadata) {
assert(
in_array(StrategyInterface::class, class_implements($fieldMetadata['hydratorStrategy'])),
'Strategy must implement ' . StrategyInterface::class,
);

$hydrator->addStrategy($fieldName, $this->get($fieldMetadata['hydratorStrategy']));
}

// Create filters and assign to hydrator
if ($hydrator instanceof Filter\FilterEnabledInterface) {
foreach ($config['hydratorFilters'] as $name => $filterConfig) {
// Default filters to AND
$condition = $filterConfig['condition'] ?? Filter\FilterComposite::CONDITION_AND;
$filterClass = $filterConfig['filter'];
assert(
in_array(Filter\FilterInterface::class, class_implements($filterClass)),
'Filter must implement ' . StrategyInterface::class,
);

$hydrator->addFilter($name, $this->get($filterClass), $condition);
}
foreach ($config['hydratorFilters'] as $name => $filterConfig) {
// Default filters to AND
$condition = $filterConfig['condition'] ?? Filter\FilterComposite::CONDITION_AND;
$filterClass = $filterConfig['filter'];
assert(
in_array(Filter\FilterInterface::class, class_implements($filterClass)),
'Filter must implement ' . StrategyInterface::class,
);

$hydrator->addFilter($name, $this->get($filterClass), $condition);
}

// Create naming strategy and assign to hydrator
Expand Down
16 changes: 6 additions & 10 deletions src/Resolve/ResolveCollectionFactory.php
Expand Up @@ -125,16 +125,12 @@ protected function buildPagination(

// Pagination
foreach ($pagination as $field => $value) {
switch ($field) {
case 'after':
$paginationFields[$field] = (int) base64_decode($value, true) + 1;
break;
case 'before':
$paginationFields[$field] = (int) base64_decode($value, true);
break;
default:
$paginationFields[$field] = $value;
break;
if ($field === 'after') {
$paginationFields[$field] = (int) base64_decode($value, true) + 1;
} elseif ($field === 'before') {
$paginationFields[$field] = (int) base64_decode($value, true);
} else {
$paginationFields[$field] = $value;
}
}

Expand Down
16 changes: 6 additions & 10 deletions src/Resolve/ResolveEntityFactory.php
Expand Up @@ -74,16 +74,12 @@ public function buildPagination(

if (isset($resolve['args']['pagination'])) {
foreach ($resolve['args']['pagination'] as $field => $value) {
switch ($field) {
case 'after':
$paginationFields[$field] = (int) base64_decode($value, true) + 1;
break;
case 'before':
$paginationFields[$field] = (int) base64_decode($value, true);
break;
default:
$paginationFields[$field] = $value;
break;
if ($field === 'after') {
$paginationFields[$field] = (int) base64_decode($value, true) + 1;
} elseif ($field === 'before') {
$paginationFields[$field] = (int) base64_decode($value, true);
} else {
$paginationFields[$field] = $value;
}
}
}
Expand Down
85 changes: 42 additions & 43 deletions src/Type/Entity.php
Expand Up @@ -178,50 +178,49 @@ protected function addAssociations(array &$fields): void
}

$associationMetadata = $classMetadata->getAssociationMapping($associationName);

switch ($associationMetadata['type']) {
case ClassMetadataInfo::ONE_TO_ONE:
case ClassMetadataInfo::MANY_TO_ONE:
case ClassMetadataInfo::TO_ONE:
$targetEntity = $associationMetadata['targetEntity'];
$fields[$associationName] = function () use ($targetEntity) {
$entity = $this->typeManager->build(self::class, $targetEntity);

return [
'type' => $entity->getGraphQLType(),
'description' => $entity->getDescription(),
];
};
break;
case ClassMetadataInfo::ONE_TO_MANY:
case ClassMetadataInfo::MANY_TO_MANY:
case ClassMetadataInfo::TO_MANY:
$targetEntity = $associationMetadata['targetEntity'];
$fields[$associationName] = function () use ($targetEntity, $associationName) {
$entity = $this->typeManager->build(self::class, $targetEntity);
$shortName = $this->getTypeName() . '_' . $associationName;

return [
'type' => $this->typeManager->build(
Connection::class,
$shortName . '_Connection',
$entity->getGraphQLType(),
),
'args' => [
'filter' => $this->filterFactory->get(
$entity,
$this,
$associationName,
$this->metadata['fields'][$associationName],
),
'pagination' => $this->typeManager->get('pagination'),
],
'description' => $this->metadata['fields'][$associationName]['description'],
'resolve' => $this->collectionFactory->get($entity),
];
};
break;
if (
in_array($associationMetadata['type'], [
ClassMetadataInfo::ONE_TO_ONE,
ClassMetadataInfo::MANY_TO_ONE,
ClassMetadataInfo::TO_ONE,
])
) {
$targetEntity = $associationMetadata['targetEntity'];
$fields[$associationName] = function () use ($targetEntity) {
$entity = $this->typeManager->build(self::class, $targetEntity);

return [
'type' => $entity->getGraphQLType(),
'description' => $entity->getDescription(),
];
};
}

// Collections
$targetEntity = $associationMetadata['targetEntity'];
$fields[$associationName] = function () use ($targetEntity, $associationName) {
$entity = $this->typeManager->build(self::class, $targetEntity);
$shortName = $this->getTypeName() . '_' . $associationName;

return [
'type' => $this->typeManager->build(
Connection::class,
$shortName . '_Connection',
$entity->getGraphQLType(),
),
'args' => [
'filter' => $this->filterFactory->get(
$entity,
$this,
$associationName,
$this->metadata['fields'][$associationName],
),
'pagination' => $this->typeManager->get('pagination'),
],
'description' => $this->metadata['fields'][$associationName]['description'],
'resolve' => $this->collectionFactory->get($entity),
];
};
}
}
}

0 comments on commit bd0d3d0

Please sign in to comment.