1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Composite Utils package. |
4
|
|
|
* |
5
|
|
|
* (c) Emily Shepherd <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the |
8
|
|
|
* LICENSE.md file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @package spaark/composite-utils |
11
|
|
|
* @author Emily Shepherd <[email protected]> |
12
|
|
|
* @license MIT |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Spaark\CompositeUtils\Traits; |
16
|
|
|
|
17
|
|
|
use Spaark\CompositeUtils\Service\ConditionalPropertyAccessor; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Classes with this trait will allow their properties to be accessed |
21
|
|
|
* according to their access permissions and datatype restrictions |
22
|
|
|
*/ |
23
|
|
|
trait PropertyAccessTrait |
24
|
|
|
{ |
25
|
|
|
use HasReflectorTrait; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The accessor for this class |
29
|
|
|
* |
30
|
|
|
* @var ConditionalPropertyAccessor |
31
|
|
|
*/ |
32
|
|
|
protected $accessor; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Returns the ConditionalPropertyAccessor for this object, or |
36
|
|
|
* constructs one on the fly if one does not yet exist |
37
|
|
|
* |
38
|
|
|
* @return ConditionalPropertyAccessor |
39
|
|
|
*/ |
40
|
|
|
protected function getPropertyAccessor() |
41
|
|
|
{ |
42
|
|
|
if (!$this->accessor) |
43
|
|
|
{ |
44
|
|
|
$this->accessor = new ConditionalPropertyAccessor |
45
|
|
|
( |
46
|
|
|
$this, |
47
|
|
|
self::getReflectionComposite() |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this->accessor; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Gets the value of a property, if it is publically readable, |
56
|
|
|
* using the ConditionalPropertyAccessor |
57
|
|
|
* |
58
|
|
|
* @param string $property The property to get |
59
|
|
|
* @return mixed The property value |
60
|
|
|
*/ |
61
|
|
|
public function __get($property) |
62
|
|
|
{ |
63
|
|
|
return $this->getPropertyAccessor()->getValue($property); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Sets the value of a property, if it is publically writable, and |
68
|
|
|
* enforces its datatype permissions using the |
69
|
|
|
* ConditionalPropertyAccessor |
70
|
|
|
* |
71
|
|
|
* @param string $property The property to set |
72
|
|
|
* @param mixed $value The value to set |
73
|
|
|
*/ |
74
|
|
|
public function __set(string $property, $value) |
75
|
|
|
{ |
76
|
|
|
$this->getPropertyAccessor()->setValue($property, $value); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|