Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
17 / 17 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| SchemaFieldLoader | |
100.00% |
17 / 17 |
|
100.00% |
3 / 3 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
| fetchAttribute | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| fetchAttributes | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @license MIT |
| 5 | * @author hazuki3417<hazuki3417@gmail.com> |
| 6 | * @copyright 2022 hazuki3417 all rights reserved. |
| 7 | */ |
| 8 | |
| 9 | namespace Selen\MongoDB\Attribute; |
| 10 | |
| 11 | use LogicException; |
| 12 | use ReflectionAttribute; |
| 13 | use ReflectionProperty; |
| 14 | use Selen\MongoDB\Attributes\Nest; |
| 15 | use Selen\MongoDB\Attributes\SchemaField; |
| 16 | |
| 17 | /** |
| 18 | * MongoDB SchemaのFieldに設定されたAttributeを読み込むクラス |
| 19 | */ |
| 20 | class SchemaFieldLoader |
| 21 | { |
| 22 | /** @var ReflectionProperty */ |
| 23 | public $reflectionProperty; |
| 24 | |
| 25 | /** @var \ReflectionAttribute<SchemaFieldMarkerInterface>|null */ |
| 26 | public $attributeFieldSchema; |
| 27 | |
| 28 | /** @var \ReflectionAttribute<Nest>|null */ |
| 29 | public $attributeNest; |
| 30 | |
| 31 | public function __construct(ReflectionProperty $reflectionProperty) |
| 32 | { |
| 33 | $this->reflectionProperty = $reflectionProperty; |
| 34 | |
| 35 | $attributes = $this->fetchAttributes(SchemaFieldMarkerInterface::class); |
| 36 | |
| 37 | if (1 < count($attributes)) { |
| 38 | $format = 'Invalid attribute specification. Only one "%s" can be specified.'; |
| 39 | $mes = \sprintf($format, SchemaField::class); |
| 40 | throw new LogicException($mes); |
| 41 | } |
| 42 | |
| 43 | $this->attributeFieldSchema = \array_shift($attributes); |
| 44 | $this->attributeNest = $this->fetchAttribute(Nest::class); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * 属性を取得します(1件) |
| 49 | * |
| 50 | * @param string $attributeName 取得する属性名を渡します |
| 51 | * |
| 52 | * @return \ReflectionAttribute<object>|null 存在する場合は属性を、存在しない場合はnullを返します |
| 53 | */ |
| 54 | public function fetchAttribute(string $attributeName) |
| 55 | { |
| 56 | $attributes = $this->reflectionProperty->getAttributes( |
| 57 | $attributeName, |
| 58 | ReflectionAttribute::IS_INSTANCEOF |
| 59 | ); |
| 60 | return \array_shift($attributes); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * 属性を取得します(n件) |
| 65 | * |
| 66 | * @param string $attributeName 取得する属性名を渡します |
| 67 | * |
| 68 | * @return \ReflectionAttribute<object>[] 存在する場合は属性を保持した配列を、存在しない場合は空配列を返します |
| 69 | */ |
| 70 | public function fetchAttributes(string $attributeName) |
| 71 | { |
| 72 | return $this->reflectionProperty->getAttributes( |
| 73 | $attributeName, |
| 74 | ReflectionAttribute::IS_INSTANCEOF |
| 75 | ); |
| 76 | } |
| 77 | } |