Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
26 / 26 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
1 / 1 |
| PublicationFieldTypes | |
100.00% |
26 / 26 |
|
100.00% |
9 / 9 |
9 | |
100.00% |
1 / 1 |
| rules | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| collect | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| values | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| names | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getRules | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| canonicable | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| arrayable | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| isCanonicable | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isArrayable | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Hyde\Publications\Concerns; |
| 6 | |
| 7 | use Illuminate\Support\Collection; |
| 8 | |
| 9 | use function collect; |
| 10 | use function in_array; |
| 11 | |
| 12 | /** |
| 13 | * The supported field types for publication types. |
| 14 | * |
| 15 | * @see \Hyde\Publications\Models\PublicationFieldDefinition |
| 16 | * @see \Hyde\Publications\Testing\Feature\PublicationFieldTypesEnumTest |
| 17 | */ |
| 18 | enum PublicationFieldTypes: string |
| 19 | { |
| 20 | case String = 'string'; |
| 21 | case Datetime = 'datetime'; |
| 22 | case Boolean = 'boolean'; |
| 23 | case Integer = 'integer'; |
| 24 | case Float = 'float'; |
| 25 | case Array = 'array'; |
| 26 | case Media = 'media'; |
| 27 | case Text = 'text'; |
| 28 | /** @deprecated May be renamed to Tags to better fit usage */ |
| 29 | case Tag = 'tag'; |
| 30 | case Url = 'url'; |
| 31 | |
| 32 | /** Get the default validation rules for this field type. */ |
| 33 | public function rules(): array |
| 34 | { |
| 35 | return self::getRules($this); |
| 36 | } |
| 37 | |
| 38 | /** @return Collection<array-key, self> */ |
| 39 | public static function collect(): Collection |
| 40 | { |
| 41 | return collect(self::cases()); |
| 42 | } |
| 43 | |
| 44 | public static function values(): array |
| 45 | { |
| 46 | return self::collect()->pluck('value')->toArray(); |
| 47 | } |
| 48 | |
| 49 | public static function names(): array |
| 50 | { |
| 51 | return self::collect()->pluck('name')->toArray(); |
| 52 | } |
| 53 | |
| 54 | /** Get the default validation rules for a field type. */ |
| 55 | public static function getRules(self $type): array |
| 56 | { |
| 57 | /** @noinspection PhpDuplicateMatchArmBodyInspection */ |
| 58 | return match ($type) { |
| 59 | self::String => ['string'], |
| 60 | self::Datetime => ['date'], |
| 61 | self::Boolean => ['boolean'], |
| 62 | self::Integer => ['integer'], |
| 63 | self::Float => ['numeric'], |
| 64 | self::Array => ['array'], |
| 65 | self::Media => ['string'], |
| 66 | self::Text => ['string'], |
| 67 | self::Tag => [], |
| 68 | self::Url => ['url'], |
| 69 | }; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * The types that can be used for canonical fields (used to generate file names). |
| 74 | * |
| 75 | * @return \Hyde\Publications\Concerns\PublicationFieldTypes[] |
| 76 | */ |
| 77 | public static function canonicable(): array |
| 78 | { |
| 79 | return [ |
| 80 | self::String, |
| 81 | self::Datetime, |
| 82 | self::Integer, |
| 83 | self::Text, |
| 84 | ]; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * The types that can be array values. |
| 89 | * |
| 90 | * @return \Hyde\Publications\Concerns\PublicationFieldTypes[] |
| 91 | */ |
| 92 | public static function arrayable(): array |
| 93 | { |
| 94 | return [ |
| 95 | self::Array, |
| 96 | self::Tag, |
| 97 | ]; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @return bool Can the field type be used for canonical fields? |
| 102 | */ |
| 103 | public function isCanonicable(): bool |
| 104 | { |
| 105 | return in_array($this, self::canonicable()); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @return bool Does the field type support arrays? |
| 110 | */ |
| 111 | public function isArrayable(): bool |
| 112 | { |
| 113 | return in_array($this, self::arrayable()); |
| 114 | } |
| 115 | } |