1 | <?php |
||
34 | final class Class_ implements ProjectFactoryStrategy |
||
35 | { |
||
36 | |||
37 | /** |
||
38 | * Returns true when the strategy is able to handle the object. |
||
39 | * |
||
40 | * @param object $object object to check. |
||
41 | * @return boolean |
||
42 | */ |
||
43 | 1 | public function matches($object) |
|
47 | |||
48 | /** |
||
49 | * Creates an ClassElement out of the given object. |
||
50 | * Since an object might contain other objects that need to be converted the $factory is passed so it can be |
||
51 | * used to create nested Elements. |
||
52 | * |
||
53 | * @param ClassNode $object object to convert to an Element |
||
54 | * @param StrategyContainer $strategies used to convert nested objects. |
||
55 | * @param Context $context of the created object |
||
56 | * @return ClassElement |
||
57 | */ |
||
58 | 9 | public function create($object, StrategyContainer $strategies, Context $context = null) |
|
59 | { |
||
60 | 9 | if (!$this->matches($object)) { |
|
61 | 1 | throw new InvalidArgumentException( |
|
62 | 1 | sprintf('%s cannot handle objects with the type %s', |
|
63 | 1 | __CLASS__, |
|
64 | 1 | is_object($object) ? get_class($object) : gettype($object) |
|
65 | ) |
||
66 | ); |
||
67 | } |
||
68 | |||
69 | 8 | $docBlock = $this->createDocBlock($object->getDocComment(), $strategies, $context); |
|
70 | |||
71 | 8 | $classElement = new ClassElement( |
|
72 | 8 | $object->fqsen, |
|
73 | $docBlock, |
||
74 | 8 | $object->extends ? new Fqsen('\\' . $object->extends) : null, |
|
75 | 8 | $object->isAbstract(), |
|
76 | 8 | $object->isFinal() |
|
77 | ); |
||
78 | |||
79 | 8 | if (isset($object->implements)) { |
|
80 | 1 | foreach ($object->implements as $interfaceClassName) { |
|
81 | 1 | $classElement->addInterface( |
|
82 | 1 | new Fqsen('\\' . $interfaceClassName->toString()) |
|
83 | ); |
||
84 | } |
||
85 | } |
||
86 | |||
87 | 8 | if (isset($object->stmts)) { |
|
88 | 4 | foreach ($object->stmts as $stmt) { |
|
89 | 4 | switch (get_class($stmt)) { |
|
90 | case TraitUse::class: |
||
91 | 1 | foreach ($stmt->traits as $use) { |
|
92 | 1 | $classElement->addUsedTrait(new Fqsen('\\'. $use->toString())); |
|
93 | } |
||
94 | 1 | break; |
|
95 | case PropertyNode::class: |
||
96 | 1 | $properties = new PropertyIterator($stmt); |
|
97 | 1 | foreach ($properties as $property) { |
|
98 | 1 | $element = $this->createMember($property, $strategies, $context); |
|
99 | 1 | $classElement->addProperty($element); |
|
100 | } |
||
101 | 1 | break; |
|
102 | case ClassMethod::class: |
||
103 | 1 | $method = $this->createMember($stmt, $strategies, $context); |
|
104 | 1 | $classElement->addMethod($method); |
|
105 | 1 | break; |
|
106 | case ClassConst::class: |
||
107 | 1 | $constants = new ClassConstantIterator($stmt); |
|
108 | 1 | foreach ($constants as $const) { |
|
109 | 1 | $element = $this->createMember($const, $strategies, $context); |
|
110 | 1 | $classElement->addConstant($element); |
|
111 | } |
||
112 | 4 | break; |
|
113 | } |
||
114 | } |
||
115 | } |
||
116 | |||
117 | 8 | return $classElement; |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * @param Node|PropertyIterator|ClassConstantIterator $stmt |
||
122 | * @param StrategyContainer $strategies |
||
123 | * @param Context $context |
||
124 | * @return Element |
||
125 | */ |
||
126 | 4 | private function createMember($stmt, StrategyContainer $strategies, Context $context = null) |
|
131 | |||
132 | |||
133 | /** |
||
134 | * @param Doc $docBlock |
||
135 | * @param StrategyContainer $strategies |
||
136 | * @param Context $context |
||
137 | * @return null|\phpDocumentor\Reflection\DocBlock |
||
138 | */ |
||
139 | 8 | private function createDocBlock(Doc $docBlock = null, StrategyContainer $strategies, Context $context = null) |
|
147 | } |
||
148 |