Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
21 / 21 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| Registry | |
100.00% |
21 / 21 |
|
100.00% |
3 / 3 |
3 | |
100.00% |
1 / 1 |
| register | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getHandler | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| withDefaults | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Cosray\Field\Schema; |
| 6 | |
| 7 | use Cosray\Schema\Allows; |
| 8 | use Cosray\Schema\Columns; |
| 9 | use Cosray\Schema\DefaultValue; |
| 10 | use Cosray\Schema\Description; |
| 11 | use Cosray\Schema\Fulltext; |
| 12 | use Cosray\Schema\Hidden; |
| 13 | use Cosray\Schema\Icon; |
| 14 | use Cosray\Schema\Immutable; |
| 15 | use Cosray\Schema\Label; |
| 16 | use Cosray\Schema\Limit; |
| 17 | use Cosray\Schema\Options; |
| 18 | use Cosray\Schema\Required; |
| 19 | use Cosray\Schema\Rows; |
| 20 | use Cosray\Schema\Syntax; |
| 21 | use Cosray\Schema\Translate; |
| 22 | use Cosray\Schema\Validate; |
| 23 | use Cosray\Schema\Width; |
| 24 | |
| 25 | class Registry |
| 26 | { |
| 27 | /** @var array<class-string, Handler> */ |
| 28 | private array $handlers = []; |
| 29 | |
| 30 | /** @param class-string $schema */ |
| 31 | public function register(string $schema, Handler $handler): void |
| 32 | { |
| 33 | $this->handlers[$schema] = $handler; |
| 34 | } |
| 35 | |
| 36 | public function getHandler(object $schema): ?Handler |
| 37 | { |
| 38 | return $this->handlers[$schema::class] ?? null; |
| 39 | } |
| 40 | |
| 41 | public static function withDefaults(): self |
| 42 | { |
| 43 | $registry = new self(); |
| 44 | $registry->register(Allows::class, new AllowsHandler()); |
| 45 | $registry->register(Label::class, new LabelHandler()); |
| 46 | $registry->register(Icon::class, new IconHandler()); |
| 47 | $registry->register(Description::class, new DescriptionHandler()); |
| 48 | $registry->register(Translate::class, new TranslateHandler()); |
| 49 | $registry->register(Required::class, new RequiredHandler()); |
| 50 | $registry->register(Validate::class, new ValidateHandler()); |
| 51 | $registry->register(DefaultValue::class, new DefaultValueHandler()); |
| 52 | $registry->register(Width::class, new WidthHandler()); |
| 53 | $registry->register(Rows::class, new RowsHandler()); |
| 54 | $registry->register(Columns::class, new ColumnsHandler()); |
| 55 | $registry->register(Hidden::class, new HiddenHandler()); |
| 56 | $registry->register(Immutable::class, new ImmutableHandler()); |
| 57 | $registry->register(Options::class, new OptionsHandler()); |
| 58 | $registry->register(Limit::class, new LimitHandler()); |
| 59 | $registry->register(Fulltext::class, new FulltextHandler()); |
| 60 | $registry->register(Syntax::class, new SyntaxHandler()); |
| 61 | |
| 62 | return $registry; |
| 63 | } |
| 64 | } |