Document::get()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 7
Ratio 38.89 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
dl 7
loc 18
rs 8.8571
c 0
b 0
f 0
ccs 11
cts 11
cp 1
cc 5
eloc 10
nc 4
nop 1
crap 5
1
<?php
2
3
namespace Mattbit\Flat\Model;
4
5
class Document extends \ArrayObject implements DocumentInterface
6
{
7
    /**
8
     * Get a document attribute using dot notation.
9
     *
10
     * @param $name
11
     *
12
     * @return mixed
13
     */
14 17
    public function get($name)
15
    {
16 17
        if (array_key_exists($name, $this)) {
17 14
            return $this[$name];
18
        }
19
20 4
        $attributes = $this->getArrayCopy();
21
22 4 View Code Duplication
        foreach (explode('.', $name) as $key) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23 4
            if (is_array($attributes) && array_key_exists($key, $attributes)) {
24 1
                $attributes = $attributes[$key];
25 1
            } else {
26 3
                return;
27
            }
28 1
        }
29
30 1
        return $attributes;
31
    }
32
33
    /**
34
     * Check if the document as a given attribute.
35
     *
36
     * @param  $name
37
     *
38
     * @return bool
39
     */
40 2
    public function has($name)
41
    {
42 2
        if (array_key_exists($name, $this)) {
43 1
            return true;
44
        }
45
46 2
        $attributes = $this->getArrayCopy();
47
48 2 View Code Duplication
        foreach (explode('.', $name) as $key) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49 2
            if (is_array($attributes) && array_key_exists($key, $attributes)) {
50 1
                $attributes = $attributes[$key];
51 1
            } else {
52 2
                return false;
53
            }
54 1
        }
55
56 1
        return true;
57
    }
58
59 3
    public function set($name, $value)
60
    {
61 3
        $keys = explode('.', $name);
62
63 3
        $attributes = &$this;
64
65 3
        while (count($keys) > 1) {
66 1
            $key = array_shift($keys);
67
            // If the key doesn't exist at this depth, we will just create an empty array
68
            // to hold the next value, allowing us to create the arrays to hold final
69
            // values at the correct depth. Then we'll keep digging into the array.
70 1
            if (!isset($attributes[$key]) || !is_array($attributes[$key])) {
71 1
                $attributes[$key] = [];
72 1
            }
73 1
            $attributes = &$attributes[$key];
74 1
        }
75
76 3
        $attributes[array_shift($keys)] = $value;
77 3
    }
78
79 5
    public function getId()
80
    {
81 5
        return $this->get('_id');
82
    }
83
84 1
    public function setId($id)
85
    {
86 1
        return $this->set('_id', $id);
87
    }
88
}
89