Completed
Push — master ( ccbe0b...982bf9 )
by Oscar
02:19
created

Document   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 10
c 2
b 0
f 1
lcom 0
cbo 0
dl 0
loc 98
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A __get() 0 4 2
A __set() 0 4 1
A jsonSerialize() 0 4 1
A setId() 0 6 1
A getId() 0 4 1
A getData() 0 4 1
A setData() 0 6 1
1
<?php
2
3
namespace FlyCrud;
4
5
use JsonSerializable;
6
7
class Document implements JsonSerializable
8
{
9
    protected $id;
10
    protected $data;
11
12
    /**
13
     * Constructor
14
     * 
15
     * @param array $data
16
     * @param mixed $id
17
     */
18
    public function __construct(array $data = [], $id = null)
19
    {
20
        $this->setData($data);
21
        $this->setId($id ?: uniqid());
22
    }
23
24
    /**
25
     * Returns a value of the document
26
     * 
27
     * @param string $name
28
     * 
29
     * @return mixed
30
     */
31
    public function __get($name)
32
    {
33
        return isset($this->data[$name]) ? $this->data[$name] : null;
34
    }
35
36
    /**
37
     * Create/edit a value
38
     * 
39
     * @param string $name
40
     * @param mixed  $value
41
     */
42
    public function __set($name, $value)
43
    {
44
        $this->data[$name] = $value;
45
    }
46
47
    /**
48
     * @see JsonSerializable
49
     *
50
     * @return array
51
     */
52
    public function jsonSerialize()
53
    {
54
        return $this->data;
55
    }
56
57
    /**
58
     * Set a new id for the document
59
     * 
60
     * @param mixed $id
61
     * 
62
     * @return self
63
     */
64
    public function setId($id)
65
    {
66
        $this->id = $id;
67
68
        return $this;
69
    }
70
71
    /**
72
     * Returns the document id
73
     * 
74
     * @return mixed
75
     */
76
    public function getId()
77
    {
78
        return $this->id;
79
    }
80
81
    /**
82
     * Returns the document data
83
     * 
84
     * @return array
85
     */
86
    public function getData()
87
    {
88
        return $this->data;
89
    }
90
91
    /**
92
     * Change the document data
93
     * 
94
     * @param array $data
95
     * 
96
     * @return self
97
     */
98
    public function setData(array $data)
99
    {
100
        $this->data = $data;
101
102
        return $this;
103
    }
104
}
105