diff --git a/src/Builder/Capacity/Get.php b/src/Builder/Capacity/Get.php new file mode 100644 index 0000000..1a84806 --- /dev/null +++ b/src/Builder/Capacity/Get.php @@ -0,0 +1,70 @@ +endpoint = $endpoint; + + return $this; + } + + public function getNode(): Node + { + if (null === $this->endpoint) { + throw new \Exception(message: 'Please check your capacity builder, you should have selected an endpoint.'); + } + + $options = []; + foreach ($this->options as $key => $value) { + $options[] = new Node\Expr\ArrayItem( + value: compileValueWhenExpression($this->interpreter, $value), + key: compileValueWhenExpression($this->interpreter, $key), + ); + } + + return new Node\Stmt\Expression( + expr: new Node\Expr\Yield_( + value: new Node\Expr\New_( + class: new Node\Name\FullyQualified(name: \Kiboko\Component\Bucket\AcceptanceResultBucket::class), + args: [ + new Node\Arg( + value: new Node\Expr\MethodCall( + var: new Node\Expr\MethodCall( + var: new Node\Expr\PropertyFetch( + var: new Node\Expr\Variable('this'), + name: new Node\Identifier('client') + ), + name: $this->endpoint + ), + name: new Node\Identifier('get'), + args: [ + new Node\Arg(new Node\Expr\Array_($options)), + ] + ), + unpack: true, + ), + ], + ), + ), + ); + } +} diff --git a/src/Capacity/Get.php b/src/Capacity/Get.php new file mode 100644 index 0000000..8b87ab1 --- /dev/null +++ b/src/Capacity/Get.php @@ -0,0 +1,90 @@ +compileSorters($options['sorters']); + unset($options['sorters']); + } + + if (isset($options['languages']) && \is_array($options['languages'])) { + $options['language'] = $this->compileLanguages($options['languages']); + unset($options['languages']); + } + + if (isset($options['columns']) && \is_array($options['columns'])) { + $options['display'] = $this->compileColumns($options['columns']); + unset($options['columns']); + } + + return (new Prestashop\Builder\Capacity\Get($this->interpreter, $options)) + ->withEndpoint(new Node\Identifier(sprintf('get%sApi', ucfirst((string) $config['type'])))) + ; + } + + private function compileSorters(array $sorters): string + { + $results = []; + foreach ($sorters as $key => $value) { + $results[] = $key.'_'.$value; + } + + return sprintf('[%s]', implode(',', $results)); + } + + private function compileLanguages(array $languages): string + { + if (\array_key_exists('from', $languages) && \array_key_exists('to', $languages)) { + return sprintf('[%s,%s]', $languages['from'], $languages['to']); + } + + return sprintf('[%s]', implode('|', $languages)); + } + + private function compileColumns(array $columns): string + { + return sprintf('[%s]', implode(',', $columns)); + } +} diff --git a/src/Factory/Extractor.php b/src/Factory/Extractor.php index ceac502..6b3cb67 100644 --- a/src/Factory/Extractor.php +++ b/src/Factory/Extractor.php @@ -25,6 +25,7 @@ public function __construct(private ExpressionLanguage $interpreter) $this->configuration = new Prestashop\Configuration\Extractor(); $this->capacities = [ new Prestashop\Capacity\All($this->interpreter), + new Prestashop\Capacity\Get($this->interpreter), ]; }