MetaData::has()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * The software is based on the Axon Framework project which is
17
 * licensed under the Apache 2.0 license. For more information on the Axon Framework
18
 * see <http://www.axonframework.org/>.
19
 *
20
 * This software consists of voluntary contributions made by many individuals
21
 * and is licensed under the MIT license. For more information, see
22
 * <http://www.governor-framework.org/>.
23
 */
24
25
namespace Governor\Framework\Domain;
26
27
use JMS\Serializer\Annotation\Type;
28
use JMS\Serializer\Annotation\Exclude;
29
30
/**
31
 * The object holds meta data information attached to a {@see MessageInterface}
32
 *
33
 * @author    "David Kalosi" <[email protected]>
34
 * @license   <a href="http://www.opensource.org/licenses/mit-license.php">MIT License</a>
35
 */
36
class MetaData implements \IteratorAggregate, \Countable
37
{
38
39
    /**
40
     * @Exclude
41
     * @var MetaData 
42
     */
43
    private static $emptyInstance;
44
45
    const METADATA_IMMUTABLE = 'The MetaData object is immutable';
46
47
    /**
48
     * Metadata storage.
49
     *
50
     * @Type ("array")
51
     * @var array
52
     */
53
    private $metadata = [];
54
55
    /**
56
     * Constructor.
57
     *     
58
     * @param array $metadata
59
     */
60 19
    public function __construct(array $metadata = [])
61
    {
62 19
        $this->metadata = $metadata;
63 19
    }
64
65
    /**
66
     * Returns the metadata.
67
     *
68
     * @return array An array of metadata
69
     *
70
     * @api
71
     */
72 6
    public function all()
73
    {
74 6
        return $this->metadata;
75
    }
76
77
    /**
78
     * Returns the metadata keys.
79
     *
80
     * @return array An array of metadata keys
81
     *
82
     * @api
83
     */
84 1
    public function keys()
85
    {
86 1
        return array_keys($this->metadata);
87
    }
88
89
    /**
90
     * Returns a metadadta.
91
     *
92
     * @param string $key    The metadadta key 
93
     *
94
     * @return mixed
95
     */
96 7
    public function get($key)
97
    {
98 7
        return isset($this->metadata[$key]) ? $this->metadata[$key] : null;
99
    }
100
101
    /**
102
     * Adds metadata.
103
     *
104
     * @param array $metadata An array of metadata
105
     * @return \Governor\Framework\Domain\MetaData
106
     */
107 6
    public function mergeWith(array $metadata = [])
108
    {
109 6
        if (empty($metadata)) {
110
            return $this;
111
        }
112
113 6
        return new MetaData(array_replace($this->metadata, $metadata));
114
    }
115
116
    /**
117
     * 
118
     * @param array $keys
119
     * @return \Governor\Framework\Domain\MetaData
120
     */
121 1
    public function withoutKeys(array $keys = [])
122
    {
123 1
        if (empty($keys)) {
124
            return $this;
125
        }
126
127 1
        $newMetadata = $this->metadata;
128
129 1
        foreach ($keys as $key) {
130 1
            if (isset($newMetadata[$key])) {
131 1
                unset($newMetadata[$key]);
132 1
            }
133 1
        }
134
135 1
        return new MetaData($newMetadata);
136
    }
137
138
    /**
139
     * 
140
     * @return boolean
141
     */
142 2
    public function isEmpty()
143
    {
144 2
        return empty($this->metadata);
145
    }
146
147
    /**
148
     * Returns true if the key is defined.
149
     *
150
     * @param string $key The key
151
     *
152
     * @return Boolean true if the parameter exists, false otherwise
153
     *
154
     * @api
155
     */
156 4
    public function has($key)
157
    {
158 4
        return isset($this->metadata[$key]);
159
    }
160
161
    /**
162
     * Returns the number of metadata entries.
163
     * 
164
     * @return integer Element count
165
     */
166 5
    public function count()
167
    {
168 5
        return count($this->metadata);
169
    }
170
171
    /**
172
     * Returns an iterator,
173
     * 
174
     * @return \ArrayIterator
175
     */
176 1
    public function getIterator()
177
    {
178 1
        return new \ArrayIterator($this->metadata);
179
    }
180
181
    /**
182
     * 
183
     * @return MetaData
184
     */
185 138
    public static function emptyInstance()
186
    {
187 138
        if (!isset(self::$emptyInstance)) {
188 1
            self::$emptyInstance = new MetaData();
189 1
        }
190
191 138
        return self::$emptyInstance;
192
    }
193
194
    /**
195
     * @param mixed $other
196
     * @return bool
197
     */
198 9
    public function isEqualTo($other)
199
    {       
200 9
        if (is_array($other)) {
201 8
            return $this->metadata == $other;
202
        }
203
204 1
        if (is_object($other)) {
205 1
            return $this == $other;
206
        }
207
208 1
        return false;
209
    }
210
211
}
212