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

Strings::validateStringLength()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
nc 2
nop 3
dl 0
loc 11
c 0
b 0
f 0
cc 3
rs 9.4285
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
        $value = self::castObjectToValueIfApplicable($value);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $value of TraderInteractive\Filter...ctToValueIfApplicable() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
        $value = self::castObjectToValueIfApplicable(/** @scrutinizer ignore-type */ $value);
Loading history...
47
48
        if (!is_string($value)) {
0 ignored issues
show
introduced by
The condition ! is_string($value) can never be true.
Loading history...
49
            throw new FilterException("Value '" . var_export($value, true) . "' is not a string");
50
        }
51
52
        self::validateStringLength($value, $minLength, $maxLength);
53
54
        return $value;
55
    }
56
57
    /**
58
     * Explodes a string into an array using the given delimiter.
59
     *
60
     * For example, given the string 'foo,bar,baz', this would return the array ['foo', 'bar', 'baz'].
61
     *
62
     * @param string $value The string to explode.
63
     * @param string $delimiter The non-empty delimiter to explode on.
64
     * @return array The exploded values.
65
     *
66
     * @throws \InvalidArgumentException if the delimiter does not pass validation.
67
     */
68
    public static function explode(string $value, string $delimiter = ',')
69
    {
70
        if (empty($delimiter)) {
71
            throw new \InvalidArgumentException(
72
                "Delimiter '" . var_export($delimiter, true) . "' is not a non-empty string"
73
            );
74
        }
75
76
        return explode($delimiter, $value);
77
    }
78
79
    private static function validateMinimumLength(int $minLength)
80
    {
81
        if ($minLength < 0) {
82
            throw new \InvalidArgumentException('$minLength was not a positive integer value');
83
        }
84
    }
85
86
    private static function validateMaximumLength(int $maxLength)
87
    {
88
        if ($maxLength < 0) {
89
            throw new \InvalidArgumentException('$maxLength was not a positive integer value');
90
        }
91
    }
92
93
    private static function castObjectToValueIfApplicable(string $value) : string
94
    {
95
        if (is_object($value) && method_exists($value, '__toString')) {
0 ignored issues
show
introduced by
The condition is_object($value) && met...s($value, '__toString') can never be true.
Loading history...
96
            $value = (string)$value;
97
        }
98
        return $value;
99
    }
100
101
    private static function validateStringLength(string $value, int $minLength, int $maxLength)
102
    {
103
        $valueLength = strlen($value);
104
        if ($valueLength < $minLength || $valueLength > $maxLength) {
105
            throw new FilterException(
106
                sprintf(
107
                    "Value '%s' with length '%d' is less than '%d' or greater than '%d'",
108
                    $value,
109
                    $valueLength,
110
                    $minLength,
111
                    $maxLength
112
                )
113
            );
114
        }
115
    }
116
}
117