Condition::isEmpty()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace TKuni\PhpNormalizer;
4
5
use TKuni\PhpNormalizer\Filters\BooleanFilter;
6
use TKuni\PhpNormalizer\Filters\ClosureFilter;
7
use TKuni\PhpNormalizer\Filters\ConditionalFilter;
8
use TKuni\PhpNormalizer\Filters\FloatFilter;
9
use TKuni\PhpNormalizer\Filters\IntegerFilter;
10
use TKuni\PhpNormalizer\Filters\Contracts\FilterContract;
11
use TKuni\PhpNormalizer\Filters\StringFilter;
12
use TKuni\PhpNormalizer\Filters\ToNullFilter;
13
use TKuni\PhpNormalizer\Filters\ToValueFilter;
14
15
class Condition
16
{
17
    /**
18
     * @var null|\Closure
19
     */
20
    private $condition;
21
22 10
    public function __construct($condition)
23
    {
24 10
        $this->condition = $condition;
25 10
    }
26
27 2
    public static function is($value) {
28 2
        if (is_callable($value)) {
29 1
            return new Condition($value);
30
        } else {
31
            return new Condition(function($in) use ($value) {
32 1
                return $in == $value;
33 1
            });
34
        }
35
    }
36
37 2
    public static function isEmpty() {
38
        return new Condition(function($in) {
39 2
            return empty($in);
40 2
        });
41
    }
42
43
    public static function isNotEmpty() {
44
        return new Condition(function($in) {
45
            return !empty($in);
46
        });
47
    }
48
49 1
    public static function isNotNull() {
50
        return new Condition(function($in) {
51 1
            return !is_null($in);
52 1
        });
53
    }
54
55 4
    public static function isAny() {
56 4
        return new Condition(null);
57
    }
58
59 1
    public static function isContains($text) {
60
        return new Condition(function($in) use ($text) {
61 1
            return strpos($in, $text) !== false;
62 1
        });
63
    }
64
65 1
    public static function isRegexp($regexp) {
66
        return new Condition(function($in) use ($regexp) {
67 1
            return preg_match($regexp, $in);
68 1
        });
69
    }
70
71 8
    public function to($value) {
72 8
        if (is_callable($value)) {
73 1
            return new ConditionalFilter($this->condition, new ClosureFilter($value));
74 7
        } else if (is_object($value) && $value instanceof FilterContract) {
75 1
            return new ConditionalFilter($this->condition, $value);
76
        } else {
77 6
            return new ConditionalFilter($this->condition, new ToValueFilter($value));
78
        }
79
    }
80
81 1
    public function toNull() {
82 1
        return new ConditionalFilter($this->condition, new ToNullFilter());
83
    }
84
85 2
    public function toInt() {
86 2
        return new ConditionalFilter($this->condition, new IntegerFilter());
87
    }
88
89 1
    public function toFloat() {
90 1
        return new ConditionalFilter($this->condition, new FloatFilter());
91
    }
92
93 1
    public function toBoolean() {
94 1
        return new ConditionalFilter($this->condition, new BooleanFilter());
95
    }
96
97 1
    public function toString() {
98 1
        return new ConditionalFilter($this->condition, new StringFilter());
99
    }
100
}