Passed
Branch master (27f176)
by refat
18:17
created

Characters::isObject()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace System\Validation;
4
5
class Characters
6
{
7
  private $object;
8
  private $chars;
9
  private $times;
10
  private $atFirst;
11
  private $atEnd;
12
  private $between;
13
  private $langsRegex;
14
  private $languages;
15
16
  /**
17
   * Constructor
18
   *
19
   */
20
  public function __construct($object, $value)
21
  {
22
    $this->object = $object;
23
    $this->chars = $this->object->chars->value ?? $this->object->chars->chars ?? null;
24
    $this->times = $this->object->chars->times ?? null;
25
    $this->atFirst = $this->object->chars->atFirst ?? null;
26
    $this->atEnd = $this->object->chars->atEnd ?? null;
27
    $this->between = $this->object->chars->between ?? null;
28
    $this->languages = $this->object->languages ?? 'english';
29
    $this->langsRegex = $this->object->languages ?? $this->languagesArray('english');
30
    $this->value = $value ?? null;
0 ignored issues
show
Bug Best Practice introduced by
The property value does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
31
32
    $this->setChars();
33
    $this->setLanguages();
34
  }
35
36
  private function isObject()
37
  {
38
    return is_object($this->object) && count((array) $this->object);
39
  }
40
41
  private function isCharsString()
42
  {
43
    return is_string($this->chars);
44
  }
45
46
  private function isCharsAnArray()
47
  {
48
    return is_array($this->chars);
49
  }
50
51
  private function canCharsSeparateViaComma()
52
  {
53
    return preg_match('/,/', $this->chars) && preg_match_all('/,/', $this->chars) > 1;
54
  }
55
56
  private function formatCharsViaComma($comma)
57
  {
58
    if ($comma) {
59
      $chars = explode(',', $this->chars);
60
    } else {
61
      $chars = str_split($this->chars);
62
    }
63
    return "\\" . implode('\\', $chars);
64
  }
65
66
  private function formatCharsString()
67
  {
68
    return $this->formatCharsViaComma($this->canCharsSeparateViaComma());
69
  }
70
71
  private function formatCharsArray()
72
  {
73
    return implode('', (array) $this->chars);
74
  }
75
76
  private function setChars()
77
  {
78
    if ($this->isObject()) {
79
      if ($this->isCharsString()) {
80
        $this->chars = $this->formatCharsString();
81
      } else if ($this->isCharsAnArray()) {
82
        $this->chars = $this->formatCharsArray();
83
      }
84
    }
85
  }
86
87
  private function languagesArray($language)
88
  {
89
    $languages = [
90
      'all' => '\\p{L}',
91
      'arabic' =>  '\\x{0621}-\\x{064A}\\x{0660}-\\x{0669} ُ ْ َ ِ ّ~ ً ٍ ٌ',
92
      'english' => 'a-z',
93
      'spanish' => 'a-zñ',
94
      'french' => 'a-zàâçéèêëîïôûùüÿñæœ',
95
      'german' => 'a-zäüöß',
96
    ];
97
    return $languages[$language] ?? $languages['english'];
98
  }
99
100
  private function isLangsAnArray()
101
  {
102
    return is_array($this->languages);
103
  }
104
105
  private function isLangsAnString()
106
  {
107
    return is_string($this->languages);
108
  }
109
110
  private function canlangsSeparateViaComma()
111
  {
112
    return preg_match('/,/', $this->languages) && preg_match_all('/,/', $this->languages);
113
  }
114
115
  private function loopOverLangsViaComma($comma)
116
  {
117
    $loopLangs = $comma ? explode(',', $this->languages) : $this->languages;
118
    $langsRegex = '';
119
    $languages = '';
120
    foreach ($loopLangs as $language) {
121
      $langsRegex .= $this->languagesArray(trim($language));
122
      $languages .= "$language, ";
123
    }
124
    $languages = rtrim($languages, ", ");
125
    return array('languages' => $languages, 'langsRegex' => $langsRegex);
126
  }
127
128
  private function formatLangsString()
129
  {
130
    if ($this->canlangsSeparateViaComma()) {
131
      extract($this->loopOverLangsViaComma(true));
132
    } else {
133
      $langsRegex = $this->languagesArray(trim($this->languages));
134
      $languages = $this->languages;
135
    }
136
    return array('languages' => $languages, 'langsRegex' => $langsRegex);
137
  }
138
139
  private function setLanguages()
140
  {
141
    if ($this->isLangsAnArray()) {
142
      extract($this->loopOverLangsViaComma(false));
143
    } else if ($this->isLangsAnString()) {
144
      extract($this->formatLangsString());
145
    }
146
    $this->languages = $languages;
147
    $this->langsRegex = $langsRegex;
148
    $this->formatLangsRegex();
149
  }
150
151
  private function formatLangsRegex()
152
  {
153
    if ($this->langsRegex !== 'all' && preg_match_all('/a-z/i', $this->langsRegex) > 1) {
154
      $this->langsRegex = preg_replace('/a-z/', '', $this->langsRegex) . 'a-z';
155
    }
156
  }
157
158
  public function variables()
159
  {
160
    $chars = $this->chars;
161
    $langsRegex = $this->langsRegex;
162
    $languages = $this->languages;
163
    $times = $this->times;
164
    $atFirst = $this->atFirst;
165
    $atEnd = $this->atEnd;
166
    $between = $this->between;
167
    $methods = $this->charactersMethods([
168
      "times" => $times,
169
      "atFirst" => $atFirst,
170
      "atEnd" => $atEnd,
171
      "between" => $between,
172
      "chars" => $chars,
173
      "value" => $this->value,
174
    ]);
175
    return [
176
      'chars' => $chars,
177
      'langsRegex' => $langsRegex,
178
      'languages' => $languages,
179
      'times' => $times,
180
      'atFirst' => $atFirst,
181
      'atEnd' => $atEnd,
182
      'between' => $between,
183
      'methods' => $methods,
184
    ];
185
  }
186
187
  private function charactersMethods($args)
188
  {
189
    extract($args);
190
    return [
191
      'charactersTimes' => [
192
        [$times, $chars, $value],
193
        'charachters are too many',
194
      ],
195
      'charactersAtFirst' => [
196
        [$atFirst, $chars, $value],
197
        'charachters cant be at the first',
198
      ],
199
      'charactersAtEnd' => [
200
        [$atEnd, $chars, $value],
201
        'charachters cant be at the end',
202
      ],
203
      'charactersBetween' => [
204
        [$between, $chars, $value],
205
        'charachters cant be between',
206
      ],
207
    ];
208
  }
209
210
  private function charactersFormatCharsRegex($chars)
211
  {
212
    if (strlen($chars) > 1) {
213
      $chars = str_split($chars);
214
      $chars = "\\" . implode('|\\', $chars);
215
    }
216
    return $chars;
217
  }
218
219
  public function charactersAtFirst($atFirst, $chars, $value)
220
  {
221
    if ($atFirst === false) {
222
      $chars = $this->charactersFormatCharsRegex($chars);
223
      $re = "/^($chars" . "|\\s+\\$chars)/";
224
      if (preg_match_all($re, $value)) {
225
        return true;
226
      }
227
      return false;
228
    }
229
  }
230
231
  private function charactersFormatCharsMsg($chars)
232
  {
233
    $chars = explode('\\', $chars);
234
    $chars = implode('', $chars);
235
    $chars = $chars ? "[ $chars ] and" : '';
236
    return $chars;
237
  }
238
239
  public function charactersAtEnd($atEnd, $chars, $value)
240
  {
241
    if ($atEnd === false) {
242
      $chars = $this->charactersFormatCharsRegex($chars);
243
      $re = "/($chars" . "|\\$chars\\s+)$/";
244
      if (preg_match_all($re, $value)) {
245
        return true;
246
      }
247
      return false;
248
    }
249
  }
250
251
  public function charactersBetween($between, $chars, $value)
252
  {
253
    if ($between === false) {
254
      $chars = $this->charactersFormatCharsRegex($chars);
255
      $re = "/.+(${chars})(.+|\\s)/";
256
      if (preg_match_all($re, $value)) {
257
        return true;
258
      }
259
      return false;
260
    }
261
  }
262
263
  public function charactersTimes($times, $chars, $value)
264
  {
265
    if ($times > 0) {
266
      $chars = $this->charactersFormatCharsRegex($chars);
267
      $re = "/($chars)/";
268
      if (preg_match($re, $value) && preg_match_all($re, $value) > $times) {
269
        return true;
270
      }
271
      return false;
272
    }
273
  }
274
275
  public function charactersMsg($chars, $languages, $msg)
276
  {
277
    $chars = $this->charactersFormatCharsMsg($chars);
278
    $languages = $languages ? "[ $languages ]" : '';
279
    return $msg ?: "just $chars $languages letters can be used PHP";
280
  }
281
}
282
283