Completed
Pull Request — master (#71)
by Felix
02:04
created

RuleParser::parseFieldData()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 34
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.439
c 0
b 0
f 0
cc 5
eloc 15
nc 7
nop 0
1
<?php namespace Felixkiss\UniqueWithValidator;
2
3
class RuleParser
4
{
5
    protected $table;
6
    protected $primaryField;
7
    protected $primaryValue;
8
    protected $additionalFields;
9
    protected $ignoreColumn;
10
    protected $ignoreValue;
11
    protected $dataFields;
12
13
    protected $parameters;
14
    protected $data;
15
16
    protected $parsed = false;
17
18
    public function __construct($attribute = null, $value = null, array $parameters = [], array $data = [])
19
    {
20
        $this->primaryField = $attribute;
21
        $this->primaryValue = $value;
22
        $this->parameters = $parameters;
23
        $this->data = $data;
24
    }
25
26
    protected function parse()
27
    {
28
        if ($this->parsed) { return; }
29
        $this->parsed = true;
30
31
        // cleaning: trim whitespace
32
        $this->parameters = array_map('trim', $this->parameters);
33
34
        // first item equals table name
35
        $this->table = array_shift($this->parameters);
36
37
        // Check if ignore data is set
38
        $this->parseIgnore();
39
40
        // Parse field data
41
        $this->parseFieldData();
42
    }
43
44
    protected function parseFieldData()
45
    {
46
        $this->additionalFields = [];
47
        $this->dataFields = [$this->primaryField];
48
49
        // Figure out whether field_name is the same as column_name
50
        // or column_name is explicitly specified.
51
        //
52
        // case 1:
53
        //     $parameter = 'last_name'
54
        //     => field_name = column_name = 'last_name'
55
        // case 2:
56
        //     $parameter = 'last_name=sur_name'
57
        //     => field_name = 'last_name', column_name = 'sur_name'
58
        foreach ($this->parameters as $parameter) {
59
            $parts = array_map('trim', explode('=', $parameter, 2));
60
            $fieldName = $parts[0];
61
            $columnName = count($parts) > 1 ? $parts[1] : $fieldName;
62
            $this->dataFields[] = $fieldName;
63
64
            if ($fieldName === $this->primaryField) {
65
                $this->primaryField = $columnName;
66
                continue;
67
            }
68
69
            if (!array_key_exists($fieldName, $this->data)) {
70
                continue;
71
            }
72
73
            $this->additionalFields[$columnName] = $this->data[$fieldName];
74
        }
75
76
        $this->dataFields = array_values(array_unique($this->dataFields));
77
    }
78
79
    public function getTable()
80
    {
81
        $this->parse();
82
        return $this->table;
83
    }
84
85
    public function getPrimaryField()
86
    {
87
        $this->parse();
88
        return $this->primaryField;
89
    }
90
91
    public function getPrimaryValue()
92
    {
93
        $this->parse();
94
        return $this->primaryValue;
95
    }
96
97
    public function getAdditionalFields()
98
    {
99
        $this->parse();
100
        return $this->additionalFields;
101
    }
102
103
    public function getIgnoreValue()
104
    {
105
        $this->parse();
106
        return $this->ignoreValue;
107
    }
108
109
    public function getIgnoreColumn()
110
    {
111
        $this->parse();
112
        return $this->ignoreColumn;
113
    }
114
115
    public function getDataFields()
116
    {
117
        $this->parse();
118
        return $this->dataFields;
119
    }
120
121
    protected function parseIgnore()
122
    {
123
        // Ignore has to be specified as the last parameter
124
        $lastParameter = end($this->parameters);
125
        if (!$this->isIgnore($lastParameter)) { return; }
126
127
        $lastParameter = array_map('trim', explode('=', $lastParameter));
128
129
        $this->ignoreValue = str_replace('ignore:', '', $lastParameter[0]);
130
        $this->ignoreColumn = (sizeof($lastParameter) > 1) ? end($lastParameter) : null;
131
132
        // Shave of the ignore_id from the array for later processing
133
        array_pop($this->parameters);
134
    }
135
136
    protected function isIgnore($parameter)
137
    {
138
        // An ignore_id can be specified by prefixing with 'ignore:'
139
        if (strpos($parameter, 'ignore:') !== false) {
140
            return true;
141
        }
142
143
        // An ignore_id can be specified if parameter starts with a
144
        // number greater than 1 (a valid id in the database)
145
        $parts = array_map('trim', explode('=', $parameter));
146
        return preg_match('/^[1-9][0-9]*$/', $parts[0]);
147
    }
148
}
149