~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to lib/Entity/EntityTrait.php

  • Committer: Dan Garner
  • Date: 2015-08-11 09:29:02 UTC
  • mto: This revision was merged to the branch mainline in revision 453.
  • Revision ID: git-v1:a86fb4369b7395c13367577d23b14c0ab4528c1a
Transitions fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
 
11
11
 
12
12
use Xibo\Helper\ObjectVars;
13
 
use Xibo\Service\LogServiceInterface;
14
 
use Xibo\Storage\StorageServiceInterface;
 
13
use Xibo\Helper\Sanitize;
15
14
 
16
 
/**
17
 
 * Class EntityTrait
18
 
 * used by all entities
19
 
 * @package Xibo\Entity
20
 
 */
21
15
trait EntityTrait
22
16
{
23
17
    private $hash = null;
24
18
    private $loaded = false;
25
 
    private $permissionsClass = null;
26
 
    private $canChangeOwner = true;
 
19
    private $deleting = false;
27
20
 
28
21
    public $buttons = [];
29
 
    private $jsonExclude = ['buttons', 'jsonExclude', 'originalValues'];
30
 
 
31
 
    /** @var array Original values hydrated */
32
 
    protected $originalValues = [];
33
 
 
34
 
    /**
35
 
     * @var StorageServiceInterface
36
 
     */
37
 
    private $store;
38
 
 
39
 
    /**
40
 
     * @var LogServiceInterface
41
 
     */
42
 
    private $log;
43
 
 
44
 
    /**
45
 
     * Set common dependencies.
46
 
     * @param StorageServiceInterface $store
47
 
     * @param LogServiceInterface $log
48
 
     * @return $this
49
 
     */
50
 
    protected function setCommonDependencies($store, $log)
51
 
    {
52
 
        $this->store = $store;
53
 
        $this->log = $log;
54
 
        return $this;
55
 
    }
56
 
 
57
 
    /**
58
 
     * Get Store
59
 
     * @return StorageServiceInterface
60
 
     */
61
 
    protected function getStore()
62
 
    {
63
 
        return $this->store;
64
 
    }
65
 
 
66
 
    /**
67
 
     * Get Log
68
 
     * @return LogServiceInterface
69
 
     */
70
 
    protected function getLog()
71
 
    {
72
 
        return $this->log;
73
 
    }
 
22
    private $jsonExclude = ['buttons', 'jsonExclude'];
74
23
 
75
24
    /**
76
25
     * Hydrate an entity with properties
89
38
        foreach ($properties as $prop => $val) {
90
39
            if (property_exists($this, $prop)) {
91
40
 
92
 
                if ((stripos(strrev($prop), 'dI') === 0 || in_array($prop, $intProperties)) && !in_array($prop, $stringProperties))
 
41
                if (stripos(strrev($prop), 'dI') === 0 || in_array($prop, $intProperties))
93
42
                    $val = intval($val);
94
43
                else if (in_array($prop, $stringProperties))
95
 
                    $val = filter_var($val, FILTER_SANITIZE_STRING);
 
44
                    $val = Sanitize::string($val);
96
45
                else if (in_array($prop, $htmlStringProperties))
97
46
                    $val = htmlentities($val);
98
47
 
99
48
                $this->{$prop} =  $val;
100
 
                $this->originalValues[$prop] = $val;
101
49
            }
102
50
        }
103
51
 
105
53
    }
106
54
 
107
55
    /**
108
 
     * Reset originals to current values
109
 
     */
110
 
    public function setOriginals()
111
 
    {
112
 
        foreach ($this->jsonSerialize() as $key => $value) {
113
 
            $this->originalValues[$key] = $value;
114
 
        }
115
 
    }
116
 
 
117
 
    /**
118
 
     * Get the original value of a property
119
 
     * @param string $property
120
 
     * @return null|mixed
121
 
     */
122
 
    public function getOriginalValue($property)
123
 
    {
124
 
        return (isset($this->originalValues[$property])) ? $this->originalValues[$property] : null;
125
 
    }
126
 
 
127
 
    /**
128
 
     * Has the provided property been changed from its original value
129
 
     * @param string $property
130
 
     * @return bool
131
 
     */
132
 
    public function hasPropertyChanged($property)
133
 
    {
134
 
        if (!property_exists($this, $property))
135
 
            return true;
136
 
 
137
 
        return $this->getOriginalValue($property) != $this->{$property};
138
 
    }
139
 
 
140
 
    /**
141
 
     * @param $property
142
 
     * @return bool
143
 
     */
144
 
    public function propertyOriginallyExisted($property)
145
 
    {
146
 
        return array_key_exists($property, $this->originalValues);
147
 
    }
148
 
 
149
 
    /**
150
 
     * Get all changed properties for this entity
151
 
     */
152
 
    public function getChangedProperties()
153
 
    {
154
 
        $changedProperties = [];
155
 
 
156
 
        foreach ($this->jsonSerialize() as $key => $value) {
157
 
            if (!is_array($value) && !is_object($value) && $this->propertyOriginallyExisted($key) && $this->hasPropertyChanged($key)) {
158
 
                $changedProperties[$key] = $this->getOriginalValue($key) . ' > ' . $value;
159
 
            }
160
 
        }
161
 
 
162
 
        return $changedProperties;
163
 
    }
164
 
 
165
 
    /**
166
56
     * Json Serialize
167
57
     * @return array
168
58
     */
181
71
    }
182
72
 
183
73
    /**
184
 
     * To Array
185
 
     * @return array
186
 
     */
187
 
    public function toArray()
188
 
    {
189
 
        return $this->jsonSerialize();
190
 
    }
191
 
 
192
 
    /**
193
74
     * Add a property to the excluded list
194
75
     * @param string $property
195
76
     */
206
87
    {
207
88
        $this->jsonExclude = array_diff($this->jsonExclude, [$property]);
208
89
    }
209
 
 
210
 
    /**
211
 
     * Get the Permissions Class
212
 
     * @return string
213
 
     */
214
 
    public function permissionsClass()
215
 
    {
216
 
        return ($this->permissionsClass == null) ? get_class($this) : $this->permissionsClass;
217
 
    }
218
 
 
219
 
    /**
220
 
     * Set the Permissions Class
221
 
     * @param string $class
222
 
     */
223
 
    protected function setPermissionsClass($class)
224
 
    {
225
 
        $this->permissionsClass = $class;
226
 
    }
227
 
 
228
 
    /**
229
 
     * Can the owner change?
230
 
     * @return bool
231
 
     */
232
 
    public function canChangeOwner()
233
 
    {
234
 
        return $this->canChangeOwner && method_exists($this, 'setOwner');
235
 
    }
236
 
 
237
 
    /**
238
 
     * @param bool $bool Can the owner be changed?
239
 
     */
240
 
    protected function setCanChangeOwner($bool)
241
 
    {
242
 
        $this->canChangeOwner = $bool;
243
 
    }
244
 
 
245
 
    /**
246
 
     * @param $entityId
247
 
     * @param $message
248
 
     * @param array[Optional] $changedProperties
249
 
     */
250
 
    protected function audit($entityId, $message, $changedProperties = null)
251
 
    {
252
 
        if ($changedProperties === null) {
253
 
            // No properties provided, so we should work them out
254
 
            // If we have originals, then get changed, otherwise get the current object state
255
 
            $changedProperties = (count($this->originalValues) <= 0) ? $this->toArray() : $this->getChangedProperties();
256
 
        }
257
 
 
258
 
        $class = substr(get_class($this), strrpos(get_class($this), '\\') + 1);
259
 
 
260
 
        if (count($changedProperties) > 0)
261
 
            $this->getLog()->audit($class, $entityId, $message, $changedProperties);
262
 
    }
263
90
}
 
 
b'\\ No newline at end of file'