Failed Conditions
Pull Request — master (#1)
by
unknown
02:02
created

Strings::filter()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 20
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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