From ac5d1e4cc07ce111d47c9cbe8c731ca3e48a07d4 Mon Sep 17 00:00:00 2001 From: woutse Date: Fri, 4 Apr 2025 12:36:48 +0200 Subject: [PATCH 1/6] Added filter argument for browseterminals --- src/Model/Request/TerminalsBrowseRequest.php | 44 ++++++++++++++++++-- src/Request/AbstractRequest.php | 30 ++++--------- 2 files changed, 48 insertions(+), 26 deletions(-) diff --git a/src/Model/Request/TerminalsBrowseRequest.php b/src/Model/Request/TerminalsBrowseRequest.php index 5c341e4..a9603cb 100644 --- a/src/Model/Request/TerminalsBrowseRequest.php +++ b/src/Model/Request/TerminalsBrowseRequest.php @@ -16,17 +16,55 @@ */ class TerminalsBrowseRequest extends RequestData { - public function __construct() + protected string $merchantCode; + protected string $excludeMerchantCode; + + /** + * @param string|null $merchantCode + * @param string $excludeMerchantCode + */ + public function __construct(string $merchantCode = '', string $excludeMerchantCode = '') { + $this->setMerchantCode($merchantCode ?? ''); + $this->setExcludeMerchantCode($excludeMerchantCode ?? ''); parent::__construct('TerminalsBrowse', '/terminals', RequestInterface::METHOD_GET); } /** - * @return array + * @param $merchantCode + * @return $this + */ + public function setMerchantCode($merchantCode): self + { + $this->merchantCode = $merchantCode; + return $this; + } + + /** + * @param string $excludeMerchantCode + * @return $this + */ + public function setExcludeMerchantCode(string $excludeMerchantCode): self + { + $this->excludeMerchantCode = $excludeMerchantCode; + return $this; + } + + /** + * @return array|null[] */ public function getPathParameters(): array { - return []; + $parameters = []; + + if (!empty($this->merchantCode)) { + $parameters['merchant[eq]'] = $this->merchantCode; + } + if (!empty($this->excludeMerchantCode)) { + $parameters['merchant[neq]'] = $this->excludeMerchantCode; + } + + return $parameters; } /** diff --git a/src/Request/AbstractRequest.php b/src/Request/AbstractRequest.php index 97033b3..684363d 100644 --- a/src/Request/AbstractRequest.php +++ b/src/Request/AbstractRequest.php @@ -170,33 +170,17 @@ public function setParams(array $params): self { $this->params = $params; - foreach ($this->getRequiredParams() as $paramName => $paramDefinition) { - if (false === $this->hasParam($paramName)) { - throw new MissingParamException(sprintf('Missing param "%s"', $paramName)); - } + $additionalArgmuments = ''; - if (true === is_string($paramDefinition) && '' !== $paramDefinition && 1 !== preg_match("/^{$paramDefinition}$/", $this->getParam($paramName))) { - throw new InvalidArgumentException(sprintf('Required param %s is not valid. It must match "%s"', $paramName, $paramDefinition)); + foreach ($params as $key => $value) { + if (strpos($this->getUri(), '%' . $key . '%') !== false) { + $this->setUri(str_replace("%{$key}%", $value, $this->getUri())); + } else { + $additionalArgmuments .= $key . '=' . $value . '&'; } - - # Set it in the array - $this->setUri(str_replace("%{$paramName}%", $this->getParam($paramName), $this->getUri())); } - $optionalParams = []; - foreach ($this->getOptionalParams() as $paramName => $paramDefinition) { - # If optional paramater is provided... - if (isset($params[$paramName])) { - if (true === is_string($paramDefinition) && '' !== $paramDefinition && 1 !== preg_match("/^{$paramDefinition}$/", $this->getParam($paramName))) { - throw new InvalidArgumentException(sprintf('Optional param %s is not valid. It must match "%s"', $paramName, $paramDefinition)); - } - $optionalParams[$paramName] = $this->params[$paramName]; - } - } - - if (!empty($optionalParams)) { - $this->setUri($this->getUri() . '?' . http_build_query($optionalParams)); - } + $this->setUri($this->getUri() . '?' . substr($additionalArgmuments, 0, -1)); return $this; } From 33fe48ddb0685e5976a11fa1876f84ac53f8392b Mon Sep 17 00:00:00 2001 From: woutse Date: Fri, 4 Apr 2025 13:29:45 +0200 Subject: [PATCH 2/6] Added filter argument for browseterminals --- src/Mapper/Manager.php | 20 ++++---------------- src/Request/ConfigProvider.php | 6 +----- 2 files changed, 5 insertions(+), 21 deletions(-) diff --git a/src/Mapper/Manager.php b/src/Mapper/Manager.php index a67f1e6..00f0a9c 100644 --- a/src/Mapper/Manager.php +++ b/src/Mapper/Manager.php @@ -71,7 +71,7 @@ public function configure(array $config): ServiceManager $mappingConfig[$mapper] = $map; unset($mappingConfig[$mapperAlias]); - // determine the necessary managers + # Determine the necessary managers preg_match_all('/((?:^|[A-Z])[a-z]+)/', Misc::getClassNameByFQN($mapper), $matches); if (2 > count($matches[1])) { throw new ServiceNotFoundException( @@ -82,7 +82,7 @@ public function configure(array $config): ServiceManager ); } - // determine the name of the source and target + # Determine the name of the source and target $sourceManagerName = lcfirst(current($matches[1])); next($matches[1]); $targetManagerName = lcfirst(current($matches[1])); @@ -93,22 +93,10 @@ public function configure(array $config): ServiceManager # Check the map foreach ($map as $source => $target) { if ($sourceManager->has($source) === false) { - throw new MapperSourceServiceNotFoundException( - sprintf( - 'Mapping source service with name "%s" not found in %s', - $source, - $sourceManagerName - ) - ); + throw new MapperSourceServiceNotFoundException(sprintf('Mapping source service with name "%s" not found in %s', $source, $sourceManagerName)); } if ($targetManager->has($target) === false) { - throw new MapperTargetServiceNotFoundException( - sprintf( - 'Mapping target service with name "%s" not found in %s', - $target, - $targetManagerName - ) - ); + throw new MapperTargetServiceNotFoundException(sprintf('Mapping target service with name "%s" not found in %s', $target, $targetManagerName)); } } diff --git a/src/Request/ConfigProvider.php b/src/Request/ConfigProvider.php index e6eb622..a82a616 100644 --- a/src/Request/ConfigProvider.php +++ b/src/Request/ConfigProvider.php @@ -108,11 +108,7 @@ protected function getPinServicesConfig(): array 'method' => RequestInterface::METHOD_GET, 'requiredParams' => ['terminalCode' => ''], ], - 'TerminalsBrowse' => [ - 'uri' => '/terminals', - 'method' => RequestInterface::METHOD_GET, - 'requiredParams' => [], - ], + 'TerminalsBrowse' => [], 'ConfirmTerminalTransaction' => [ 'uri' => '/pin/%terminalTransactionId%/confirm', 'method' => RequestInterface::METHOD_PATCH, From 252df5f7e299f54af08229da613b720e52fe5f87 Mon Sep 17 00:00:00 2001 From: woutse Date: Fri, 4 Apr 2025 15:28:20 +0200 Subject: [PATCH 3/6] Added filter argument for browseterminals --- Tests/Unit/TerminalsBrowseRequestTest.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Tests/Unit/TerminalsBrowseRequestTest.php b/Tests/Unit/TerminalsBrowseRequestTest.php index 14b1004..5976152 100644 --- a/Tests/Unit/TerminalsBrowseRequestTest.php +++ b/Tests/Unit/TerminalsBrowseRequestTest.php @@ -49,4 +49,17 @@ public function testStartWrongConfig() $this->assertEquals('Something went wrong', $e->getFriendlyMessage()); } } + + public function testPathParametersWithMerchantCode() + { + $request = new TerminalsBrowseRequest(); + $request->setMerchantCode('M-1111-2222'); + + $params = $request->getPathParameters(); + + $this->assertArrayHasKey('merchant[eq]', $params); + $this->assertEquals('M-1111-2222', $params['merchant[eq]']); + $this->assertArrayNotHasKey('merchant[neq]', $params); + } + } From b2fac8b571ffb5cba29cca6847d40ea8288a05ce Mon Sep 17 00:00:00 2001 From: woutse Date: Fri, 4 Apr 2025 15:42:57 +0200 Subject: [PATCH 4/6] Updates --- Tests/Unit/TerminalsBrowseRequestTest.php | 44 +++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/Tests/Unit/TerminalsBrowseRequestTest.php b/Tests/Unit/TerminalsBrowseRequestTest.php index 5976152..a8fef20 100644 --- a/Tests/Unit/TerminalsBrowseRequestTest.php +++ b/Tests/Unit/TerminalsBrowseRequestTest.php @@ -50,6 +50,9 @@ public function testStartWrongConfig() } } + /** + * @return void + */ public function testPathParametersWithMerchantCode() { $request = new TerminalsBrowseRequest(); @@ -62,4 +65,45 @@ public function testPathParametersWithMerchantCode() $this->assertArrayNotHasKey('merchant[neq]', $params); } + /** + * @return void + */ + public function testPathParametersWithExcludeMerchantCode() + { + $request = new TerminalsBrowseRequest(); + $request->setExcludeMerchantCode('M-9999-8888'); + + $params = $request->getPathParameters(); + + $this->assertArrayHasKey('merchant[neq]', $params); + $this->assertEquals('M-9999-8888', $params['merchant[neq]']); + $this->assertArrayNotHasKey('merchant[eq]', $params); + } + + /** + * @return void + */ + public function testPathParametersWithBoth() + { + $request = new TerminalsBrowseRequest(); + $request + ->setMerchantCode('M-1111-2222') + ->setExcludeMerchantCode('M-9999-8888'); + + $params = $request->getPathParameters(); + + $this->assertEquals('M-1111-2222', $params['merchant[eq]']); + $this->assertEquals('M-9999-8888', $params['merchant[neq]']); + } + + /** + * @return void + */ + public function testPathParametersEmpty() + { + $request = new TerminalsBrowseRequest(); + $params = $request->getPathParameters(); + $this->assertEmpty($params); + } + } From f081eb23a26c5ae3fc362ac94a7e0263eaaec000 Mon Sep 17 00:00:00 2001 From: woutse Date: Fri, 4 Apr 2025 15:57:59 +0200 Subject: [PATCH 5/6] Updates --- Tests/Unit/TerminalsBrowseRequestTest.php | 1 - src/Mapper/Manager.php | 16 +++++++--------- src/Model/Request/TerminalsBrowseRequest.php | 8 ++++---- src/Request/ConfigProvider.php | 19 +++++++++---------- 4 files changed, 20 insertions(+), 24 deletions(-) diff --git a/Tests/Unit/TerminalsBrowseRequestTest.php b/Tests/Unit/TerminalsBrowseRequestTest.php index a8fef20..2c6388d 100644 --- a/Tests/Unit/TerminalsBrowseRequestTest.php +++ b/Tests/Unit/TerminalsBrowseRequestTest.php @@ -105,5 +105,4 @@ public function testPathParametersEmpty() $params = $request->getPathParameters(); $this->assertEmpty($params); } - } diff --git a/src/Mapper/Manager.php b/src/Mapper/Manager.php index 00f0a9c..1609b50 100644 --- a/src/Mapper/Manager.php +++ b/src/Mapper/Manager.php @@ -34,15 +34,12 @@ class Manager extends AbstractPluginManager */ protected $mapping = []; + /** - * @inheritDoc - * - * @throws ServiceNotCreatedException - * @throws ServiceNotFoundException - * @throws MapperSourceServiceNotFoundException - * @throws MapperTargetServiceNotFoundException - * - * @SuppressWarnings(PHPMD.StaticAccess) + * @param array $config + * @return ServiceManager + * @throws \Psr\Container\ContainerExceptionInterface + * @throws \Psr\Container\NotFoundExceptionInterface */ public function configure(array $config): ServiceManager { @@ -109,7 +106,8 @@ public function configure(array $config): ServiceManager } /** - * @inheritDoc + * @param array $config + * @return void */ protected function validateOverrides(array $config): void { diff --git a/src/Model/Request/TerminalsBrowseRequest.php b/src/Model/Request/TerminalsBrowseRequest.php index a9603cb..fc5692b 100644 --- a/src/Model/Request/TerminalsBrowseRequest.php +++ b/src/Model/Request/TerminalsBrowseRequest.php @@ -20,7 +20,7 @@ class TerminalsBrowseRequest extends RequestData protected string $excludeMerchantCode; /** - * @param string|null $merchantCode + * @param string $merchantCode * @param string $excludeMerchantCode */ public function __construct(string $merchantCode = '', string $excludeMerchantCode = '') @@ -31,10 +31,10 @@ public function __construct(string $merchantCode = '', string $excludeMerchantCo } /** - * @param $merchantCode + * @param string $merchantCode * @return $this */ - public function setMerchantCode($merchantCode): self + public function setMerchantCode(string $merchantCode): self { $this->merchantCode = $merchantCode; return $this; @@ -85,4 +85,4 @@ public function start(): Terminals $this->config->setVersion(2); return parent::start(); } -} \ No newline at end of file +} diff --git a/src/Request/ConfigProvider.php b/src/Request/ConfigProvider.php index a82a616..a5e82d3 100644 --- a/src/Request/ConfigProvider.php +++ b/src/Request/ConfigProvider.php @@ -17,7 +17,7 @@ class ConfigProvider implements ConfigProviderInterface { /** - * @inheritDoc + * @return array */ public function __invoke(): array { @@ -38,7 +38,7 @@ public function __invoke(): array } /** - * @inheritDoc + * @return array */ public function getDependencyConfig(): array { @@ -65,10 +65,10 @@ public function getRequestConfig(): array DebugAwareInitializer::class, ], 'services' => array_merge( - $this->getIsPayServicesConfig(), - $this->getPinServicesConfig(), - $this->getServiceServicesConfig(), - $this->getTransactionServicesConfig(), + $this->getIsPayServicesConfig(), + $this->getPinServicesConfig(), + $this->getServiceServicesConfig(), + $this->getTransactionServicesConfig(), ), 'factories' => [ Request::class => Factory::class, @@ -202,13 +202,13 @@ protected function getTransactionServicesConfig(): array 'transactionId' => '', ], ], - 'OrderUpdate' => [ + 'OrderUpdate' => [ 'uri' => '/', 'method' => RequestInterface::METHOD_PATCH, 'requiredParams' => [ 'transactionId' => '', ], - ], + ], 'OrderCapture' => [ 'uri' => '', 'requiredParams' => [ @@ -237,7 +237,7 @@ protected function getTransactionServicesConfig(): array 'method' => RequestInterface::METHOD_GET, 'requiredParams' => [ 'transactionId' => '', - ], + ], ], 'OrderStatus' => [ 'uri' => '/transactions/%transactionId%/status', @@ -276,5 +276,4 @@ protected function getTransactionServicesConfig(): array ] ]; } - } From 0e818a4162843c6ce7a5261849701fc7b21d84ec Mon Sep 17 00:00:00 2001 From: woutse Date: Mon, 7 Apr 2025 16:31:46 +0200 Subject: [PATCH 6/6] Code polish --- src/Model/Request/TerminalsBrowseRequest.php | 4 ++-- src/Request/AbstractRequest.php | 23 +++++++++++--------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/Model/Request/TerminalsBrowseRequest.php b/src/Model/Request/TerminalsBrowseRequest.php index fc5692b..d921a9a 100644 --- a/src/Model/Request/TerminalsBrowseRequest.php +++ b/src/Model/Request/TerminalsBrowseRequest.php @@ -25,8 +25,8 @@ class TerminalsBrowseRequest extends RequestData */ public function __construct(string $merchantCode = '', string $excludeMerchantCode = '') { - $this->setMerchantCode($merchantCode ?? ''); - $this->setExcludeMerchantCode($excludeMerchantCode ?? ''); + $this->setMerchantCode($merchantCode); + $this->setExcludeMerchantCode($excludeMerchantCode); parent::__construct('TerminalsBrowse', '/terminals', RequestInterface::METHOD_GET); } diff --git a/src/Request/AbstractRequest.php b/src/Request/AbstractRequest.php index 684363d..8260d53 100644 --- a/src/Request/AbstractRequest.php +++ b/src/Request/AbstractRequest.php @@ -160,27 +160,30 @@ public function hasParam($name): bool /** * @param array $params - * - * @throws MissingParamException - * @throws InvalidArgumentException - * - * @return static + * @return $this */ public function setParams(array $params): self { $this->params = $params; - $additionalArgmuments = ''; + $queryParams = []; + $uri = $this->getUri(); foreach ($params as $key => $value) { - if (strpos($this->getUri(), '%' . $key . '%') !== false) { - $this->setUri(str_replace("%{$key}%", $value, $this->getUri())); + $placeholder = "%{$key}%"; + + if (strpos($uri, $placeholder) !== false) { + $uri = str_replace($placeholder, $value, $uri); } else { - $additionalArgmuments .= $key . '=' . $value . '&'; + $queryParams[$key] = $value; } } - $this->setUri($this->getUri() . '?' . substr($additionalArgmuments, 0, -1)); + if (!empty($queryParams)) { + $uri .= '?' . http_build_query($queryParams); + } + + $this->setUri($uri); return $this; }