Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
14 / 14 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| PublicationFieldValue | |
100.00% |
14 / 14 |
|
100.00% |
4 / 4 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| parseFieldValue | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Hyde\Publications\Models; |
| 6 | |
| 7 | use DateTime; |
| 8 | use Hyde\Publications\Concerns\PublicationFieldTypes; |
| 9 | use Hyde\Publications\Concerns\ParsesPublicationFieldInputs; |
| 10 | |
| 11 | /** |
| 12 | * Represents a single value for a field in a publication's front matter, |
| 13 | * following rules defined in the "fields" array of the publication type's schema. |
| 14 | * |
| 15 | * @see \Hyde\Publications\Models\PublicationFieldDefinition |
| 16 | * @see \Hyde\Publications\Concerns\PublicationFieldTypes |
| 17 | * @see \Hyde\Publications\Testing\Feature\PublicationFieldValueTest |
| 18 | */ |
| 19 | final class PublicationFieldValue |
| 20 | { |
| 21 | use ParsesPublicationFieldInputs; |
| 22 | |
| 23 | public readonly PublicationFieldTypes $type; |
| 24 | protected string|array|bool|float|int|DateTime $value; |
| 25 | |
| 26 | public function __construct(PublicationFieldTypes $type, string|array $value) |
| 27 | { |
| 28 | $this->type = $type; |
| 29 | |
| 30 | $this->value = self::parseFieldValue($type, $value); |
| 31 | } |
| 32 | |
| 33 | public function getType(): PublicationFieldTypes |
| 34 | { |
| 35 | return $this->type; |
| 36 | } |
| 37 | |
| 38 | public function getValue(): string|array|bool|float|int|DateTime |
| 39 | { |
| 40 | return $this->value; |
| 41 | } |
| 42 | |
| 43 | /** Parse an input string from the command line into a value with the appropriate type for the field. */ |
| 44 | public static function parseFieldValue(PublicationFieldTypes $fieldType, string|array $value): string|array|bool|float|int|DateTime |
| 45 | { |
| 46 | return match ($fieldType) { |
| 47 | PublicationFieldTypes::String => self::parseStringValue($value), |
| 48 | PublicationFieldTypes::Datetime => self::parseDatetimeValue($value), |
| 49 | PublicationFieldTypes::Boolean => self::parseBooleanValue($value), |
| 50 | PublicationFieldTypes::Integer => self::parseIntegerValue($value), |
| 51 | PublicationFieldTypes::Float => self::parseFloatValue($value), |
| 52 | PublicationFieldTypes::Media => self::parseMediaValue($value), |
| 53 | PublicationFieldTypes::Array => self::parseArrayValue($value), |
| 54 | PublicationFieldTypes::Text => self::parseTextValue($value), |
| 55 | PublicationFieldTypes::Url => self::parseUrlValue($value), |
| 56 | PublicationFieldTypes::Tag => self::parseTagValue($value), |
| 57 | }; |
| 58 | } |
| 59 | } |