Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| SchemaLoader | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
4 | |||
| 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 ReflectionClass; |
| 14 | use Selen\MongoDB\Attributes\Schema; |
| 15 | |
| 16 | /** |
| 17 | * MongoDB Schemaに設定されたAttributeを読み込むクラス |
| 18 | */ |
| 19 | class SchemaLoader |
| 20 | { |
| 21 | /** @var ReflectionClass<object> */ |
| 22 | public $reflectionClass; |
| 23 | |
| 24 | /** @var \ReflectionAttribute<SchemaMarkerInterface>|null */ |
| 25 | public $attributeSchema; |
| 26 | |
| 27 | /** @var array<string,SchemaFieldLoader> key: fieldName, value: instance */ |
| 28 | public $fieldLoaders; |
| 29 | |
| 30 | /** |
| 31 | * @param ReflectionClass<object> $reflectionClass MongoDB Schemaのクラス名を渡します |
| 32 | * |
| 33 | * @throws LogicException 属性の指定が不正なときに発生します |
| 34 | */ |
| 35 | public function __construct(ReflectionClass $reflectionClass) |
| 36 | { |
| 37 | $this->reflectionClass = $reflectionClass; |
| 38 | |
| 39 | $attributes = $reflectionClass->getAttributes( |
| 40 | SchemaMarkerInterface::class, |
| 41 | ReflectionAttribute::IS_INSTANCEOF |
| 42 | ); |
| 43 | |
| 44 | if (1 < count($attributes)) { |
| 45 | $format = 'Invalid attribute specification. Only one "%s" can be specified.'; |
| 46 | $mes = \sprintf($format, Schema::class); |
| 47 | throw new LogicException($mes); |
| 48 | } |
| 49 | |
| 50 | $this->attributeSchema = \array_shift($attributes); |
| 51 | |
| 52 | $properties = $reflectionClass->getProperties(); |
| 53 | |
| 54 | $fieldLoaders = []; |
| 55 | |
| 56 | foreach ($properties as $property) { |
| 57 | $schemaFieldLoader = new SchemaFieldLoader($property); |
| 58 | |
| 59 | if ($schemaFieldLoader->attributeFieldSchema === null) { |
| 60 | // SchemaField属性がないものは対象外 |
| 61 | continue; |
| 62 | } |
| 63 | $fieldLoaders[$property->getName()] = $schemaFieldLoader; |
| 64 | } |
| 65 | $this->fieldLoaders = $fieldLoaders; |
| 66 | } |
| 67 | } |