ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilIdentifiedMultiValuesJsPositionIndexRemover.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4require_once 'Services/Form/interfaces/interface.ilFormValuesManipulator.php';
5
13{
14 const IDENTIFIER_INDICATOR_PREFIX = 'IDENTIFIER~';
15
16 public function manipulateFormInputValues($inputValues)
17 {
18 return $this->brandIdentifiersWithIndicator($inputValues);
19 }
20
21 protected function brandIdentifiersWithIndicator($origValues)
22 {
23 $brandedValues = array();
24
25 foreach ($origValues as $identifier => $val) {
26 $brandedValues[$this->getIndicatorBrandedIdentifier($identifier)] = $val;
27 }
28
29 return $brandedValues;
30 }
31
32 protected function getIndicatorBrandedIdentifier($identifier)
33 {
34 return self::IDENTIFIER_INDICATOR_PREFIX . $identifier;
35 }
36
37 public function manipulateFormSubmitValues($submitValues)
38 {
40 return $this->removePositionIndexLevels($submitValues);
41 }
42
43 protected function cleanSubmitCommandFromPossibleIdentifierIndicators($cmdArrayLevel)
44 {
45 if (is_array($cmdArrayLevel)) {
46 $currentKey = key($cmdArrayLevel);
47 $nextLevel = current($cmdArrayLevel);
48
49 $nextLevel = $this->cleanSubmitCommandFromPossibleIdentifierIndicators($nextLevel);
50
51 unset($cmdArrayLevel[$currentKey]);
52
53 if ($this->isValueIdentifier($currentKey)) {
54 $currentKey = $this->removeIdentifierIndicator($currentKey);
55 }
56
57 $cmdArrayLevel[$currentKey] = $nextLevel;
58 }
59
60 return $cmdArrayLevel;
61 }
62
63 protected function removePositionIndexLevels($values)
64 {
65 foreach ($values as $key => $val) {
66 unset($values[$key]);
67
68 if ($this->isValueIdentifier($key)) {
69 $key = $this->removeIdentifierIndicator($key);
70
71 if ($this->isPositionIndexLevel($val)) {
72 $val = $this->fetchPositionIndexedValue($val);
73 }
74 } elseif (is_array($val)) {
75 $val = $this->removePositionIndexLevels($val);
76 }
77
78 $values[$key] = $val;
79 }
80
81 return $values;
82 }
83
84 protected function isPositionIndexLevel($val)
85 {
86 if (!is_array($val)) {
87 return false;
88 }
89
90 if (count($val) != 1) {
91 return false;
92 }
93
94 return true;
95 }
96
97 protected function isValueIdentifier($key)
98 {
99 $indicatorPrefixLength = self::IDENTIFIER_INDICATOR_PREFIX;
100
101 if (strlen($key) <= strlen($indicatorPrefixLength)) {
102 return false;
103 }
104
105 if (substr($key, 0, strlen($indicatorPrefixLength)) != $indicatorPrefixLength) {
106 return false;
107 }
108
109 return true;
110 }
111
112 protected function removeIdentifierIndicator($key)
113 {
114 return str_replace(self::IDENTIFIER_INDICATOR_PREFIX, '', $key);
115 }
116
117 protected function fetchPositionIndexedValue($value)
118 {
119 return current($value);
120 }
121}
$_POST["username"]
An exception for terminatinating execution or to throw for unit testing.