diff --git a/src/Common/AbstractGateway.php b/src/Common/AbstractGateway.php index adc91bff..17077647 100755 --- a/src/Common/AbstractGateway.php +++ b/src/Common/AbstractGateway.php @@ -43,7 +43,10 @@ */ abstract class AbstractGateway implements GatewayInterface { - use ParametersTrait; + /** + * @var \Symfony\Component\HttpFoundation\ParameterBag + */ + protected $parameters; /** * @var ClientInterface @@ -110,6 +113,35 @@ public function getDefaultParameters() return array(); } + /** + * @return array + */ + public function getParameters() + { + return $this->parameters->all(); + } + + /** + * @param string $key + * @return mixed + */ + public function getParameter($key) + { + return $this->parameters->get($key); + } + + /** + * @param string $key + * @param mixed $value + * @return $this + */ + public function setParameter($key, $value) + { + $this->parameters->set($key, $value); + + return $this; + } + /** * @return boolean */ diff --git a/src/Common/CreditCard.php b/src/Common/CreditCard.php index c92285ba..88def908 100644 --- a/src/Common/CreditCard.php +++ b/src/Common/CreditCard.php @@ -93,8 +93,6 @@ */ class CreditCard { - use ParametersTrait; - const BRAND_VISA = 'visa'; const BRAND_MASTERCARD = 'mastercard'; const BRAND_DISCOVER = 'discover'; @@ -135,6 +133,13 @@ class CreditCard self::BRAND_LASER => '/^(6304|6706|6709|6771(?!89))\d{8}(\d{4}|\d{6,7})?$/', ); + /** + * Internal storage of all of the card parameters. + * + * @var \Symfony\Component\HttpFoundation\ParameterBag + */ + protected $parameters; + /** * Create a new CreditCard object using the specified parameters * @@ -199,6 +204,40 @@ public function initialize(array $parameters = null) return $this; } + /** + * Get all parameters. + * + * @return array An associative array of parameters. + */ + public function getParameters() + { + return $this->parameters->all(); + } + + /** + * Get one parameter. + * + * @return mixed A single parameter value. + */ + protected function getParameter($key) + { + return $this->parameters->get($key); + } + + /** + * Set one parameter. + * + * @param string $key Parameter key + * @param mixed $value Parameter value + * @return $this + */ + protected function setParameter($key, $value) + { + $this->parameters->set($key, $value); + + return $this; + } + /** * Set the credit card year. * @@ -229,9 +268,8 @@ protected function setYearParameter($key, $value) * Generally if you want to validate the credit card yourself with custom error * messages, you should use your framework's validation library, not this method. * - * @return void - * @throws Exception\InvalidRequestException * @throws InvalidCreditCardException + * @return void */ public function validate() { @@ -259,6 +297,7 @@ public function validate() throw new InvalidCreditCardException('Card number should have 12 to 19 digits'); } } + /** * Get Card Title. * diff --git a/src/Common/Item.php b/src/Common/Item.php index 7279abc4..8eaa8957 100644 --- a/src/Common/Item.php +++ b/src/Common/Item.php @@ -15,7 +15,10 @@ */ class Item implements ItemInterface { - use ParametersTrait; + /** + * @var \Symfony\Component\HttpFoundation\ParameterBag + */ + protected $parameters; /** * Create a new item with the specified parameters @@ -42,6 +45,26 @@ public function initialize(array $parameters = null) return $this; } + /** + * @return array + */ + public function getParameters() + { + return $this->parameters->all(); + } + + protected function getParameter($key) + { + return $this->parameters->get($key); + } + + protected function setParameter($key, $value) + { + $this->parameters->set($key, $value); + + return $this; + } + /** * {@inheritDoc} */ diff --git a/src/Common/Message/AbstractRequest.php b/src/Common/Message/AbstractRequest.php index 004a7757..2e441b15 100644 --- a/src/Common/Message/AbstractRequest.php +++ b/src/Common/Message/AbstractRequest.php @@ -18,7 +18,6 @@ use Omnipay\Common\Http\Client; use Omnipay\Common\Http\ClientInterface; use Omnipay\Common\ItemBag; -use Omnipay\Common\ParametersTrait; use Symfony\Component\HttpFoundation\ParameterBag; use Symfony\Component\HttpFoundation\Request as HttpRequest; @@ -65,9 +64,12 @@ */ abstract class AbstractRequest implements RequestInterface { - use ParametersTrait { - setParameter as traitSetParameter; - } + /** + * The request parameters + * + * @var \Symfony\Component\HttpFoundation\ParameterBag + */ + protected $parameters; /** * The request client. @@ -141,6 +143,27 @@ public function initialize(array $parameters = array()) return $this; } + /** + * Get all parameters as an associative array. + * + * @return array + */ + public function getParameters() + { + return $this->parameters->all(); + } + + /** + * Get a single parameter. + * + * @param string $key The parameter key + * @return mixed + */ + protected function getParameter($key) + { + return $this->parameters->get($key); + } + /** * Set a single parameter * @@ -155,7 +178,9 @@ protected function setParameter($key, $value) throw new RuntimeException('Request cannot be modified after it has been sent!'); } - return $this->traitSetParameter($key, $value); + $this->parameters->set($key, $value); + + return $this; } /** @@ -179,6 +204,25 @@ public function setTestMode($value) return $this->setParameter('testMode', $value); } + /** + * Validate the request. + * + * This method is called internally by gateways to avoid wasting time with an API call + * when the request is clearly invalid. + * + * @param string ... a variable length list of required parameters + * @throws InvalidRequestException + */ + public function validate() + { + foreach (func_get_args() as $key) { + $value = $this->parameters->get($key); + if (! isset($value)) { + throw new InvalidRequestException("The $key parameter is required"); + } + } + } + /** * Get the card. * diff --git a/src/Common/ParametersTrait.php b/src/Common/ParametersTrait.php deleted file mode 100644 index a945d9a3..00000000 --- a/src/Common/ParametersTrait.php +++ /dev/null @@ -1,84 +0,0 @@ -parameters->set($key, $value); - - return $this; - } - - /** - * Get one parameter. - * - * @return mixed A single parameter value. - */ - protected function getParameter($key) - { - return $this->parameters->get($key); - } - - /** - * Get all parameters. - * - * @return array An associative array of parameters. - */ - public function getParameters() - { - return $this->parameters->all(); - } - - /** - * Initialize the object with parameters. - * - * If any unknown parameters passed, they will be ignored. - * - * @param array $parameters An associative array of parameters - * @return $this. - */ - public function initialize(array $parameters = []) - { - $this->parameters = new ParameterBag; - Helper::initialize($this, $parameters); - return $this; - } - - /** - * Validate the request. - * - * This method is called internally by gateways to avoid wasting time with an API call - * when the request is clearly invalid. - * - * @param string ... a variable length list of required parameters - * @throws InvalidRequestException - */ - public function validate(...$args) - { - foreach ($args as $key) { - $value = $this->parameters->get($key); - if (! isset($value)) { - throw new InvalidRequestException("The $key parameter is required"); - } - } - } -} diff --git a/tests/Omnipay/Common/CreditCardTest.php b/tests/Omnipay/Common/CreditCardTest.php index fdd4e91e..78c69dc1 100644 --- a/tests/Omnipay/Common/CreditCardTest.php +++ b/tests/Omnipay/Common/CreditCardTest.php @@ -2,7 +2,6 @@ namespace Omnipay\Common; -use Omnipay\Common\Exception\InvalidRequestException; use Omnipay\Tests\TestCase; class CreditCardTest extends TestCase