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

Strings::explode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 9
rs 9.6666
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($value, bool $allowNull = false, int $minLength = 1, int $maxLength = PHP_INT_MAX)
30
    {
31
        if ($minLength < 0) {
32
            throw new \InvalidArgumentException('$minLength was not a positive integer value');
33
        }
34
35
        if ($maxLength < 0) {
36
            throw new \InvalidArgumentException('$maxLength was not a positive integer value');
37
        }
38
39
        if ($allowNull === true && $value === null) {
40
            return null;
41
        }
42
43
        if (is_scalar($value)) {
44
            $value = "{$value}";
45
        }
46
47
        if (is_object($value) && method_exists($value, '__toString')) {
48
            $value = (string)$value;
49
        }
50
51
        if (!is_string($value)) {
52
            throw new FilterException("Value '" . var_export($value, true) . "' is not a string");
53
        }
54
55
        $valueLength = strlen($value);
56
57
        if ($valueLength < $minLength || $valueLength > $maxLength) {
58
            throw new FilterException(
59
                sprintf(
60
                    "Value '%s' with length '%d' is less than '%d' or greater than '%d'",
61
                    $value,
62
                    $valueLength,
63
                    $minLength,
64
                    $maxLength
65
                )
66
            );
67
        }
68
69
        return $value;
70
    }
71
72
    /**
73
     * Explodes a string into an array using the given delimiter.
74
     *
75
     * For example, given the string 'foo,bar,baz', this would return the array ['foo', 'bar', 'baz'].
76
     *
77
     * @param string $value The string to explode.
78
     * @param string $delimiter The non-empty delimiter to explode on.
79
     * @return array The exploded values.
80
     *
81
     * @throws \InvalidArgumentException if the delimiter does not pass validation.
82
     */
83
    public static function explode(string $value, string $delimiter = ',')
84
    {
85
        if (empty($delimiter)) {
86
            throw new \InvalidArgumentException(
87
                "Delimiter '" . var_export($delimiter, true) . "' is not a non-empty string"
88
            );
89
        }
90
91
        return explode($delimiter, $value);
92
    }
93
}
94