Completed
Push — master ( aeb11c...f98da5 )
by Adrian
02:32
created

ArrayWrapper::__construct()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 4
Bugs 1 Features 1
Metric Value
c 4
b 1
f 1
dl 0
loc 14
ccs 12
cts 12
cp 1
rs 8.8571
cc 5
eloc 9
nc 8
nop 1
crap 5
1
<?php
2
3
namespace Sirius\Validation\DataWrapper;
4
5
use Sirius\Validation\Util\Arr;
6
use Sirius\Validation\DataWrapper\WrapperInterface;
7
8
class ArrayWrapper implements WrapperInterface
9
{
10
11
    /**
12
     * @var array
13
     */
14
    protected $data = array();
15
16
    /**
17
     * @param array|\ArrayObject|object $data
18
     *
19
     * @throws \InvalidArgumentException
20
     */
21 31
    public function __construct($data = array())
22
    {
23 31
        if (is_object($data)) {
24 1
            if ($data instanceof \ArrayObject) {
25 1
                $data = $data->getArrayCopy();
26 1
            } elseif (method_exists($data, 'toArray')) {
27 1
                $data = $data->toArray();
28 1
            }
29 1
        }
30 31
        if ( ! is_array($data)) {
31 1
            throw new \InvalidArgumentException('Data passed to validator is not an array or an ArrayObject');
32
        }
33 30
        $this->data = $data;
34 30
    }
35
36 14
    public function getItemValue($item)
37
    {
38 14
        return Arr::getByPath($this->data, $item);
39
    }
40
41 14
    public function getItemsBySelector($selector)
42
    {
43 14
        return Arr::getBySelector($this->data, $selector);
44
    }
45
46
}
47