Skip to content

Commit

Permalink
Merge pull request #60 from php-etl/feature/add-category-enriched-att…
Browse files Browse the repository at this point in the history
…ributes-parameter

Add new parameter for endpoint category
  • Loading branch information
gplanchat authored May 28, 2024
2 parents 1c5ef48 + b85c8b0 commit 8bd9897
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 29 deletions.
39 changes: 38 additions & 1 deletion src/Builder/Capacity/Extractor/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ final class All implements Builder
private ?Node\Expr $code = null;
private ?Node\Expr $referenceEntity = null;
private ?Node\Expr $referenceEntityAttributeCode = null;
private ?Node\Expr $withEnrichedAttributes = null;

public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self
{
Expand Down Expand Up @@ -51,6 +52,13 @@ public function withReferenceEntityAttributeOption(?Node\Expr $referenceEntityAt
return $this;
}

public function withEnrichedAttributes(?Node\Expr $withEnrichedAttributes): self
{
$this->withEnrichedAttributes = $withEnrichedAttributes;

return $this;
}

public function getNode(): Node
{
if (null === $this->endpoint) {
Expand Down Expand Up @@ -109,9 +117,21 @@ private function compileArguments(): array
$args = [];

if (null !== $this->search) {

Check warning on line 119 in src/Builder/Capacity/Extractor/All.php

View workflow job for this annotation

GitHub Actions / infection

Escaped Mutant for Mutator "NotIdentical": --- Original +++ New @@ @@ private function compileArguments() : array { $args = []; - if (null !== $this->search) { + if (null === $this->search) { $items = $this->compileSearch(); if (null !== $this->withEnrichedAttributes) { $items = array_merge($this->compileSearch(), [new Node\Expr\ArrayItem($this->withEnrichedAttributes, new Node\Scalar\String_('with_enriched_attributes'))]);

Check warning on line 119 in src/Builder/Capacity/Extractor/All.php

View workflow job for this annotation

GitHub Actions / infection

Escaped Mutant for Mutator "NotIdentical": --- Original +++ New @@ @@ private function compileArguments() : array { $args = []; - if (null !== $this->search) { + if (null === $this->search) { $items = $this->compileSearch(); if (null !== $this->withEnrichedAttributes) { $items = array_merge($this->compileSearch(), [new Node\Expr\ArrayItem($this->withEnrichedAttributes, new Node\Scalar\String_('with_enriched_attributes'))]);
$items = $this->compileSearch();
if (null !== $this->withEnrichedAttributes) {
$items = array_merge(
$this->compileSearch(),
[
new Node\Expr\ArrayItem(
$this->withEnrichedAttributes,
new Node\Scalar\String_('with_enriched_attributes'),
),
]
);
}
$args[] = new Node\Arg(
value: new Node\Expr\Array_(
items: $this->compileSearch(),
items: $items,
attributes: [
'kind' => Node\Expr\Array_::KIND_SHORT,
]
Expand Down Expand Up @@ -141,6 +161,23 @@ private function compileArguments(): array
);
}

if (null !== $this->withEnrichedAttributes && null === $this->search) {

Check warning on line 164 in src/Builder/Capacity/Extractor/All.php

View workflow job for this annotation

GitHub Actions / infection

Escaped Mutant for Mutator "Identical": --- Original +++ New @@ @@ if (null !== $this->referenceEntityAttributeCode) { $args[] = new Node\Arg(value: $this->referenceEntityAttributeCode, name: new Node\Identifier('attributeCode')); } - if (null !== $this->withEnrichedAttributes && null === $this->search) { + if (null !== $this->withEnrichedAttributes && null !== $this->search) { $args[] = new Node\Arg(value: new Node\Expr\Array_(items: [new Node\Expr\ArrayItem($this->withEnrichedAttributes, new Node\Scalar\String_('with_enriched_attributes'))], attributes: ['kind' => Node\Expr\Array_::KIND_SHORT]), name: new Node\Identifier('queryParameters')); } return $args; } }

Check warning on line 164 in src/Builder/Capacity/Extractor/All.php

View workflow job for this annotation

GitHub Actions / infection

Escaped Mutant for Mutator "Identical": --- Original +++ New @@ @@ if (null !== $this->referenceEntityAttributeCode) { $args[] = new Node\Arg(value: $this->referenceEntityAttributeCode, name: new Node\Identifier('attributeCode')); } - if (null !== $this->withEnrichedAttributes && null === $this->search) { + if (null !== $this->withEnrichedAttributes && null !== $this->search) { $args[] = new Node\Arg(value: new Node\Expr\Array_(items: [new Node\Expr\ArrayItem($this->withEnrichedAttributes, new Node\Scalar\String_('with_enriched_attributes'))], attributes: ['kind' => Node\Expr\Array_::KIND_SHORT]), name: new Node\Identifier('queryParameters')); } return $args; } }
$args[] = new Node\Arg(
value: new Node\Expr\Array_(
items: [
new Node\Expr\ArrayItem(
$this->withEnrichedAttributes,
new Node\Scalar\String_('with_enriched_attributes'),
),
],
attributes: [
'kind' => Node\Expr\Array_::KIND_SHORT,
]
),
name: new Node\Identifier('queryParameters'),
);
}

return $args;

Check warning on line 181 in src/Builder/Capacity/Extractor/All.php

View workflow job for this annotation

GitHub Actions / infection

Escaped Mutant for Mutator "ArrayOneItem": --- Original +++ New @@ @@ if (null !== $this->withEnrichedAttributes && null === $this->search) { $args[] = new Node\Arg(value: new Node\Expr\Array_(items: [new Node\Expr\ArrayItem($this->withEnrichedAttributes, new Node\Scalar\String_('with_enriched_attributes'))], attributes: ['kind' => Node\Expr\Array_::KIND_SHORT]), name: new Node\Identifier('queryParameters')); } - return $args; + return count($args) > 1 ? array_slice($args, 0, 1, true) : $args; } }

Check warning on line 181 in src/Builder/Capacity/Extractor/All.php

View workflow job for this annotation

GitHub Actions / infection

Escaped Mutant for Mutator "ArrayOneItem": --- Original +++ New @@ @@ if (null !== $this->withEnrichedAttributes && null === $this->search) { $args[] = new Node\Arg(value: new Node\Expr\Array_(items: [new Node\Expr\ArrayItem($this->withEnrichedAttributes, new Node\Scalar\String_('with_enriched_attributes'))], attributes: ['kind' => Node\Expr\Array_::KIND_SHORT]), name: new Node\Identifier('queryParameters')); } - return $args; + return count($args) > 1 ? array_slice($args, 0, 1, true) : $args; } }
}
}
4 changes: 0 additions & 4 deletions src/Builder/Capacity/Extractor/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ final class Get implements Builder
private Node\Expr|Node\Identifier|null $endpoint = null;
private ?Node\Expr $identifier = null;

public function __construct()
{
}

public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self
{
$this->endpoint = $endpoint;
Expand Down
4 changes: 0 additions & 4 deletions src/Builder/Capacity/Extractor/ListPerPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ final class ListPerPage implements Builder
private ?Node\Expr $search = null;
private ?Node\Expr $code = null;

public function __construct()
{
}

public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self
{
$this->endpoint = $endpoint;
Expand Down
4 changes: 0 additions & 4 deletions src/Builder/Capacity/Lookup/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ final class All implements Builder
private ?Node\Expr $code = null;
private string $type = '';

public function __construct()
{
}

public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self
{
$this->endpoint = $endpoint;
Expand Down
4 changes: 0 additions & 4 deletions src/Builder/Capacity/Lookup/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ final class Download implements Builder
private Node\Expr|Node\Identifier|null $endpoint = null;
private ?Node\Expr $file = null;

public function __construct()
{
}

public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self
{
$this->endpoint = $endpoint;
Expand Down
4 changes: 0 additions & 4 deletions src/Builder/Capacity/Lookup/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ final class Get implements Builder
private ?Node\Expr $code = null;
private ?string $type = '';

public function __construct()
{
}

public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self
{
$this->endpoint = $endpoint;
Expand Down
4 changes: 0 additions & 4 deletions src/Builder/Capacity/Lookup/ListPerPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ final class ListPerPage implements Builder
private ?Node\Expr $search = null;
private ?Node\Expr $code = null;

public function __construct()
{
}

public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self
{
$this->endpoint = $endpoint;
Expand Down
4 changes: 0 additions & 4 deletions src/Builder/ConditionalLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ final class ConditionalLookup implements StepBuilderInterface
private iterable $alternatives = [];
private ?Node\Expr $client = null;

public function __construct()
{
}

public function withClient(Node\Expr $client): self
{
$this->client = $client;
Expand Down
4 changes: 4 additions & 0 deletions src/Capacity/Extractor/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ public function getBuilder(array $config): Builder
$builder->withSearch($this->compileFilters(...$config['search']));
}

if (isset($config['with_enriched_attributes']) && 'category' === $config['type']) {
$builder->withEnrichedAttributes(compileValueWhenExpression($this->interpreter, $config['with_enriched_attributes']));
}

if (\in_array($config['type'], ['attributeOption', 'assetManager']) && \array_key_exists('code', $config)) {
$builder->withCode(compileValueWhenExpression($this->interpreter, $config['code']));
}
Expand Down
14 changes: 14 additions & 0 deletions src/Configuration/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,14 @@ public function getConfigTreeBuilder(): Config\Definition\Builder\TreeBuilder
&& \in_array($data['type'], ['referenceEntityAttributeOption'], true))
->thenInvalid('The reference_entity option should be used with the "referenceEntityAttributeOption" endpoint.')
->end()
->validate()
->ifTrue(fn ($data) => \array_key_exists('with_enriched_attributes', $data) && \array_key_exists('type', $data) && !\in_array($data['type'], ['category'], true))
->thenInvalid('The with_enriched_attributes option should only be used with the "category" endpoint.')
->end()
->validate()
->ifTrue(fn ($data) => \array_key_exists('with_enriched_attributes', $data) && \array_key_exists('method', $data) && 'all' !== $data['method'])
->thenInvalid('The with_enriched_attributes option should only be used with the "all" method.')
->end()
->children()
->scalarNode('type')
->isRequired()
Expand Down Expand Up @@ -238,6 +246,12 @@ public function getConfigTreeBuilder(): Config\Definition\Builder\TreeBuilder
->then(asExpression())
->end()
->end()
->booleanNode('with_enriched_attributes')
->validate()
->ifTrue(isExpression())
->then(asExpression())
->end()
->end()
->append((new Search())->getConfigTreeBuilder()->getRootNode())
->end()
;
Expand Down
14 changes: 14 additions & 0 deletions tests/functional/Configuration/ExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ public static function validDataProvider(): iterable
'search' => [],
],
];
yield [
'config' => [
'method' => 'all',
'type' => 'category',
'search' => [],
'with_enriched_attributes' => true,
],
'expected' => [
'method' => 'all',
'type' => 'category',
'search' => [],
'with_enriched_attributes' => true,
],
];
}

#[\PHPUnit\Framework\Attributes\DataProvider('validDataProvider')]
Expand Down

0 comments on commit 8bd9897

Please sign in to comment.