1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Omnipay\Common; |
4
|
|
|
|
5
|
|
|
use Omnipay\Common\Exception\InvalidRequestException; |
6
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
7
|
|
|
|
8
|
|
|
trait ParametersTrait |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Internal storage of all of the parameters. |
12
|
|
|
* |
13
|
|
|
* @var ParameterBag |
14
|
|
|
*/ |
15
|
|
|
protected $parameters; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Set one parameter. |
19
|
|
|
* |
20
|
|
|
* @param string $key Parameter key |
21
|
|
|
* @param mixed $value Parameter value |
22
|
|
|
* @return $this |
23
|
|
|
*/ |
24
|
476 |
|
protected function setParameter($key, $value) |
25
|
|
|
{ |
26
|
476 |
|
$this->parameters->set($key, $value); |
27
|
|
|
|
28
|
476 |
|
return $this; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get one parameter. |
33
|
|
|
* |
34
|
|
|
* @return mixed A single parameter value. |
35
|
|
|
*/ |
36
|
440 |
|
protected function getParameter($key) |
37
|
|
|
{ |
38
|
440 |
|
return $this->parameters->get($key); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get all parameters. |
43
|
|
|
* |
44
|
|
|
* @return array An associative array of parameters. |
45
|
|
|
*/ |
46
|
27 |
|
public function getParameters() |
47
|
|
|
{ |
48
|
27 |
|
return $this->parameters->all(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Initialize the object with parameters. |
53
|
|
|
* |
54
|
|
|
* If any unknown parameters passed, they will be ignored. |
55
|
|
|
* |
56
|
|
|
* @param array $parameters An associative array of parameters |
57
|
|
|
* @return $this. |
|
|
|
|
58
|
|
|
*/ |
59
|
|
|
public function initialize(array $parameters = []) |
60
|
|
|
{ |
61
|
|
|
$this->parameters = new ParameterBag; |
62
|
|
|
Helper::initialize($this, $parameters); |
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Validate the request. |
68
|
|
|
* |
69
|
|
|
* This method is called internally by gateways to avoid wasting time with an API call |
70
|
|
|
* when the request is clearly invalid. |
71
|
|
|
* |
72
|
|
|
* @param string ... a variable length list of required parameters |
73
|
|
|
* @throws InvalidRequestException |
74
|
|
|
*/ |
75
|
12 |
|
public function validate(...$args) |
76
|
|
|
{ |
77
|
12 |
|
foreach ($args as $key) { |
78
|
12 |
|
$value = $this->parameters->get($key); |
79
|
12 |
|
if (! isset($value)) { |
80
|
6 |
|
throw new InvalidRequestException("The $key parameter is required"); |
81
|
|
|
} |
82
|
8 |
|
} |
83
|
9 |
|
} |
84
|
|
|
} |
85
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.