PublicationFieldTypes::canonicable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
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());
0 ignored issues
show
Bug introduced by
self::cases() of type array<integer,Hyde\Publi...\PublicationFieldTypes> is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
        return collect(/** @scrutinizer ignore-type */ self::cases());
Loading history...
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 => [],
0 ignored issues
show
Deprecated Code introduced by
The constant Hyde\Publications\Concer...licationFieldTypes::Tag has been deprecated: May be renamed to Tags to better fit usage ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

67
            /** @scrutinizer ignore-deprecated */ self::Tag => [],

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
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,
0 ignored issues
show
Deprecated Code introduced by
The constant Hyde\Publications\Concer...licationFieldTypes::Tag has been deprecated: May be renamed to Tags to better fit usage ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

96
            /** @scrutinizer ignore-deprecated */ self::Tag,

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
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
}
116