Completed
Push — master ( d8757f...fa9ad1 )
by Matteo
02:59
created

Document::has()   B

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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 7
loc 18
ccs 11
cts 11
cp 1
rs 8.8571
cc 5
eloc 10
nc 4
nop 1
crap 5
1
<?php
2
3
namespace Mattbit\Flat\Document;
4
5
class Document implements Matchable, Identifiable, Encodable
6
{
7
    protected $attributes;
8
9
    /**
10
     * Construct a document.
11
     *
12
     * @param array $attributes
13
     */
14 21
    public function __construct(array $attributes = [])
15
    {
16 21
        $this->attributes = $attributes;
17 21
    }
18
19 1
    public function toArray()
20
    {
21 1
        return $this->attributes;
22
    }
23
24 5
    public function getId()
25
    {
26 5
        return $this->get('_id');
27
    }
28
29 1
    public function set($name, $value)
30
    {
31 1
        $this->attributes[$name] = $value;
32 1
    }
33
34
    /**
35
     * Get a document attribute using dot notation.
36
     *
37
     * @param $name
38
     *
39
     * @return mixed
40
     */
41 15
    public function get($name)
42
    {
43 15
        if (array_key_exists($name, $this->attributes)) {
44 12
            return $this->attributes[$name];
45
        }
46
47 4
        $attributes = $this->attributes;
48
49 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...
50 4
            if (is_array($attributes) && array_key_exists($key, $attributes)) {
51 1
                $attributes = $attributes[$key];
52 1
            } else {
53 3
                return;
54
            }
55 1
        }
56
57 1
        return $attributes;
58
    }
59
60
    /**
61
     * Check if the document as a given attribute.
62
     *
63
     * @param  $name
64
     *
65
     * @return bool
66
     */
67 2
    public function has($name)
68
    {
69 2
        if (array_key_exists($name, $this->attributes)) {
70 1
            return true;
71
        }
72
73 2
        $attributes = $this->attributes;
74
75 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...
76 2
            if (is_array($attributes) && array_key_exists($key, $attributes)) {
77 1
                $attributes = $attributes[$key];
78 1
            } else {
79 2
                return false;
80
            }
81 1
        }
82
83 1
        return true;
84
    }
85
}
86