Skip to content

Use ParameterTrait #184

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
May 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 1 addition & 33 deletions src/Common/AbstractGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@
*/
abstract class AbstractGateway implements GatewayInterface
{
/**
* @var \Symfony\Component\HttpFoundation\ParameterBag
*/
protected $parameters;
use ParametersTrait;

/**
* @var ClientInterface
Expand Down Expand Up @@ -114,35 +111,6 @@ 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
*/
Expand Down
47 changes: 4 additions & 43 deletions src/Common/CreditCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@
*/
class CreditCard
{
use ParametersTrait;

const BRAND_VISA = 'visa';
const BRAND_MASTERCARD = 'mastercard';
const BRAND_DISCOVER = 'discover';
Expand Down Expand Up @@ -133,13 +135,6 @@ 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
*
Expand Down Expand Up @@ -205,40 +200,6 @@ 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.
*
Expand Down Expand Up @@ -269,8 +230,9 @@ 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.
*
* @throws InvalidCreditCardException
* @return void
* @throws Exception\InvalidRequestException
* @throws InvalidCreditCardException
*/
public function validate()
{
Expand Down Expand Up @@ -298,7 +260,6 @@ public function validate()
throw new InvalidCreditCardException('Card number should have 12 to 19 digits');
}
}

/**
* Get Card Title.
*
Expand Down
25 changes: 1 addition & 24 deletions src/Common/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
*/
class Item implements ItemInterface
{
/**
* @var \Symfony\Component\HttpFoundation\ParameterBag
*/
protected $parameters;
use ParametersTrait;

/**
* Create a new item with the specified parameters
Expand All @@ -46,26 +43,6 @@ 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}
*/
Expand Down
54 changes: 5 additions & 49 deletions src/Common/Message/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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;

Expand Down Expand Up @@ -66,12 +67,9 @@
*/
abstract class AbstractRequest implements RequestInterface
{
/**
* The request parameters
*
* @var \Symfony\Component\HttpFoundation\ParameterBag
*/
protected $parameters;
use ParametersTrait {
setParameter as traitSetParameter;
}

/**
* The request client.
Expand Down Expand Up @@ -145,27 +143,6 @@ 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
*
Expand All @@ -180,9 +157,7 @@ protected function setParameter($key, $value)
throw new RuntimeException('Request cannot be modified after it has been sent!');
}

$this->parameters->set($key, $value);

return $this;
return $this->traitSetParameter($key, $value);
}

/**
Expand All @@ -206,25 +181,6 @@ 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.
*
Expand Down
84 changes: 84 additions & 0 deletions src/Common/ParametersTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Omnipay\Common;

use Omnipay\Common\Exception\InvalidRequestException;
use Symfony\Component\HttpFoundation\ParameterBag;

trait ParametersTrait
{
/**
* Internal storage of all of the parameters.
*
* @var ParameterBag
*/
protected $parameters;

/**
* 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;
}

/**
* 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");
}
}
}
}
1 change: 1 addition & 0 deletions tests/Omnipay/Common/CreditCardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Omnipay\Common;

use Omnipay\Common\Exception\InvalidRequestException;
use Omnipay\Tests\TestCase;

class CreditCardTest extends TestCase
Expand Down