Completed
Push — master ( 2df407...de4379 )
by Neomerx
05:05
created

StringToBool   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B execute() 0 19 11
1
<?php namespace Limoncello\Validation\Rules\Converters;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of 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,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Limoncello\Validation\Contracts\Errors\ErrorCodes;
20
use Limoncello\Validation\Contracts\Execution\ContextInterface;
21
use Limoncello\Validation\Rules\ExecuteRule;
22
23
/**
24
 * @package Limoncello\Validation
25
 */
26
final class StringToBool extends ExecuteRule
27
{
28
    /**
29
     * @param mixed            $value
30
     * @param ContextInterface $context
31
     *
32
     * @return array
33
     *
34
     * @SuppressWarnings(PHPMD.StaticAccess)
35
     * @SuppressWarnings(PHPMD.ElseExpression)
36
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
37
     */
38 2
    public static function execute($value, ContextInterface $context): array
39
    {
40 2
        if (is_string($value) === true) {
41 2
            $lcValue = strtolower($value);
42 2
            if ($lcValue === 'true' || $lcValue === '1' || $lcValue === 'on' || $lcValue === 'yes') {
43 2
                $reply = static::createSuccessReply(true);
44 2
            } elseif ($lcValue === 'false' || $lcValue === '0' || $lcValue === 'off' || $lcValue === 'no') {
45 1
                $reply = static::createSuccessReply(false);
46
            } else {
47 2
                $reply = static::createErrorReply($context, $value, ErrorCodes::IS_BOOL);
48
            }
49 1
        } elseif (is_bool($value) === true) {
50 1
            $reply = static::createSuccessReply($value);
51
        } else {
52 1
            $reply = static::createErrorReply($context, $value, ErrorCodes::IS_BOOL);
53
        }
54
55 2
        return $reply;
56
    }
57
}
58