Passed
Branch master (d9287e)
by refat
05:38
created

AlloweJust::hasLinkMultipleIndexes()   A

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 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace System\Validation;
4
5
use System\Application;
6
7
class AlloweJust
8
{
9
  private $app;
10
  private $characters;
11
12
  public function __construct(Application $app, $characters)
13
  {
14
    $this->app = $app;
15
    $this->characters = (!is_array($characters) && $characters !== '') ? [$characters] : $characters;
16
17
    $this->checkCharacters($this->characters);
18
  }
19
20
  private function isLink($character)
21
  {
22
    return strpos($character, 'path:') === 0;
23
  }
24
25
  private function hasLinkKeyAndValuse($character)
26
  {
27
    return strpos($character, '::');
28
  }
29
30
  private function hasLinkSpecificIndex($character)
31
  {
32
    return strpos($character, ':[');
33
  }
34
35
  private function hasLinkMultipleIndexes($indexes)
36
  {
37
    return strpos($indexes, '][');
38
  }
39
40
  private function whenLinkIndexes($indexes, $path)
41
  {
42
    $files = [];
43
    if ($this->hasLinkMultipleIndexes($indexes)) {
44
      $indexes = explode('][', $indexes);
45
      $files += $this->getIndexesInFiles($indexes, $path);
46
    } else {
47
      $files += $this->app->file->call($path . '.php')[$indexes];
48
    }
49
    return $files;
50
  }
51
52
  private function getIndexesInFiles($indexes, $path)
53
  {
54
    $indexesInFiles = [];
55
56
    foreach ($indexes as $index) {
57
      if (!empty($indexesInFiles)) {
58
        $indexesInFiles = $indexesInFiles[$index];
59
60
      } else {
61
        $indexesInFiles = $this->app->file->call($path . '.php')[$index];
62
      }
63
    }
64
    return $indexesInFiles;
65
  }
66
67
  private function formatPath($character)
68
  {
69
    $path = substr($character, 5);
70
    $getFrom = 'value';
71
    if ($this->hasLinkKeyAndValuse($character)) {
72
      list($path, $getFrom) = explode('::', $path);
73
    }
74
    return [$path, $getFrom];
75
  }
76
77
  private function formatCharacters($characters, $key, $character)
78
  {
79
    list($path, $getFrom) = $this->formatPath($character);
80
    $files = [];
81
82
    unset($characters[$key]);
83
84
    if ($this->hasLinkSpecificIndex($character)) {
85
      list($path, $indexes) = explode(':[', $path);
86
      $indexes = rtrim($indexes, ']');
87
      $files += $this->whenLinkIndexes($indexes, $path);
88
    } else {
89
      $files += $this->app->file->call($path . '.php');
90
    }
91
    $this->characters += $getFrom === 'keys' ? array_keys($files) : array_values($files);
92
  }
93
94
  private function checkCharacters($characters)
95
  {
96
    foreach ($characters as $key => $character) {
97
      if ($this->isLink($character)) {
98
        $this->formatCharacters($characters, $key, $character);
99
      } else {
100
        array_push($this->characters, $character);
101
      }
102
    }
103
  }
104
105
  public function getCharacters()
106
  {
107
    return $this->characters;
108
  }
109
}
110