ILIAS  trunk Revision v11.0_alpha-1713-gd8962da2f67
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilIdentifiedMultiValuesJsPositionIndexRemover.php
Go to the documentation of this file.
1 <?php
2 
23 {
24  public const IDENTIFIER_INDICATOR_PREFIX = 'IDENTIFIER~';
25 
26  public function manipulateFormInputValues(array $inputValues): array
27  {
28  return $this->brandIdentifiersWithIndicator($inputValues);
29  }
30 
31  protected function brandIdentifiersWithIndicator(array $origValues): array
32  {
33  $brandedValues = [];
34 
35  foreach ($origValues as $identifier => $val) {
36  $brandedValues[$this->getIndicatorBrandedIdentifier($identifier)] = $val;
37  }
38 
39  return $brandedValues;
40  }
41 
42  protected function getIndicatorBrandedIdentifier(string $identifier): string
43  {
44  return self::IDENTIFIER_INDICATOR_PREFIX . $identifier;
45  }
46 
47  public function manipulateFormSubmitValues(array $submitValues): array
48  {
49  return $this->removePositionIndexLevels($submitValues);
50  }
51 
52  protected function cleanSubmitCommandFromPossibleIdentifierIndicators($cmdArrayLevel)
53  {
54  if (is_array($cmdArrayLevel)) {
55  $currentKey = key($cmdArrayLevel);
56  $nextLevel = current($cmdArrayLevel);
57 
58  $nextLevel = $this->cleanSubmitCommandFromPossibleIdentifierIndicators($nextLevel);
59 
60  unset($cmdArrayLevel[$currentKey]);
61 
62  if ($this->isValueIdentifier($currentKey)) {
63  $currentKey = $this->removeIdentifierIndicator($currentKey);
64  }
65 
66  $cmdArrayLevel[$currentKey] = $nextLevel;
67  }
68 
69  return $cmdArrayLevel;
70  }
71 
72  protected function removePositionIndexLevels(array $values): array
73  {
74  foreach ($values as $key => $val) {
75  unset($values[$key]);
76 
77  if ($this->isValueIdentifier($key)) {
78  $key = $this->removeIdentifierIndicator($key);
79 
80  if ($this->isPositionIndexLevel($val)) {
81  $val = $this->fetchPositionIndexedValue($val);
82  }
83  } elseif (is_array($val)) {
84  $val = $this->removePositionIndexLevels($val);
85  }
86 
87  $values[$key] = $val;
88  }
89 
90  return $values;
91  }
92 
93  protected function isPositionIndexLevel($val): bool
94  {
95  if (!is_array($val)) {
96  return false;
97  }
98 
99  if (count($val) != 1) {
100  return false;
101  }
102 
103  return true;
104  }
105 
106  protected function isValueIdentifier($key): bool
107  {
108  $indicatorPrefixLength = self::IDENTIFIER_INDICATOR_PREFIX;
109 
110  if (strlen($key) <= strlen($indicatorPrefixLength)) {
111  return false;
112  }
113 
114  if (substr($key, 0, strlen($indicatorPrefixLength)) != $indicatorPrefixLength) {
115  return false;
116  }
117 
118  return true;
119  }
120 
121  protected function removeIdentifierIndicator($key)
122  {
123  return str_replace(self::IDENTIFIER_INDICATOR_PREFIX, '', $key);
124  }
125 
126  protected function fetchPositionIndexedValue($value)
127  {
128  return current($value);
129  }
130 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...