Completed
Push — develop ( 2df407...de4379 )
by Neomerx
06:41 queued 04:46
created

StringArrayToIntArray   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B execute() 0 20 6
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 StringArrayToIntArray 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
     */
37 1
    public static function execute($value, ContextInterface $context): array
38
    {
39 1
        $reply = null;
40
41 1
        $result = [];
42 1
        if (is_array($value) === true) {
43 1
            foreach ($value as $key => $mightBeString) {
44 1
                if (is_string($mightBeString) === true || is_numeric($mightBeString) === true) {
45 1
                    $result[$key] = (int)$mightBeString;
46
                } else {
47 1
                    $reply = static::createErrorReply($context, $mightBeString, ErrorCodes::IS_STRING);
48 1
                    break;
49
                }
50
            }
51
        } else {
52 1
            $reply = static::createErrorReply($context, $value, ErrorCodes::IS_ARRAY);
53
        }
54
55 1
        return $reply !== null ? $reply : static::createSuccessReply($result);
56
    }
57
}
58