1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright (c) 2017-present Ganbaro Digital Ltd |
7
|
|
|
* All rights reserved. |
8
|
|
|
* |
9
|
|
|
* Redistribution and use in source and binary forms, with or without |
10
|
|
|
* modification, are permitted provided that the following conditions |
11
|
|
|
* are met: |
12
|
|
|
* |
13
|
|
|
* * Redistributions of source code must retain the above copyright |
14
|
|
|
* notice, this list of conditions and the following disclaimer. |
15
|
|
|
* |
16
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
17
|
|
|
* notice, this list of conditions and the following disclaimer in |
18
|
|
|
* the documentation and/or other materials provided with the |
19
|
|
|
* distribution. |
20
|
|
|
* |
21
|
|
|
* * Neither the names of the copyright holders nor the names of his |
22
|
|
|
* contributors may be used to endorse or promote products derived |
23
|
|
|
* from this software without specific prior written permission. |
24
|
|
|
* |
25
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
26
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
27
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
28
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
29
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
30
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
31
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
32
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
33
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
34
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
35
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
36
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
37
|
|
|
* |
38
|
|
|
* @category Libraries |
39
|
|
|
* @package MessagingPipeline/Requirements |
40
|
|
|
* @author Stuart Herbert <[email protected]> |
41
|
|
|
* @copyright 2017-present Ganbaro Digital Ltd www.ganbarodigital.com |
42
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
43
|
|
|
* @link http://ganbarodigital.github.io/php-mv-messaging-pipeline |
44
|
|
|
*/ |
45
|
|
|
|
46
|
|
|
namespace GanbaroDigital\MessagingPipeline\V1\Requirements; |
47
|
|
|
|
48
|
|
|
use GanbaroDigital\Defensive\V1\Interfaces\ListRequirement; |
49
|
|
|
use GanbaroDigital\Defensive\V1\Interfaces\Requirement; |
50
|
|
|
use GanbaroDigital\Defensive\V1\Requirements\InvokeableRequirement; |
51
|
|
|
use GanbaroDigital\Defensive\V1\Requirements\ListableRequirement; |
52
|
|
|
use GanbaroDigital\MessagingPipeline\V1\Checks\DoesArrayHaveKey; |
53
|
|
|
use GanbaroDigital\MessagingPipeline\V1\Exceptions\ConfigKeyNotFound; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* make sure that the supplied config array contains the array key |
57
|
|
|
* that we need |
58
|
|
|
*/ |
59
|
|
|
class RequireConfigHasKey implements Requirement, ListRequirement |
60
|
|
|
{ |
61
|
|
|
// saves us having to declare ::__invoke() ourselves |
62
|
|
|
use InvokeableRequirement; |
63
|
|
|
|
64
|
|
|
// saves us having to declare ::toList() ourselves |
65
|
|
|
use ListableRequirement; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* the array key that we are looking for |
69
|
|
|
* |
70
|
|
|
* @var string|int |
71
|
|
|
*/ |
72
|
|
|
private $expectedKey; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* create a Requirement that is ready to use |
76
|
|
|
* |
77
|
|
|
* @param string|int $expectedKey |
78
|
|
|
* the array key that we are looking for |
79
|
|
|
*/ |
80
|
|
|
public function __construct($expectedKey) |
81
|
|
|
{ |
82
|
|
|
$this->expectedKey = $expectedKey; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* create a Requirement that is ready to use |
87
|
|
|
* |
88
|
|
|
* @param string|int $expectedKey |
89
|
|
|
* the array key that we are looking for |
90
|
|
|
* @return Requirement |
91
|
|
|
*/ |
92
|
|
|
public static function apply($expectedKey) : Requirement |
93
|
|
|
{ |
94
|
|
|
return new static($expectedKey); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* make sure that the supplied config array contains the entry that |
99
|
|
|
* we need |
100
|
|
|
* |
101
|
|
|
* @param array|ArrayAccess $item |
102
|
|
|
* the item to examine |
103
|
|
|
* @param string $fieldOrVarName |
104
|
|
|
* what do you call $item in your own code? |
105
|
|
|
* @return void |
106
|
|
|
* |
107
|
|
|
* @throws ConfigKeyNotFound |
108
|
|
|
* if $item[$this->expectedKey] does not exist |
109
|
|
|
*/ |
110
|
|
|
public function to($item, $fieldOrVarName='$item') |
111
|
|
|
{ |
112
|
|
|
// are we happy? |
113
|
|
|
if (DoesArrayHaveKey::check($item, $this->expectedKey)) { |
114
|
|
|
// yes we are |
115
|
|
|
return; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// if we get here, then no, we are not happy |
119
|
|
|
throw ConfigKeyNotFound::newFromInputParameter( |
120
|
|
|
$item, |
121
|
|
|
$fieldOrVarName, [ |
122
|
|
|
'expectedKey' => $this->expectedKey |
123
|
|
|
] |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
} |