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

Strings::validateMaximumLength()   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
        string $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::validateStringLength($value, $minLength, $maxLength);
43
44
        return $value;
45
    }
46
47
    /**
48
     * Explodes a string into an array using the given delimiter.
49
     *
50
     * For example, given the string 'foo,bar,baz', this would return the array ['foo', 'bar', 'baz'].
51
     *
52
     * @param string $value The string to explode.
53
     * @param string $delimiter The non-empty delimiter to explode on.
54
     * @return array The exploded values.
55
     *
56
     * @throws \InvalidArgumentException if the delimiter does not pass validation.
57
     */
58
    public static function explode(string $value, string $delimiter = ',')
59
    {
60
        if (empty($delimiter)) {
61
            throw new \InvalidArgumentException(
62
                "Delimiter '" . var_export($delimiter, true) . "' is not a non-empty string"
63
            );
64
        }
65
66
        return explode($delimiter, $value);
67
    }
68
69
    private static function validateMinimumLength(int $minLength)
70
    {
71
        if ($minLength < 0) {
72
            throw new \InvalidArgumentException('$minLength was not a positive integer value');
73
        }
74
    }
75
76
    private static function validateMaximumLength(int $maxLength)
77
    {
78
        if ($maxLength < 0) {
79
            throw new \InvalidArgumentException('$maxLength was not a positive integer value');
80
        }
81
    }
82
83
    private static function validateStringLength(string $value = null, int $minLength, int $maxLength)
84
    {
85
        $valueLength = strlen($value);
86
        if ($valueLength < $minLength || $valueLength > $maxLength) {
87
            throw new FilterException(
88
                sprintf(
89
                    "Value '%s' with length '%d' is less than '%d' or greater than '%d'",
90
                    $value,
91
                    $valueLength,
92
                    $minLength,
93
                    $maxLength
94
                )
95
            );
96
        }
97
    }
98
99
    private static function valueIsNullAndValid(bool $allowNull, string $value = null) : bool
100
    {
101
        if ($allowNull === false && $value === null) {
102
            throw new FilterException('Value failed filtering, $allowNull is set to false');
103
        }
104
105
        return $allowNull === true && $value === null;
106
    }
107
}
108