Passed
Branch master (a1faa7)
by refat
03:26
created

AllowedCharacters::hasLinkMultipleIndexes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 1
f 0
1
<?php
2
3
namespace System\Validation;
4
5
use System\Application;
6
7
class AllowedCharacters
8
{
9
  private $app;
10
  private $characters;
11
12
  public function __construct(Application $app, $characters)
13
  {
14
    $this->app = $app;
15
    $this->characters = $characters;
16
    $this->checkCharacters($characters);
17
  }
18
19
  private function isLink($character)
20
  {
21
    return strpos($character, 'path:') === 0;
22
  }
23
24
  private function hasLinkKeyAndValuse($character)
25
  {
26
    return strpos($character, '::');
27
  }
28
29
  private function hasLinkSpecificIndex($character)
30
  {
31
    return strpos($character, ':[');
32
  }
33
34
  private function hasLinkMultipleIndexes($indexes)
35
  {
36
    return strpos($indexes, '][');
37
  }
38
39
  private function whenLinkIndexes($indexes, $path)
40
  {
41
    $files = [];
42
    if ($this->hasLinkMultipleIndexes($indexes)) {
43
      $indexes = explode('][', $indexes);
44
      $files += $this->getIndexesInFiles($indexes, $path);
45
    } else {
46
      $files += $this->app->file->call($path . '.php')[$indexes];
47
    }
48
    return $files;
49
  }
50
51
  private function getIndexesInFiles($indexes, $path)
52
  {
53
    $indexesInFiles = [];
54
55
    foreach ($indexes as $index) {
56
      if (!empty($indexesInFiles)) {
57
        $indexesInFiles = $indexesInFiles[$index];
58
59
      } else {
60
        $indexesInFiles = $this->app->file->call($path . '.php')[$index];
61
      }
62
    }
63
    return $indexesInFiles;
64
  }
65
66
  private function formatPath($character)
67
  {
68
    $path = substr($character, 5);
69
    $getFrom = 'value';
70
    if ($this->hasLinkKeyAndValuse($character)) {
71
      list($path, $getFrom) = explode('::', $path);
72
    }
73
    return [$path, $getFrom];
74
  }
75
76
  private function formatCharacters($characters, $key, $character)
77
  {
78
    list($path, $getFrom) = $this->formatPath($character);
79
    $indexes = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $indexes is dead and can be removed.
Loading history...
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