RulesParser::getAttributes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Yeelight\Generators\Migrations;
4
5
use Illuminate\Contracts\Support\Arrayable;
6
7
/**
8
 * Class RulesParser
9
 *
10
 * @category Yeelight
11
 *
12
 * @package Yeelight\Generators\Migrations
13
 *
14
 * @author Sheldon Lee <[email protected]>
15
 *
16
 * @license https://opensource.org/licenses/MIT MIT
17
 *
18
 * @link https://www.yeelight.com
19
 */
20
class RulesParser implements Arrayable
21
{
22
    /**
23
     * The set of rules.
24
     *
25
     * @var string
26
     */
27
    protected $rules;
28
29
    /**
30
     * Create new instance.
31
     *
32
     * @param string|null $rules Rules
33
     */
34
    public function __construct($rules = null)
35
    {
36
        $this->rules = $rules;
37
    }
38
39
    /**
40
     * Convert string migration to array.
41
     *
42
     * @return array
43
     */
44
    public function toArray()
45
    {
46
        return $this->parse($this->rules);
47
    }
48
49
    /**
50
     * Parse a string to array of formatted rules.
51
     *
52
     * @param string $rules Rules
53
     *
54
     * @return array
55
     */
56
    public function parse($rules)
57
    {
58
        $this->rules = $rules;
59
        $parsed = [];
60
        foreach ($this->getRules() as $rulesArray) {
61
            $column = $this->getColumn($rulesArray);
62
            $attributes = $this->getAttributes($column, $rulesArray);
63
            $parsed[$column] = $attributes;
64
        }
65
66
        return $parsed;
67
    }
68
69
    /**
70
     * Get array of rules.
71
     *
72
     * @return array
73
     */
74
    public function getRules()
75
    {
76
        if (is_null($this->rules)) {
0 ignored issues
show
introduced by
The condition is_null($this->rules) is always false.
Loading history...
77
            return [];
78
        }
79
80
        return explode(',', str_replace(' ', '', $this->rules));
81
    }
82
83
    /**
84
     * Get column name from rules.
85
     *
86
     * @param string $rules Rules
87
     *
88
     * @return string
89
     */
90
    public function getColumn($rules)
91
    {
92
        return array_first(
93
            explode('=>', $rules),
94
            function ($key/*, $value*/) {
95
                return $key;
96
            }
97
        );
98
    }
99
100
    /**
101
     * Get column attributes.
102
     *
103
     * @param string $column column
104
     * @param string $rules rules
105
     *
106
     * @return array
107
     */
108
    public function getAttributes($column, $rules)
109
    {
110
        return str_replace($column.'=>', '', $rules);
0 ignored issues
show
Bug Best Practice introduced by
The expression return str_replace($column . '=>', '', $rules) returns the type string which is incompatible with the documented return type array.
Loading history...
111
    }
112
}
113