Passed
Push — master ( 612632...9e8075 )
by Dedipyaman
08:57
created

TextCase::isAllCaps()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of Phypes <https://github.com/2DSharp/Phypes>.
4
 *
5
 * (c) Dedipyaman Das <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Phypes\Rule\String;
12
13
use Phypes\Error\RuleError;
14
use Phypes\Error\RuleErrorCode;
15
use Phypes\Result\Failure;
16
use Phypes\Result\Result;
17
use Phypes\Result\Success;
18
use Phypes\Rule\Rule;
19
20
class TextCase implements Rule
21
{
22
    const NONE_CAPS = 0;
23
    const ALL_CAPS = 1;
24
    const MIXED = 2;
25
26
    const LEVEL_STRICT = 0;
27
    const LEVEL_TOLERANT = 1;
28
    /**
29
     * @var integer $caseType
30
     */
31
    private $caseType;
32
33
    /**
34
     * @var bool $strictCheck
35
     */
36
    private $strictCheck;
37
    /**
38
     * TextCase constructor.
39
     * @throws \InvalidArgumentException
40
     * @param int $caseType
41
     * @param bool $strictCheck
42
     */
43
    public function __construct(int $caseType, bool $strictCheck = false)
44
    {
45
        if ($caseType > 2 || $caseType < 0)
46
            throw new \InvalidArgumentException('Case Type ' . $caseType . ' is invalid. 
47
            Check the class constants available to be used as caseTypes');
48
49
        $this->caseType = $caseType;
50
        $this->strictCheck = $strictCheck;
51
    }
52
53
    /**
54
     * @param string $text
55
     * @return bool
56
     */
57
    private function isMixed(string $text) : bool
58
    {
59
        if ($this->strictCheck) {
60
            if (preg_match("/^[a-zA-Z]+$/", $text))
61
                return preg_match('/[a-z]/', $text) && preg_match('/[A-Z]/', $text);
62
            else
63
                return false;
64
        }
65
66
        return preg_match('/[a-z]/', $text) && preg_match('/[A-Z]/', $text);
67
    }
68
69
    private function isAllCaps(string $text) : bool
70
    {
71
        if ($this->strictCheck)
72
            return ctype_upper($text);
73
        else
74
            return !preg_match('/[a-z]/', $text) && preg_match('/[A-Z]/', $text);
75
            
76
    }
77
78
    private function isNoneCaps(string $text) : bool
79
    {
80
        if ($this->strictCheck)
81
            return ctype_lower($text);
82
        else
83
            return preg_match('/[a-z]/', $text) && !preg_match('/[A-Z]/', $text);
84
    }
85
86
    public function validate($data): Result
87
    {
88
        $isValid = false;
89
90
        switch ($this->caseType) {
91
            case self::MIXED:
92
                $isValid = $this->isMixed($data);
93
                break;
94
            case self::ALL_CAPS:
95
                $isValid = $this->isAllCaps($data);
96
                break;
97
98
            case self::NONE_CAPS:
99
                $isValid = $this->isNoneCaps($data);
100
                break;
101
        }
102
103
        if ($isValid)
104
            return new Success();
105
        else
106
            return new Failure(new RuleError(RuleErrorCode::CASING_MISMATCH,
107
                "The given string doesn't match the required case"));
108
    }
109
}