1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This software package is licensed under AGPL, Commercial license. |
5
|
|
|
* |
6
|
|
|
* @package maslosoft/addendum |
7
|
|
|
* @licence AGPL, Commercial |
8
|
|
|
* @copyright Copyright (c) Piotr Masełkowski <[email protected]> (Meta container, further improvements, bugfixes) |
9
|
|
|
* @copyright Copyright (c) Maslosoft (Meta container, further improvements, bugfixes) |
10
|
|
|
* @copyright Copyright (c) Jan Suchal (Original version, builder, parser) |
11
|
|
|
* @link http://maslosoft.com/addendum/ - maslosoft addendum |
12
|
|
|
* @link https://code.google.com/p/addendum/ - original addendum project |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Maslosoft\Addendum; |
16
|
|
|
|
17
|
|
|
use Maslosoft\Addendum\Exceptions\CircularReferenceException; |
18
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
19
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotationInterface; |
20
|
|
|
use Maslosoft\Addendum\Utilities\ConflictChecker; |
21
|
|
|
use Maslosoft\Addendum\Utilities\TargetChecker; |
22
|
|
|
use ReflectionClass; |
23
|
|
|
use ReflectionProperty; |
24
|
|
|
use UnexpectedValueException; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Modernized Addendum PHP Reflection Annotations |
28
|
|
|
* http://code.google.com/p/addendum/ |
29
|
|
|
* |
30
|
|
|
* Copyright (C) 2006-2009 Jan "johno Suchal <[email protected]> |
31
|
|
|
|
32
|
|
|
* This library is free software; you can redistribute it and/or |
33
|
|
|
* modify it under the terms of the GNU Lesser General Public |
34
|
|
|
* License as published by the Free Software Foundation; either |
35
|
|
|
* version 2.1 of the License, or (at your option) any later version. |
36
|
|
|
|
37
|
|
|
* This library is distributed in the hope that it will be useful, |
38
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
39
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
40
|
|
|
* Lesser General Public License for more details. |
41
|
|
|
|
42
|
|
|
* You should have received a copy of the GNU Lesser General Public |
43
|
|
|
* License along with this library; if not, write to the Free Software |
44
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
45
|
|
|
* */ |
46
|
|
|
abstract class Annotation implements AnnotationInterface |
47
|
|
|
{ |
48
|
|
|
|
49
|
|
|
use Traits\MetaState; |
50
|
|
|
|
51
|
|
|
protected $_properties = []; |
52
|
|
|
protected $_publicProperties = []; |
53
|
|
|
private static $_creationStack = []; |
54
|
|
|
|
55
|
50 |
|
public function __construct($data = [], $target = false) |
56
|
|
|
{ |
57
|
50 |
|
$reflection = new ReflectionClass($this); |
58
|
50 |
|
$class = $reflection->name; |
59
|
50 |
|
if (isset(self::$_creationStack[$class])) |
60
|
50 |
|
{ |
61
|
|
|
throw new CircularReferenceException("Circular annotation reference on '$class'", E_USER_ERROR); |
62
|
|
|
} |
63
|
50 |
|
self::$_creationStack[$class] = true; |
64
|
50 |
|
foreach ($data as $key => $value) |
65
|
|
|
{ |
66
|
43 |
|
if ($reflection->hasProperty($key)) |
67
|
43 |
|
{ |
68
|
43 |
|
$this->$key = $value; |
69
|
43 |
|
} |
70
|
43 |
|
$this->_properties[$key] = $value; |
71
|
50 |
|
} |
72
|
50 |
|
foreach ($reflection->getProperties(ReflectionProperty::IS_PUBLIC) as $field) |
73
|
|
|
{ |
74
|
50 |
|
$this->_publicProperties[] = $field->name; |
75
|
50 |
|
} |
76
|
|
|
try |
77
|
|
|
{ |
78
|
50 |
|
ConflictChecker::register($this); |
79
|
50 |
|
TargetChecker::check($this, $target); |
80
|
|
|
} |
81
|
50 |
|
catch (UnexpectedValueException $ex) |
82
|
|
|
{ |
83
|
5 |
|
throw $ex; |
84
|
50 |
|
} finally |
85
|
|
|
{ |
86
|
50 |
|
unset(self::$_creationStack[$class]); |
87
|
50 |
|
} |
88
|
50 |
|
} |
89
|
|
|
|
90
|
|
|
public function getProperties() |
91
|
|
|
{ |
92
|
|
|
return $this->_properties; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Init annotation |
97
|
|
|
*/ |
98
|
|
|
abstract public function init(); |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Convert to array |
102
|
|
|
* @return mixed[] |
103
|
|
|
*/ |
104
|
|
|
public function toArray() |
105
|
|
|
{ |
106
|
|
|
$result = []; |
107
|
|
|
foreach ($this->_publicProperties as $field) |
108
|
|
|
{ |
109
|
|
|
$result[$field] = $this->$field; |
110
|
|
|
} |
111
|
|
|
return $result; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|