Object::bind()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * Joomla! common library.
4
 *
5
 * @copyright  Copyright (C) 2017 Roberto Segura López, Inc. All rights reserved.
6
 * @license    See COPYING.txt
7
 */
8
9
namespace Phproberto\Joomla\Object;
10
11
/**
12
 * Object class
13
 *
14
 * @since   __DEPLOY_VERSION__
15
 */
16
class Object
17
{
18
	/**
19
	 * Stored entity.
20
	 *
21
	 * @var  array
22
	 */
23
	protected $data;
24
25
	/**
26
	 * Constructor.
27
	 *
28
	 * @param   array  $data  Object data
29
	 */
30 19
	public function __construct(array $data = [])
31
	{
32 19
		$this->data = $data ? $data : [];
33 19
	}
34
35
	/**
36
	 * Retrieve all the values of the object.
37
	 *
38
	 * @return  array
39
	 */
40 2
	public function data()
41
	{
42 2
		return $this->data;
43
	}
44
45
	/**
46
	 * Bind data to the object.
47
	 *
48
	 * @param   mixed  $data  Array | Object
49
	 *
50
	 * @return  self
51
	 */
52 3
	public function bind($data)
53
	{
54 3
		$data = (array) $data;
55
56 3
		foreach ($data as $property => $value)
57
		{
58 3
			$this->data[$property] = $value;
59
		}
60
61 3
		return $this;
62
	}
63
64
	/**
65
	 * Clear stored object.
66
	 *
67
	 * @return  self
68
	 */
69 1
	public function clear()
70
	{
71 1
		$this->data = [];
72
73 1
		return $this;
74
	}
75
76
	/**
77
	 * Get property of the object
78
	 *
79
	 * @param   string  $property  Name of the property to retrieve
80
	 * @param   mixed   $default   Default value if property does not exist
81
	 *
82
	 * @return  mixed
83
	 */
84 3
	public function get($property, $default = null)
85
	{
86 3
		if (!$this->has($property))
87
		{
88 2
			return $default;
89
		}
90
91 3
		return null !== $this->data[$property] ? $this->data[$property] : $default;
92
	}
93
94
	/**
95
	 * Check if the object has a property.
96
	 *
97
	 * @param   string  $property  Name of the property to check for
98
	 *
99
	 * @return  boolean
100
	 */
101 5
	public function has($property)
102
	{
103 5
		return array_key_exists($property, $this->data);
104
	}
105
106
	/**
107
	 * Show available properties.
108
	 *
109
	 * @return  array
110
	 */
111 1
	public function properties()
112
	{
113 1
		return array_keys($this->data);
114
	}
115
116
	/**
117
	 * Assign a value of to object property.
118
	 *
119
	 * @param   string  $property  Name of the property to set
120
	 * @param   mixed   $value     Value to assign
121
	 *
122
	 * @return  self
123
	 */
124 2
	public function assign($property, $value)
125
	{
126 2
		$this->data[$property] = $value;
127
128 2
		return $this;
129
	}
130
131
	/**
132
	 * Set data.
133
	 *
134
	 * @param   array  $data  Data
135
	 *
136
	 * @return  self
137
	 */
138 1
	public function setData(array $data)
139
	{
140 1
		$this->data = $data;
141
142 1
		return $this;
143
	}
144
145
	/**
146
	 * Unassigns an object property.
147
	 *
148
	 * @param   string  $property  Name of the property to set
149
	 *
150
	 * @return  self
151
	 */
152 2
	public function unassign($property)
153
	{
154 2
		unset($this->data[$property]);
155
156 2
		return $this;
157
	}
158
}
159