-
-
Notifications
You must be signed in to change notification settings - Fork 243
/
Copy pathParametersTrait.php
87 lines (78 loc) · 2.07 KB
/
ParametersTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?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.
*
* @param string $key The key
* @param mixed $default The default value if the parameter key does not exist
*
* @return mixed A single parameter value.
*/
protected function getParameter($key, $default = null)
{
return $this->parameters->get($key, $default);
}
/**
* 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");
}
}
}
}