1: <?php
2:
3: /*
4: * The MIT License
5: *
6: * Copyright 2014 Damien Doussaud (namide.com).
7: *
8: * Permission is hereby granted, free of charge, to any person obtaining a copy
9: * of this software and associated documentation files (the "Software"), to deal
10: * in the Software without restriction, including without limitation the rights
11: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12: * copies of the Software, and to permit persons to whom the Software is
13: * furnished to do so, subject to the following conditions:
14: *
15: * The above copyright notice and this permission notice shall be included in
16: * all copies or substantial portions of the Software.
17: *
18: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24: * THE SOFTWARE.
25: */
26:
27: namespace Flea;
28:
29: /**
30: * Replace Array, effective to save in data base
31: *
32: * @author Namide
33: */
34: class DataList {
35:
36: private static $_EMPTY = null;
37: private $_datas;
38: private $_isAssoc;
39:
40: /**
41: * Construct the DataList in associative array or not
42: *
43: * @param boolean $isAssociative
44: */
45: public function __construct($isAssociative = false) {
46: $this->_datas = array();
47: $this->_isAssoc = $isAssociative;
48: }
49:
50: /**
51: * Add a value in this DataList.
52: * If DataList is not associative, don't add the key.
53: *
54: * @param type $val Value of the data
55: * @param type $key Key of the data (for a non-associative array)
56: */
57: public function add($val, $key = null) {
58: if ($this->_isAssoc) {
59: if (_DEBUG && $key === null) {
60: Debug::getInstance()->addError('Associative DataList must have a key');
61: } elseif (_DEBUG && $this->hasKey($key)) {
62: Debug::getInstance()->addError('The pair [' . $key . '=>' . $val . '] already exist');
63: }
64:
65: $this->_datas[$key] = $val;
66: } else {
67: $this->_datas[] = $val;
68: }
69: }
70:
71: /**
72: * Replace a value for a key.
73: * If this is a non-assiciative array,
74: * you must use an unsigned integer for the key.
75: *
76: * @param type $val New value
77: * @param type $key String for an associative array, otherwise unsigned integer
78: */
79: public function update($val, $key) {
80: $this->_datas[$key] = $val;
81: }
82:
83: /**
84: * Remove a data in the DataList.
85: * Key is not required.
86: *
87: * @param type $val Value to remove
88: * @param type $key Key of the value (not required)
89: * @return boolean Value is removed
90: */
91: public function remove($val, $key = null) {
92: if (!$this->hasValue($val)) {
93: return false;
94: }
95:
96: if ($key === null) {
97: $key = $this->getKey($val);
98: if ($this->_isAssoc) {
99: unset($this->_datas[$key]);
100: } else {
101: array_splice($this->_datas, $key, 1);
102: }
103: }
104: return true;
105: }
106:
107: /**
108: * Get the key of the value
109: *
110: * @param type $val Value to search
111: * @return type Key of the value
112: */
113: public function getKey($val) {
114: return array_search($val, $this->_datas);
115: }
116:
117: /**
118: * Add multiples datas
119: *
120: * @param array $values Use an associative array if the DataList is associative
121: */
122: public function addMultiple(array $values) {
123: foreach ($values as $key => $val) {
124: $this->add($val, $key);
125: }
126: }
127:
128: /**
129: * Get value by his key
130: *
131: * @param type $key Key of the value
132: * @return type Value of the key
133: */
134: public function getValue($key) {
135: if (!$this->hasKey($key) && _DEBUG) {
136: Debug::getInstance()->addError('The key:' . $key . ' don\'t exist');
137: }
138: if (!$this->hasKey($key)) {
139: return "";
140: }
141: return $this->_datas[$key];
142: }
143:
144: /**
145: * Obtains the array (with all the keys and values)
146: *
147: * @return type Array of the DataList
148: */
149: public function getArray() {
150: return $this->_datas;
151: }
152:
153: /**
154: * Change the content by a new array.
155: *
156: * @param type $array New array
157: */
158: public function setByArray($array) {
159: $this->_datas = $array;
160: $this->_isAssoc = array_keys($array) !== range(0, count($array) - 1);
161: }
162:
163: /**
164: * Check if the value is in the array
165: *
166: * @param type $val Value to check
167: * @return boolean Is in array
168: */
169: public function hasValue($val) {
170: return in_array($val, $this->_datas);
171: }
172:
173: /**
174: * Check if the key is in the array
175: *
176: * @param type $key Key to check
177: * @return boolean Is in array
178: */
179: public function hasKey($key) {
180: return array_key_exists($key, $this->_datas);
181: }
182:
183: /**
184: * Length of the DataList
185: *
186: * @return int Length of the DataList (unsigned integer)
187: */
188: public function length() {
189: return count($this->_datas);
190: }
191:
192: /**
193: * Get an empty DataList to avoid the construction.
194: * You must use it temporarily, because a new call to this function void the previous DataList
195: *
196: * @param boolean $isAssociative Precise if the new DataList is associative
197: * @return DataList New DataList
198: */
199: public static function getEmptyDataList($isAssociative = false) {
200: if (DataList::$_EMPTY === null) {
201: DataList::$_EMPTY = new DataList($isAssociative);
202: }
203: DataList::$_EMPTY->_isAssoc = $isAssociative;
204: return DataList::$_EMPTY;
205: }
206:
207: }
208: