1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
/** |
3
|
|
|
* Created by Vitaly Iegorov <[email protected]>. |
4
|
|
|
* on 18.08.16 at 11:25 |
5
|
|
|
*/ |
6
|
|
|
namespace samsonframework\container\resolver; |
7
|
|
|
|
8
|
|
|
use samsonframework\container\metadata\ClassMetadata; |
9
|
|
|
use samsonframework\container\metadata\PropertyMetadata; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class property resolving trait. |
13
|
|
|
* |
14
|
|
|
* @author Vitaly Iegorov <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
trait PropertyResolverTrait |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Generic class property resolver. |
20
|
|
|
* |
21
|
|
|
* @param \ReflectionProperty $property |
22
|
|
|
* @param ClassMetadata $classMetadata |
23
|
|
|
* |
24
|
|
|
* @return PropertyMetadata Resolved property metadata |
25
|
|
|
*/ |
26
|
11 |
|
protected function resolvePropertyMetadata(\ReflectionProperty $property, ClassMetadata $classMetadata) : PropertyMetadata |
27
|
|
|
{ |
28
|
|
|
// Create method metadata instance |
29
|
11 |
|
$propertyMetadata = new PropertyMetadata($classMetadata); |
30
|
11 |
|
$propertyMetadata->name = $property->getName(); |
31
|
11 |
|
$propertyMetadata->modifiers = $property->getModifiers(); |
32
|
11 |
|
$propertyMetadata->isPublic = $property->isPublic(); |
33
|
11 |
|
$propertyMetadata->typeHint = $this->getCommentTypeHint($property->getDocComment()); |
34
|
|
|
|
35
|
|
|
// Store property metadata to class metadata |
36
|
11 |
|
return $classMetadata->propertiesMetadata[$propertyMetadata->name] = $propertyMetadata; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Parse property comments and return type hint if present. |
41
|
|
|
* |
42
|
|
|
* @param string $comments Property comments |
43
|
|
|
* |
44
|
|
|
* @return string Property type hint if present |
45
|
|
|
*/ |
46
|
11 |
|
protected function getCommentTypeHint(string $comments) : string |
47
|
|
|
{ |
48
|
|
|
// Parse property type hint if present |
49
|
11 |
|
if (preg_match('/@var\s+(?<class>[^\s]+)/', $comments, $matches)) { |
50
|
11 |
|
return $matches[1]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return ''; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|