Passed
Pull Request — master (#1)
by
unknown
02:07
created

Strings::checkIfScalarAndConvert()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
nc 2
nop 1
dl 0
loc 4
c 0
b 0
f 0
cc 2
rs 10
1
<?php
2
3
namespace TraderInteractive\Filter;
4
5
use TraderInteractive\Exceptions\FilterException;
6
7
/**
8
 * A collection of filters for strings.
9
 */
10
final class Strings
11
{
12
    /**
13
     * Filter a string.
14
     *
15
     * Verify that the passed in value  is a string.  By default, nulls are not allowed, and the length is restricted
16
     * between 1 and PHP_INT_MAX.  These parameters can be overwritten for custom behavior.
17
     *
18
     * The return value is the string, as expected by the \TraderInteractive\Filterer class.
19
     *
20
     * @param mixed $value The value to filter.
21
     * @param bool $allowNull True to allow nulls through, and false (default) if nulls should not be allowed.
22
     * @param int $minLength Minimum length to allow for $value.
23
     * @param int $maxLength Maximum length to allow for $value.
24
     * @return string|null The passed in $value.
25
     *
26
     * @throws FilterException if the value did not pass validation.
27
     * @throws \InvalidArgumentException if one of the parameters was not correctly typed.
28
     */
29
    public static function filter(
30
        $value = null,
31
        bool $allowNull = false,
32
        int $minLength = 1,
33
        int $maxLength = PHP_INT_MAX
34
    ) {
35
        self::validateMinimumLength($minLength);
36
        self::validateMaximumLength($maxLength);
37
38
        if (self::valueIsNullAndValid($allowNull, $value)) {
39
            return null;
40
        }
41
42
        self::checkIfScalarAndConvert($value);
43
        self::checkIfObjectAndConvert($value);
44
        self::validateIfObjectIsAString($value);
45
        self::validateStringLength($value, $minLength, $maxLength);
46
47
        return $value;
48
    }
49
50
    /**
51
     * Explodes a string into an array using the given delimiter.
52
     *
53
     * For example, given the string 'foo,bar,baz', this would return the array ['foo', 'bar', 'baz'].
54
     *
55
     * @param string $value The string to explode.
56
     * @param string $delimiter The non-empty delimiter to explode on.
57
     * @return array The exploded values.
58
     *
59
     * @throws \InvalidArgumentException if the delimiter does not pass validation.
60
     */
61
    public static function explode($value, string $delimiter = ',')
62
    {
63
        self::validateIfObjectIsAString($value);
64
65
        if (empty($delimiter)) {
66
            throw new \InvalidArgumentException(
67
                "Delimiter '" . var_export($delimiter, true) . "' is not a non-empty string"
68
            );
69
        }
70
71
        return explode($delimiter, $value);
72
    }
73
74
    private static function validateMinimumLength(int $minLength)
75
    {
76
        if ($minLength < 0) {
77
            throw new \InvalidArgumentException('$minLength was not a positive integer value');
78
        }
79
    }
80
81
    private static function validateMaximumLength(int $maxLength)
82
    {
83
        if ($maxLength < 0) {
84
            throw new \InvalidArgumentException('$maxLength was not a positive integer value');
85
        }
86
    }
87
88
    private static function validateStringLength(string $value = null, int $minLength, int $maxLength)
89
    {
90
        $valueLength = strlen($value);
91
        if ($valueLength < $minLength || $valueLength > $maxLength) {
92
            throw new FilterException(
93
                sprintf(
94
                    "Value '%s' with length '%d' is less than '%d' or greater than '%d'",
95
                    $value,
96
                    $valueLength,
97
                    $minLength,
98
                    $maxLength
99
                )
100
            );
101
        }
102
    }
103
104
    private static function valueIsNullAndValid(bool $allowNull, $value = null) : bool
105
    {
106
        if ($allowNull === false && $value === null) {
107
            throw new FilterException('Value failed filtering, $allowNull is set to false');
108
        }
109
110
        return $allowNull === true && $value === null;
111
    }
112
113
    private static function checkIfScalarAndConvert(&$value)
114
    {
115
        if (is_scalar($value)) {
116
            $value = (string)$value;
117
        }
118
    }
119
120
    private static function checkIfObjectAndConvert(&$value)
121
    {
122
        if (is_object($value) && method_exists($value, '__toString')) {
123
            $value = (string)$value;
124
        }
125
    }
126
127
    private static function validateIfObjectIsAString($value)
128
    {
129
        if (!is_string($value)) {
130
            throw new FilterException("Value '" . var_export($value, true) . "' is not a string");
131
        }
132
    }
133
}
134