WosObjectMetadata::offsetExists()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * PHP Client for DDN Web Object Scalar (WOS) API
5
 *
6
 * @package Wosclient
7
 * @author  Casey McLaughlin <[email protected]>
8
 * @license http://opensource.org/licenses/MIT MIT
9
 * @link    https://github.com/caseyamcl/wosclient
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 *
14
 * ------------------------------------------------------------------
15
 */
16
17
namespace WosClient;
18
19
use Countable;
20
use Psr\Http\Message\ResponseInterface;
21
use Traversable;
22
use WosClient\Helper\ArrayToMetadataStringTrait;
23
24
/**
25
 * Class WosObjectMetadata
26
 *
27
 * @author Casey McLaughlin <[email protected]>
28
 */
29
class WosObjectMetadata implements \IteratorAggregate, WosObjectMetadataInterface
30
{
31
    use ArrayToMetadataStringTrait;
32
33
    /**
34
     * Unknown value
35
     */
36
    const UNKNOWN = null;
37
38
    /**
39
     * @var array
40
     */
41
    private $metadata;
42
43
    /**
44
     * @var int
45
     */
46
    private $objectSize;
47
48
    /**
49
     * WosObjectMetadata constructor.
50
     *
51
     * @param ResponseInterface $httpResponse
52
     */
53
    public function __construct(ResponseInterface $httpResponse)
54
    {
55
        $this->metadata   = json_decode('{' . $httpResponse->getHeaderLine('x-ddn-meta') . '}', true);
0 ignored issues
show
Documentation Bug introduced by
It seems like json_decode('{' . $httpR...ddn-meta') . '}', true) of type * is incompatible with the declared type array of property $metadata.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
56
        $this->objectSize = $httpResponse->hasHeader('x-ddn-length')
57
            ? (int) $httpResponse->getHeaderLine('x-ddn-length')
58
            : self::UNKNOWN;
59
    }
60
61
    /**
62
     * Return representation of the metadata as it would appear as a x-ddn-metadata value
63
     *
64
     * @return mixed
65
     */
66
    public function __toString()
67
    {
68
        return $this->metadataToString($this->toArray());
69
    }
70
71
    /**
72
     * @param string $key
73
     * @return mixed
74
     */
75
    public function get($key)
76
    {
77
        return $this->offsetGet($key);
78
    }
79
80
    /**
81
     * @param string $key
82
     * @return bool
83
     */
84
    public function has($key)
85
    {
86
        return $this->offsetExists($key);
87
    }
88
89
    /**
90
     * @return array|mixed[]
91
     */
92
    public function toArray()
93
    {
94
        return $this->metadata;
95
    }
96
97
    /**
98
     * Returns data length in BYTES if known, NULL otherwise
99
     *
100
     * @return int|null
101
     */
102
    public function getObjectSize()
103
    {
104
        return $this->objectSize;
105
    }
106
107
    /**
108
     * Whether a offset exists
109
     *
110
     * @link   http://php.net/manual/en/arrayaccess.offsetexists.php
111
     * @param  mixed $offset <p>
112
     *                      An offset to check for.
113
     *                      </p>
114
     * @return boolean true on success or false on failure.
115
     *                      </p>
116
     *                      <p>
117
     *                      The return value will be casted to boolean if non-boolean was returned.
118
     * @since  5.0.0
119
     */
120
    public function offsetExists($offset)
121
    {
122
        return array_key_exists($offset, $this->metadata);
123
    }
124
125
    /**
126
     * Offset to retrieve
127
     *
128
     * @link   http://php.net/manual/en/arrayaccess.offsetget.php
129
     * @param  mixed $offset <p>
130
     *                      The offset to retrieve.
131
     *                      </p>
132
     * @return mixed Can return all value types.
133
     * @since  5.0.0
134
     */
135
    public function offsetGet($offset)
136
    {
137
        return $this->metadata[$offset];
138
    }
139
140
    /**
141
     * Offset to set
142
     *
143
     * @link   http://php.net/manual/en/arrayaccess.offsetset.php
144
     * @param  mixed $offset <p>
145
     *                      The offset to assign the value to.
146
     *                      </p>
147
     * @param  mixed $value  <p>
148
     *                      The value to set.
149
     *                      </p>
150
     * @return void
151
     * @since  5.0.0
152
     */
153
    public function offsetSet($offset, $value)
154
    {
155
        throw new \RuntimeException("Cannot set $offset for WosObjectMetadata.  Metadata is immutable");
156
    }
157
158
    /**
159
     * Offset to unset
160
     *
161
     * @link   http://php.net/manual/en/arrayaccess.offsetunset.php
162
     * @param  mixed $offset <p>
163
     *                      The offset to unset.
164
     *                      </p>
165
     * @return void
166
     * @since  5.0.0
167
     */
168
    public function offsetUnset($offset)
169
    {
170
        throw new \RuntimeException("Cannot unset $offset for WosObjectMetadata.  Metadata is immutable");
171
    }
172
173
    /**
174
     * Retrieve an external iterator
175
     *
176
     * @link   http://php.net/manual/en/iteratoraggregate.getiterator.php
177
     * @return Traversable An instance of an object implementing <b>Iterator</b> or
178
     *        <b>Traversable</b>
179
     * @since  5.0.0
180
     */
181
    public function getIterator()
182
    {
183
        return new \ArrayIterator($this->metadata);
184
    }
185
186
    /**
187
     * Count elements of an object
188
     *
189
     * @link   http://php.net/manual/en/countable.count.php
190
     * @return int The custom count as an integer.
191
     *        </p>
192
     *        <p>
193
     *        The return value is cast to an integer.
194
     * @since  5.1.0
195
     */
196
    public function count()
197
    {
198
        return count($this->metadata);
199
    }
200
}
201