CheckGroupOptionsStep   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 16
dl 0
loc 56
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A checkValue() 0 20 6
A __construct() 0 3 1
A process() 0 11 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jtotty\Steps;
6
7
use Port\Steps\Step;
8
9
class CheckGroupOptionsStep implements Step
10
{
11
    /**
12
     * @var Array
13
     */
14
    private $options;
15
16
    /**
17
     * @param array $options
18
     */
19
    public function __construct(array $options = null)
20
    {
21
        $this->options = $options;
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function process($item, callable $next)
28
    {
29
        if ($this->options !== null) {
30
            foreach ($this->options as $option) {
31
                if (array_key_exists($option, $item)) {
32
                    $item[$option] = $this->checkValue($item[$option]);
33
                }
34
            }
35
        }
36
37
        return $next($item);
38
    }
39
40
    /**
41
     * Converts the attribute to the appropriate String value 'T' or 'F'.
42
     *
43
     * @param $attribute
44
     */
45
    public function checkValue($attribute)
46
    {
47
        // Catch if value null
48
        if (is_null($attribute)) {
49
            return 'F';
50
        }
51
52
        if (is_string($attribute)) {
53
            $attribute = trim($attribute); // remove whitespace
54
55
            // Catch lowercase 't' or 'f'
56
            // Catch whole words 'True' or 'False'
57
            $firstChar = substr($attribute, 0, 1);
58
            if ($firstChar === 't' || $firstChar === 'f') {
59
                return strtoupper($firstChar);
60
            }
61
        }
62
63
        // Not 'T' or 'F' OR not string - boolean filter validation
64
        return filter_var($attribute, FILTER_VALIDATE_BOOLEAN) ? 'T' : 'F';
65
    }
66
}
67