HasObject::setObject()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 20 and the first side effect is on line 13.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Joomla! common library.
4
 *
5
 * @copyright  Copyright (C) 2017 Roberto Segura López, Inc. All rights reserved.
6
 * @license    GNU/GPL 2, http://www.gnu.org/licenses/gpl-2.0.htm
7
 */
8
9
namespace Phproberto\Joomla\Object\Traits;
10
11
use Phproberto\Joomla\Object\Object;
12
13 1
defined('JPATH_PLATFORM') || die;
14
15
/**
16
 * Classes that have an associated object.
17
 *
18
 * @since  __DEPLOY_VERSION__
19
 */
20
trait HasObject
21
{
22
	/**
23
	 * Associated object.
24
	 *
25
	 * @var  \Phproberto\Joomla\Object\Object
26
	 */
27
	protected $object;
28
29
	/**
30
	 * Set value of an object property.
31
	 *
32
	 * @param   string  $property  Name of the property to set
33
	 * @param   mixed   $value     Value to assign
34
	 *
35
	 * @return  self
36
	 */
37 1
	public function assign($property, $value)
38
	{
39 1
		if (null === $this->object)
40
		{
41 1
			$this->object = new Object;
42
		}
43
44 1
		$this->object->assign($property, $value);
45
46 1
		return $this;
47
	}
48
49
	/**
50
	 * Bind data to the object.
51
	 *
52
	 * @param   array  $data  Data to bind
53
	 *
54
	 * @return  self
55
	 */
56 2
	public function bind(array $data)
57
	{
58 2
		if (null === $this->object)
59
		{
60 1
			$this->object = new Object;
61
		}
62
63 2
		$this->object->bind($data);
64
65 2
		return $this;
66
	}
67
68
	/**
69
	 * Retrieve all the values of the object.
70
	 *
71
	 * @return  array
72
	 */
73 1
	public function data()
74
	{
75 1
		return $this->object()->data();
76
	}
77
78
	/**
79
	 * Get property of the object
80
	 *
81
	 * @param   string  $property  Name of the property to retrieve
82
	 * @param   mixed   $default   Default value if property does not exist
83
	 *
84
	 * @return  mixed
85
	 */
86 1
	public function get($property, $default = null)
87
	{
88 1
		return $this->object()->get($property, $default);
89
	}
90
91
	/**
92
	 * Check if the object has a property.
93
	 *
94
	 * @param   string  $property  Name of the property to check for
95
	 *
96
	 * @return  boolean
97
	 */
98 1
	public function has($property)
99
	{
100 1
		return $this->object()->has($property);
101
	}
102
103
	/**
104
	 * Load object.
105
	 *
106
	 * @return  self
107
	 */
108 4
	public function loadObject()
109
	{
110 4
		$this->object = new Object($this->loadObjectData());
111
112 4
		return $this;
113
	}
114
115
	/**
116
	 * Load object data.
117
	 *
118
	 * @return  array
119
	 */
120
	abstract protected function loadObjectData();
121
122
	/**
123
	 * Get the associated object.
124
	 *
125
	 * @return  Entity
126
	 */
127 4
	public function object()
128
	{
129 4
		if (null === $this->object)
130
		{
131 4
			$this->loadObject();
132
		}
133
134 4
		return clone $this->object;
135
	}
136
137
	/**
138
	 * Set the object.
139
	 *
140
	 * @param   Object  $object  Value to assign
141
	 *
142
	 * @return  self
143
	 */
144 1
	public function setObject(Object $object)
145
	{
146 1
		$this->object = $object;
147
148 1
		return $this;
149
	}
150
151
	/**
152
	 * Unassign an object property.
153
	 *
154
	 * @param   string  $property  Name of the property to set
155
	 *
156
	 * @return  self
157
	 */
158 1
	public function unassign($property)
159
	{
160 1
		if (null === $this->object)
161
		{
162 1
			$this->object = new Object;
163
		}
164
165 1
		$this->object->unassign($property);
166
167 1
		return $this;
168
	}
169
}
170