Passed
Push — master ( 2a3d19...5e9e4e )
by Caen
05:36 queued 13s
created

ParsesPublicationFieldInputs   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 77
rs 10
c 0
b 0
f 0
wmc 15

11 Methods

Rating   Name   Duplication   Size   Complexity  
A parseError() 0 3 1
A parseTagValue() 0 3 1
A parseBooleanValue() 0 6 1
A parseStringValue() 0 3 1
A parseFloatValue() 0 7 2
A parseMediaValue() 0 3 1
A parseIntegerValue() 0 7 2
A parseDatetimeValue() 0 3 1
A parseUrlValue() 0 7 2
A parseTextValue() 0 9 2
A parseArrayValue() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Publications\Concerns;
6
7
use DateTime;
8
use InvalidArgumentException;
9
10
use function filter_var;
11
use function is_numeric;
12
use function substr_count;
13
use function trim;
14
use function ucfirst;
15
16
/**
17
 * @internal Single-use trait for the PublicationFieldValue class.
18
 *
19
 * @see \Hyde\Publications\Models\PublicationFieldValue
20
 */
21
trait 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
}
100