1
|
|
|
<?php |
|
|
|
|
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 Entity |
26
|
|
|
*/ |
27
|
|
|
protected $object; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Bind data to the object. |
31
|
|
|
* |
32
|
|
|
* @param array $data Data to bind |
33
|
|
|
* |
34
|
|
|
* @return self |
35
|
|
|
*/ |
36
|
2 |
|
public function bind(array $data) |
37
|
|
|
{ |
38
|
2 |
|
if (null === $this->object) |
39
|
|
|
{ |
40
|
1 |
|
$this->object = new Object; |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
2 |
|
$this->object->bind($data); |
44
|
|
|
|
45
|
2 |
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Retrieve all the values of the object. |
50
|
|
|
* |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
1 |
|
public function data() |
54
|
|
|
{ |
55
|
1 |
|
return $this->object()->data(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get the associated object. |
60
|
|
|
* |
61
|
|
|
* @return Entity |
62
|
|
|
*/ |
63
|
4 |
|
public function object() |
64
|
|
|
{ |
65
|
4 |
|
if (null === $this->object) |
66
|
|
|
{ |
67
|
4 |
|
$this->loadObject(); |
68
|
|
|
} |
69
|
|
|
|
70
|
4 |
|
return clone $this->object; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get property of the object |
75
|
|
|
* |
76
|
|
|
* @param string $property Name of the property to retrieve |
77
|
|
|
* @param mixed $default Default value if property does not exist |
78
|
|
|
* |
79
|
|
|
* @return mixed |
80
|
|
|
*/ |
81
|
1 |
|
public function get($property, $default = null) |
82
|
|
|
{ |
83
|
1 |
|
return $this->object()->get($property, $default); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Check if the object has a property. |
88
|
|
|
* |
89
|
|
|
* @param string $property Name of the property to check for |
90
|
|
|
* |
91
|
|
|
* @return boolean |
92
|
|
|
*/ |
93
|
1 |
|
public function has($property) |
94
|
|
|
{ |
95
|
1 |
|
return $this->object()->has($property); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Load object. |
100
|
|
|
* |
101
|
|
|
* @return self |
102
|
|
|
*/ |
103
|
4 |
|
public function loadObject() |
104
|
|
|
{ |
105
|
4 |
|
$this->object = new Object($this->loadData()); |
|
|
|
|
106
|
|
|
|
107
|
4 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Load object data. |
112
|
|
|
* |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
|
|
abstract protected function loadData(); |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Set value of an object property. |
119
|
|
|
* |
120
|
|
|
* @param string $property Name of the property to set |
121
|
|
|
* @param mixed $value Value to assign |
122
|
|
|
* |
123
|
|
|
* @return self |
124
|
|
|
*/ |
125
|
1 |
|
public function set($property, $value) |
126
|
|
|
{ |
127
|
1 |
|
if (null === $this->object) |
128
|
|
|
{ |
129
|
1 |
|
$this->object = new Object; |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
1 |
|
$this->object->set($property, $value); |
133
|
|
|
|
134
|
1 |
|
return $this; |
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
|
|
|
|
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.