Casts   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 65
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A toInteger() 0 6 2
A toFloat() 0 6 2
A toBoolean() 0 9 3
A toArray() 0 4 1
A toDefault() 0 6 3
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Caridea
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
 * use this file except in compliance with the License. You may obtain a copy of
8
 * the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 * License for the specific language governing permissions and limitations under
16
 * the License.
17
 *
18
 * @copyright 2015-2018 LibreWorks contributors
19
 * @license   Apache-2.0
20
 */
21
namespace Caridea\Filter;
22
23
/**
24
 * Turn that mush into muscle!
25
 */
26
class Casts
27
{
28
    private static $truthy = ['yes', 'y', 'on', '1', 'true', 't'];
29
30
    /**
31
     * Creates a filter that casts values to booleans.
32
     *
33
     * @return \Closure The created filter
34
     */
35
    public static function toBoolean(): \Closure
36
    {
37 1
        return function ($value) {
38 1
            return is_bool($value) ? $value :
39 1
                (is_scalar($value) ?
40 1
                    in_array(strtolower(trim(Strings::coerce($value))), Casts::$truthy, true)
41 1
                    : false);
42 1
        };
43
    }
44
45
    /**
46
     * Creates a filter that casts values to integers.
47
     *
48
     * @return \Closure The created filter
49
     */
50
    public static function toInteger(): \Closure
51
    {
52 1
        return function ($value) {
53 1
            return (int)(is_scalar($value) ? $value : Strings::coerce($value));
54 1
        };
55
    }
56
57
    /**
58
     * Creates a filter that casts values to floats.
59
     *
60
     * @return \Closure The created filter
61
     */
62
    public static function toFloat(): \Closure
63
    {
64 1
        return function ($value) {
65 1
            return (float)(is_scalar($value) ? $value : Strings::coerce($value));
66 1
        };
67
    }
68
69
    /**
70
     * Creates a filter that casts values to arrays.
71
     *
72
     * @return callable The created filter
73
     */
74 1
    public static function toArray(): callable
75
    {
76 1
        return [Arrays::class, 'coerce'];
77
    }
78
79
    /**
80
     * Creates a filter that casts values to a default if empty.
81
     *
82
     * @return \Closure The created filter
83
     */
84
    public static function toDefault($default = null): \Closure
85
    {
86 1
        return function ($value) use ($default) {
87 1
            return $value === null || $value === '' ? $default : $value;
88 1
        };
89
    }
90
}
91