|
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
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Classes using this trait can have generic properties |
|
23
|
|
|
* |
|
24
|
|
|
* All generated generics will automatically use this Generic, but you |
|
25
|
|
|
* may also use this in the root class itself as it provides helpers |
|
26
|
|
|
* for creating generic instances |
|
27
|
|
|
*/ |
|
28
|
|
|
trait GenericTrait |
|
29
|
|
|
{ |
|
30
|
|
|
use HasReflectorTrait; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var GenericContext |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $genericContext; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var ObjectType |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $objectType; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Gets the object descriptor for this class |
|
44
|
|
|
* |
|
45
|
|
|
* @return ObjectType |
|
46
|
|
|
*/ |
|
47
|
2 |
|
public function getObjectType() : ObjectType |
|
48
|
|
|
{ |
|
49
|
2 |
|
if (!$this->objectType) |
|
50
|
|
|
{ |
|
51
|
2 |
|
$this->objectType = |
|
52
|
2 |
|
(new TypeParser())->parseFromType($this); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
2 |
|
return $this->objectType; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Gets the generic context for this class or creates one if it does |
|
60
|
|
|
* not exist |
|
61
|
|
|
* |
|
62
|
|
|
* @return GenericContext |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getGenericContext() : GenericContext |
|
65
|
|
|
{ |
|
66
|
|
|
if (!$this->genericContext) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->genericContext = new GenericContext |
|
69
|
|
|
( |
|
70
|
|
|
$this->getObjectType(), |
|
71
|
|
|
static::getReflectionComposite() |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $this->genericContext; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Sets the generic context for this class if one does not already |
|
80
|
|
|
* exist |
|
81
|
|
|
* |
|
82
|
|
|
* @param GenericContext $genericContext |
|
83
|
|
|
*/ |
|
84
|
|
|
public function setGenericContext(GenericContext $genericContext) |
|
85
|
|
|
: void |
|
86
|
|
|
{ |
|
87
|
|
|
if ($this->genericContext) |
|
88
|
|
|
{ |
|
89
|
|
|
throw new ImmutablePropertyException |
|
90
|
|
|
( |
|
91
|
|
|
(string)$this->getObjectType(), |
|
92
|
|
|
'genericContext' |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$this->genericContext = $genericContext; |
|
97
|
|
|
$this->objectType = $genericContext->object; |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Gets an instance of a generic |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $name |
|
104
|
|
|
* @return mixed |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function getInstanceOfGeneric(string $name) |
|
107
|
|
|
{ |
|
108
|
|
|
$class = |
|
109
|
|
|
(string)$this->getGenericContext()->getGenericType($name); |
|
110
|
|
|
|
|
111
|
|
|
return new $class(); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
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@propertyannotation 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.