1 | <?php |
||
36 | class WrapperParser |
||
37 | { |
||
38 | private $_wrapperClass; |
||
39 | /** |
||
40 | * @var ComplexTypeParser[] |
||
41 | */ |
||
42 | private $_complexTypes; |
||
43 | |||
44 | /** |
||
45 | * @desc Count of arrays in document |
||
46 | * @var array |
||
47 | */ |
||
48 | static private $arrayCnt = []; |
||
49 | |||
50 | 16 | public function __construct($wrapperClass) |
|
54 | |||
55 | 16 | public function parse() |
|
56 | { |
||
57 | 16 | $publicFields = $this->_wrapperClass->getProperties(ReflectionProperty::IS_PUBLIC); |
|
58 | 16 | foreach ($publicFields as $field) { |
|
59 | 14 | $this->_makeComplexType($field->getName(), $field->getDocComment()); |
|
60 | 16 | } |
|
61 | 16 | } |
|
62 | |||
63 | 14 | private function _makeComplexType($name, $docComment) |
|
64 | { |
||
65 | 14 | if (preg_match('#@type (\w*)\[\]#', $docComment, $matches)) { |
|
66 | $type = $matches[1]; |
||
67 | $strategy = 'array'; |
||
68 | } else { |
||
69 | 14 | preg_match('#@type (\w+)#', $docComment, $matches); |
|
70 | 14 | if (isset($matches[1])) { |
|
71 | 14 | $type = trim($matches[1]); |
|
72 | 14 | $strategy = trim($matches[1]); |
|
73 | 14 | } else { |
|
74 | $type = 'void'; |
||
75 | $strategy = 'void'; |
||
76 | } |
||
77 | } |
||
78 | |||
79 | 14 | $optional = false; |
|
80 | 14 | if (preg_match('#@optional#', $docComment, $matches)) { |
|
81 | $optional = true; |
||
82 | } |
||
83 | |||
84 | switch ($strategy) { |
||
85 | 14 | case 'object': |
|
86 | $this->_complexTypes[] = new Object($type, $name, $this->getComplexTypes(), $optional); |
||
87 | break; |
||
88 | 14 | case 'wrapper': |
|
89 | $this->_complexTypes[] = $this->_createWrapperObject($type, $name, $docComment, $optional); |
||
90 | break; |
||
91 | 14 | case 'array': |
|
92 | $this->_complexTypes[] = $this->_createArrayObject($type, $name, $docComment, $optional); |
||
93 | break; |
||
94 | 14 | default: |
|
95 | 14 | $this->_complexTypes[] = new ComplexTypeParser($type, $name, $optional); |
|
96 | 14 | break; |
|
97 | 14 | } |
|
98 | 14 | } |
|
99 | |||
100 | private function _createWrapperObject($type, $name, $docComment, $optional = false) |
||
101 | { |
||
102 | $wrapper = $this->wrapper($type, $docComment); |
||
103 | $object = null; |
||
104 | if ($wrapper->getComplexTypes()) { |
||
105 | $object = new Object($type, $name, $wrapper->getComplexTypes(), $optional); |
||
106 | } |
||
107 | return new Object($type, $name, $object, $optional); |
||
108 | } |
||
109 | |||
110 | private function _createArrayObject($type, $name, $docComment, $optional = false) |
||
111 | { |
||
112 | $object = null; |
||
113 | if ($type == 'wrapper') { |
||
114 | $complex = $this->wrapper($type, $docComment)->getComplexTypes(); |
||
115 | $object = new Object($type, $name, $complex, $optional); |
||
116 | } elseif ($this->isComplex($type)) { |
||
117 | $complex = $this->getComplexTypes(); |
||
118 | $object = new Object($type, $name, $complex, $optional); |
||
119 | } |
||
120 | if (!isset(self::$arrayCnt[$name])) { |
||
121 | self::$arrayCnt[$name] = 0; |
||
122 | } |
||
123 | return new Arrays($type, $name, $object, $optional, self::$arrayCnt[$name]++); |
||
124 | } |
||
125 | |||
126 | 16 | public function getComplexTypes() |
|
130 | |||
131 | public function wrapper(&$type, $docComment) |
||
132 | { |
||
133 | if (!$this->isComplex($type)) { |
||
134 | throw new WrapperParserException("This attribute is not complex type."); |
||
143 | |||
144 | public function isComplex($type) |
||
148 | } |
||
149 |