ILIAS  release_8 Revision v8.23
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 = array();
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  //$_POST['cmd'] = $this->cleanSubmitCommandFromPossibleIdentifierIndicators($_POST['cmd']);
50  return $this->removePositionIndexLevels($submitValues);
51  }
52 
53  protected function cleanSubmitCommandFromPossibleIdentifierIndicators($cmdArrayLevel)
54  {
55  if (is_array($cmdArrayLevel)) {
56  $currentKey = key($cmdArrayLevel);
57  $nextLevel = current($cmdArrayLevel);
58 
59  $nextLevel = $this->cleanSubmitCommandFromPossibleIdentifierIndicators($nextLevel);
60 
61  unset($cmdArrayLevel[$currentKey]);
62 
63  if ($this->isValueIdentifier($currentKey)) {
64  $currentKey = $this->removeIdentifierIndicator($currentKey);
65  }
66 
67  $cmdArrayLevel[$currentKey] = $nextLevel;
68  }
69 
70  return $cmdArrayLevel;
71  }
72 
73  protected function removePositionIndexLevels(array $values): array
74  {
75  foreach ($values as $key => $val) {
76  unset($values[$key]);
77 
78  if ($this->isValueIdentifier($key)) {
79  $key = $this->removeIdentifierIndicator($key);
80 
81  if ($this->isPositionIndexLevel($val)) {
82  $val = $this->fetchPositionIndexedValue($val);
83  }
84  } elseif (is_array($val)) {
85  $val = $this->removePositionIndexLevels($val);
86  }
87 
88  $values[$key] = $val;
89  }
90 
91  return $values;
92  }
93 
94  protected function isPositionIndexLevel($val): bool
95  {
96  if (!is_array($val)) {
97  return false;
98  }
99 
100  if (count($val) != 1) {
101  return false;
102  }
103 
104  return true;
105  }
106 
107  protected function isValueIdentifier($key): bool
108  {
109  $indicatorPrefixLength = self::IDENTIFIER_INDICATOR_PREFIX;
110 
111  if (strlen($key) <= strlen($indicatorPrefixLength)) {
112  return false;
113  }
114 
115  if (substr($key, 0, strlen($indicatorPrefixLength)) != $indicatorPrefixLength) {
116  return false;
117  }
118 
119  return true;
120  }
121 
122  protected function removeIdentifierIndicator($key)
123  {
124  return str_replace(self::IDENTIFIER_INDICATOR_PREFIX, '', $key);
125  }
126 
127  protected function fetchPositionIndexedValue($value)
128  {
129  return current($value);
130  }
131 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
string $key
Consumer key/client ID value.
Definition: System.php:193