diff --git a/Tests/Unit/TerminalsBrowseRequestTest.php b/Tests/Unit/TerminalsBrowseRequestTest.php index 14b1004..2c6388d 100644 --- a/Tests/Unit/TerminalsBrowseRequestTest.php +++ b/Tests/Unit/TerminalsBrowseRequestTest.php @@ -49,4 +49,60 @@ public function testStartWrongConfig() $this->assertEquals('Something went wrong', $e->getFriendlyMessage()); } } + + /** + * @return void + */ + 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); + } + + /** + * @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); + } } diff --git a/src/Mapper/Manager.php b/src/Mapper/Manager.php index a67f1e6..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 { @@ -71,7 +68,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 +79,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 +90,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)); } } @@ -121,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 5c341e4..d921a9a 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 $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 string $merchantCode + * @return $this + */ + public function setMerchantCode(string $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; } /** @@ -47,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/AbstractRequest.php b/src/Request/AbstractRequest.php index 97033b3..8260d53 100644 --- a/src/Request/AbstractRequest.php +++ b/src/Request/AbstractRequest.php @@ -160,44 +160,31 @@ 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; - foreach ($this->getRequiredParams() as $paramName => $paramDefinition) { - if (false === $this->hasParam($paramName)) { - throw new MissingParamException(sprintf('Missing param "%s"', $paramName)); - } - - 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)); - } + $queryParams = []; + $uri = $this->getUri(); - # Set it in the array - $this->setUri(str_replace("%{$paramName}%", $this->getParam($paramName), $this->getUri())); - } + foreach ($params as $key => $value) { + $placeholder = "%{$key}%"; - $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 (strpos($uri, $placeholder) !== false) { + $uri = str_replace($placeholder, $value, $uri); + } else { + $queryParams[$key] = $value; } } - if (!empty($optionalParams)) { - $this->setUri($this->getUri() . '?' . http_build_query($optionalParams)); + if (!empty($queryParams)) { + $uri .= '?' . http_build_query($queryParams); } + $this->setUri($uri); + return $this; } diff --git a/src/Request/ConfigProvider.php b/src/Request/ConfigProvider.php index e6eb622..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, @@ -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, @@ -206,13 +202,13 @@ protected function getTransactionServicesConfig(): array 'transactionId' => '', ], ], - 'OrderUpdate' => [ + 'OrderUpdate' => [ 'uri' => '/', 'method' => RequestInterface::METHOD_PATCH, 'requiredParams' => [ 'transactionId' => '', ], - ], + ], 'OrderCapture' => [ 'uri' => '', 'requiredParams' => [ @@ -241,7 +237,7 @@ protected function getTransactionServicesConfig(): array 'method' => RequestInterface::METHOD_GET, 'requiredParams' => [ 'transactionId' => '', - ], + ], ], 'OrderStatus' => [ 'uri' => '/transactions/%transactionId%/status', @@ -280,5 +276,4 @@ protected function getTransactionServicesConfig(): array ] ]; } - }