Passed
Branch master (7fff21)
by refat
05:12
created

Characters::canCharsSeparateViaComma()   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 $excepts;
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($excepts)
21
  {
22
    $this->excepts = $excepts;
23
    $this->chars = $this->excepts->chars->value ?? $this->excepts->chars->chars ?? null;
24
    $this->times = $this->excepts->chars->times ?? null;
25
    $this->atFirst = $this->excepts->chars->atFirst ?? null;
26
    $this->atEnd = $this->excepts->chars->atEnd ?? null;
27
    $this->between = $this->excepts->chars->between ?? null;
28
    $this->languages = $this->excepts->languages ?? 'english';
29
    $this->langsRegex = $this->excepts->languages ?? $this->languagesArray('english');
30
31
    $this->setChars();
32
    $this->setLanguages();
33
  }
34
35
  private function isExcepts()
36
  {
37
    return is_object($this->excepts) && count((array) $this->excepts);
38
  }
39
40
  private function isCharsString()
41
  {
42
    return is_string($this->chars);
43
  }
44
45
  private function isCharsAnArray()
46
  {
47
    return is_array($this->chars);
48
  }
49
50
  private function canCharsSeparateViaComma()
51
  {
52
    return preg_match('/,/', $this->chars) && preg_match_all('/,/', $this->chars) > 1;
53
  }
54
55
  private function formatCharsViaComma($comma)
56
  {
57
    if ($comma) {
58
      $chars = explode(',', $this->chars);
59
    } else {
60
      $chars = str_split($this->chars);
61
    }
62
    return "\\" . implode('\\', $chars);
63
  }
64
65
  private function formatCharsString()
66
  {
67
    if ($this->canCharsSeparateViaComma()) {
68
      return $this->formatCharsViaComma(true);
69
    } else {
70
      return $this->formatCharsViaComma(false);
71
    }
72
  }
73
74
  private function formatCharsArray()
75
  {
76
    return implode('', (array) $this->chars);
77
  }
78
79
  private function setChars()
80
  {
81
    if ($this->isExcepts()) {
82
      if ($this->isCharsString()) {
83
        $this->chars = $this->formatCharsString();
84
      } else if ($this->isCharsAnArray()) {
85
        $this->chars = $this->formatCharsArray();
86
      }
87
    }
88
  }
89
90
  public function getChars()
91
  {
92
    return $this->chars;
93
  }
94
95
  private function languagesArray($language)
96
  {
97
    $languages = [
98
      'all' => '\\p{L}',
99
      'arabic' =>  '\\x{0621}-\\x{064A}\\x{0660}-\\x{0669} ُ ْ َ ِ ّ~ ً ٍ ٌ',
100
      'english' => 'a-z',
101
      'spanish' => 'a-zñ',
102
      'french' => 'a-zàâçéèêëîïôûùüÿñæœ',
103
      'german' => 'a-zäüöß',
104
    ];
105
    return $languages[$language] ?? $languages['english'];
106
  }
107
108
  private function isLangsAnArray()
109
  {
110
    return is_array($this->languages);
111
  }
112
113
  private function isLangsAnString()
114
  {
115
    return is_string($this->languages);
116
  }
117
118
  private function canlangsSeparateViaComma()
119
  {
120
    return preg_match('/,/', $this->languages) && preg_match_all('/,/', $this->languages);
121
  }
122
123
  private function loopOverLangsViaComma($comma)
124
  {
125
    $loopLangs = $comma ? explode(',', $this->languages) : $this->languages;
126
    $langsRegex = '';
127
    $languages = '';
128
    foreach ($loopLangs as $language) {
129
      $langsRegex .= $this->languagesArray(trim($language));
130
      $languages .= "$language, ";
131
    }
132
    $languages = rtrim($languages, ", ");
133
    return array('languages' => $languages, 'langsRegex' => $langsRegex);
134
  }
135
136
  private function formatLangsString()
137
  {
138
    if ($this->canlangsSeparateViaComma()) {
139
      extract($this->loopOverLangsViaComma(true));
140
    } else {
141
      $langsRegex = $this->languagesArray(trim($this->languages));
142
      $languages = $this->languages;
143
    }
144
    return array('languages' => $languages, 'langsRegex' => $langsRegex);
145
  }
146
147
  private function setLanguages()
148
  {
149
    if ($this->isLangsAnArray()) {
150
      extract($this->loopOverLangsViaComma(false));
151
    } else if ($this->isLangsAnString()) {
152
      extract($this->formatLangsString());
153
    }
154
    $this->languages = $languages;
155
    $this->langsRegex = $langsRegex;
156
    $this->formatLangsRegex();
157
  }
158
159
  private function formatLangsRegex()
160
  {
161
    if ($this->langsRegex !== 'all' && preg_match_all('/a-z/i', $this->langsRegex) > 1) {
162
      $this->langsRegex = preg_replace('/a-z/', '', $this->langsRegex) . 'a-z';
163
    }
164
  }
165
166
  public function getLanguages()
167
  {
168
    return $this->languages;
169
  }
170
171
  public function getLangsRegex()
172
  {
173
    return $this->langsRegex;
174
  }
175
176
  public function getTimes()
177
  {
178
    return $this->times;
179
  }
180
  public function getAtFirst()
181
  {
182
    return $this->atFirst;
183
  }
184
  public function getAtEnd()
185
  {
186
    return $this->atEnd;
187
  }
188
  public function getBetween()
189
  {
190
    return $this->between;
191
  }
192
}
193
194