Completed
Pull Request — develop (#2)
by
unknown
04:30
created

Parser::addBitwiseKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Cerbero\LaravelEnum;
4
5
use Illuminate\Support\Str;
6
7
/**
8
 * The enums parser.
9
 *
10
 */
11
class Parser
12
{
13
    const SEPARATOR_ENUM = '|';
14
    const SEPARATOR_PART = '=';
15
16
    private $key = 1;
17
18
    /**
19
     * Parse the enums definition
20
     *
21
     * @param string $definition
22
     * @param bool $numeric
23
     * @param bool $bitwise
24
     * @return array
25
     */
26 39
    public function parseDefinition(string $definition, bool $numeric = false, bool $bitwise = false) : array
27
    {
28 39
        $enums = explode(static::SEPARATOR_ENUM, $definition);
29
30
        return array_map(function (string $enum) use ($numeric, $bitwise) {
31 39
            $parts = explode(static::SEPARATOR_PART, $enum);
32
33 39
            if ($numeric) {
34 6
                $parts = $this->addNumericKey($parts);
35 33
            } elseif ($bitwise) {
36 6
                $parts = $this->addBitwiseKey($parts);
37
            }
38
39 39
            return $this->hydrateEnumDefinition($parts);
40 39
        }, $enums);
41
    }
42
43
    /**
44
     * Retrieve the hydrated enum definition
45
     *
46
     * @param array $parts
47
     * @return EnumDefinition
48
     */
49 39
    private function hydrateEnumDefinition(array $parts) : EnumDefinition
50
    {
51
        return tap(new EnumDefinition, function ($enumDefinition) use ($parts) {
52 39
            $enumDefinition->name = $parts[0];
53 39
            $enumDefinition->key = isset($parts[1]) ? $this->parseValue($parts[1]) : Str::lower($parts[0]);
54 39
            $enumDefinition->value = isset($parts[2]) ? $this->parseValue($parts[2]) : null;
55 39
        });
56
    }
57
58
    /**
59
     * Add a numeric enum key to the parts.
60
     *
61
     * @param  array  $parts
62
     * @return array
63
     */
64 6
    private function addNumericKey(array $parts)
65
    {
66 6
        array_splice($parts, 1, 0, $this->key);
67 6
        $this->key++;
68
69 6
        return $parts;
70
    }
71
72
    /**
73
     * Add a bitwise enum key to the parts.
74
     *
75
     * @param  array  $parts
76
     * @return array
77
     */
78 6
    private function addBitwiseKey(array $parts)
79
    {
80 6
        array_splice($parts, 1, 0, $this->key);
81 6
        $this->key *= 2;
82
83 6
        return $parts;
84
    }
85
86
    /**
87
     * Parse the given variable to retrieve its actual value
88
     *
89
     * @param mixed $value
90
     * @return mixed
91
     */
92 39
    public function parseValue($value)
93
    {
94 39
        if ($value === null || $value === '') {
95 3
            return null;
96
        }
97
98
        // Return floats, integers, booleans or arrays if possible
99 36
        if (null !== $decoded = json_decode($value, true)) {
100 33
            return $decoded;
101
        }
102
103 9
        return $value;
104
    }
105
}
106