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\Factory\Reflection\TypeParser; |
18
|
|
|
use Spaark\CompositeUtils\Model\Generic\GenericContext; |
19
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\ObjectType; |
20
|
|
|
use Spaark\CompositeUtils\Exception\ImmutablePropertyException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Classes using this trait can have generic properties |
24
|
|
|
* |
25
|
|
|
* All generated generics will automatically use this Generic, but you |
26
|
|
|
* may also use this in the root class itself as it provides helpers |
27
|
|
|
* for creating generic instances |
28
|
|
|
*/ |
29
|
|
|
trait GenericTrait |
30
|
|
|
{ |
31
|
|
|
use HasReflectorTrait; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var GenericContext |
35
|
|
|
*/ |
36
|
|
|
protected $genericContext; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var ObjectType |
40
|
|
|
*/ |
41
|
|
|
protected $objectType; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Gets the object descriptor for this class |
45
|
|
|
* |
46
|
|
|
* @return ObjectType |
47
|
|
|
*/ |
48
|
2 |
|
public function getObjectType() : ObjectType |
49
|
|
|
{ |
50
|
2 |
|
if (!$this->objectType) |
51
|
|
|
{ |
52
|
2 |
|
$this->objectType = |
53
|
2 |
|
(new TypeParser())->parseFromType($this); |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
return $this->objectType; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Gets the generic context for this class or creates one if it does |
61
|
|
|
* not exist |
62
|
|
|
* |
63
|
|
|
* @return GenericContext |
64
|
|
|
*/ |
65
|
|
|
public function getGenericContext() : GenericContext |
66
|
|
|
{ |
67
|
|
|
if (!$this->genericContext) |
68
|
|
|
{ |
69
|
|
|
$this->genericContext = new GenericContext |
70
|
|
|
( |
71
|
|
|
$this->getObjectType(), |
72
|
|
|
static::getReflectionComposite() |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $this->genericContext; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Sets the generic context for this class if one does not already |
81
|
|
|
* exist |
82
|
|
|
* |
83
|
|
|
* @param GenericContext $genericContext |
84
|
|
|
*/ |
85
|
|
|
public function setGenericContext(GenericContext $genericContext) |
86
|
|
|
: void |
87
|
|
|
{ |
88
|
|
|
if ($this->genericContext) |
89
|
|
|
{ |
90
|
|
|
throw new ImmutablePropertyException |
91
|
|
|
( |
92
|
|
|
(string)$this->getObjectType(), |
93
|
|
|
'genericContext' |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->genericContext = $genericContext; |
98
|
|
|
$this->objectType = $genericContext->object; |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Gets an instance of a generic |
103
|
|
|
* |
104
|
|
|
* @param string $name |
105
|
|
|
* @return mixed |
106
|
|
|
*/ |
107
|
|
|
protected function getInstanceOfGeneric(string $name) |
108
|
|
|
{ |
109
|
|
|
$class = |
110
|
|
|
(string)$this->getGenericContext()->getGenericType($name); |
111
|
|
|
|
112
|
|
|
return new $class(); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.