Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
11 / 11
CRAP
100.00% covered (success)
100.00%
1 / 1
ParsesPublicationFieldInputs
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
11 / 11
15
100.00% covered (success)
100.00%
1 / 1
 parseStringValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 parseDatetimeValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 parseBooleanValue
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 parseIntegerValue
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 parseFloatValue
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 parseMediaValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 parseArrayValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 parseTextValue
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 parseUrlValue
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 parseTagValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 parseError
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Hyde\Publications\Concerns;
6
7use DateTime;
8use InvalidArgumentException;
9
10use function filter_var;
11use function is_numeric;
12use function substr_count;
13use function trim;
14use function ucfirst;
15
16/**
17 * @internal Single-use trait for the PublicationFieldValue class.
18 *
19 * @see \Hyde\Publications\Models\PublicationFieldValue
20 */
21trait ParsesPublicationFieldInputs
22{
23    protected static function parseStringValue(string $value): string
24    {
25        return $value;
26    }
27
28    protected static function parseDatetimeValue(string $value): DateTime
29    {
30        return new DateTime($value);
31    }
32
33    protected static function parseBooleanValue(string $value): bool
34    {
35        return match ($value) {
36            'true', '1' => true,
37            'false', '0' => false,
38            default => throw self::parseError('boolean', $value)
39        };
40    }
41
42    protected static function parseIntegerValue(string $value): int
43    {
44        if (! is_numeric($value)) {
45            throw self::parseError('integer', $value);
46        }
47
48        return (int) $value;
49    }
50
51    protected static function parseFloatValue(string $value): float
52    {
53        if (! is_numeric($value)) {
54            throw self::parseError('float', $value);
55        }
56
57        return (float) $value;
58    }
59
60    protected static function parseMediaValue(string $value): string
61    {
62        return $value;
63    }
64
65    protected static function parseArrayValue(string|array $value): array
66    {
67        return (array) $value;
68    }
69
70    protected static function parseTextValue(string $value): string
71    {
72        // In order to properly store multi-line text fields as block literals,
73        // we need to make sure the string ends with a newline character.
74        if (substr_count($value, "\n") > 0) {
75            return trim($value, "\r\n")."\n";
76        }
77
78        return $value;
79    }
80
81    protected static function parseUrlValue(string $value): string
82    {
83        if (! filter_var($value, FILTER_VALIDATE_URL)) {
84            throw self::parseError('url', $value);
85        }
86
87        return $value;
88    }
89
90    protected static function parseTagValue(string|array $value): array
91    {
92        return (array) $value;
93    }
94
95    protected static function parseError(string $typeName, string $input): InvalidArgumentException
96    {
97        return new InvalidArgumentException(ucfirst("{$typeName}Field: Unable to parse invalid $typeName value '$input'"));
98    }
99}