@@ -26,6 +26,23 @@ and you're ready to go.
2626Usage examples
2727==============================
2828For additional help, look at the tests, additional input scenarios are tested.
29+ addConfigRow
30+ ------------------------------
31+ Method is intended to remove need for configuration files that need to be parsed into php arrays, since that carries significant performance
32+ penalties. It combines readability of ini file with speed of having configurations defined directly on php level.
33+ First parameter represents configuration array, second parameter represents a numerical array (in case of associative array, exception
34+ is thrown) of nested keys of the new configuration row. Third parameter represents the value that is being mapped.
35+ property, that is being added. Method returns configuration with the new row added, or throws exception if the same combination of
36+ keys has already been used.
37+ ```
38+ SimpleArrayLibrary::addConfigRow(array('foo' => array('baz' => 1)), array('foo', 'bar'), 2); // array('foo' => array('baz' => 1, 'bar' => 2))
39+ $config = array();
40+ $config = SimpleArrayLibrary::addConfigRow($config, array('foo', 'bar'), new stdClass());
41+ $config = SimpleArrayLibrary::addConfigRow($config, array('foo', 'baz'), 1);
42+ $config = SimpleArrayLibrary::addConfigRow($config, array('foo', 3), array(1, 2));
43+ // $config = array('foo' => array('bar' => stdClass(), 'baz' => 1, 3 => array(1, 2)))
44+ $config = SimpleArrayLibrary::addConfigRow($config, array('foo', 3), false); // exception because of repeated array('foo', 3) path
45+ ```
2946allElementsEqual
3047------------------------------
3148Checks whether all elements of the array are equal and optionally if they are all equal to specified value.
@@ -35,6 +52,19 @@ SimpleArrayLibrary::allElementsEqual(array(1, 2)); // false
3552SimpleArrayLibrary::allElementsEqual(array(1, 1), 1); // true
3653SimpleArrayLibrary::allElementsEqual(array(1, 1), 2); // false
3754```
55+ castColumns
56+ ------------------------------
57+ Attempts to cast specified columns of all rows of the two dimensional array to specified type. Allowed types are represented by
58+ constants: TYPE_INT, TYPE_STRING, TYPE_FLOAT, TYPE_BOOL, TYPE_ARRAY, TYPE_OBJECT. Any other type will cause exception to be thrown.
59+ User should know how type casting works in PHP as this method provides no protection from possible casting errors.
60+
61+ Third parameter must be a boolean, otherwise exception is thrown. If third parameter is set to true, columns defined as keys in the
62+ second parameter have to be present, otherwise exception is thrown.
63+ ```
64+ SimpleArrayLibrary::castColumns(array(array('a' => '2')), array('a' => SimpleArrayLibrary::TYPE_INT)); // array(array('a' => 2))
65+ SimpleArrayLibrary::castColumns(array(array()), array('a' => SimpleArrayLibrary::TYPE_INT), false); // array(array())
66+ SimpleArrayLibrary::castColumns(array(array('a' => '1'), array('b' => 'foo')), array('a' => SimpleArrayLibrary::TYPE_INT), false); // array(array('a' => 1), array('b' => 'foo'))
67+ ```
3868countMaxDepth
3969------------------------------
4070Count maximum depth of the array (number of nested sub-arrays) recursively.
@@ -156,6 +186,20 @@ SimpleArrayLibrary::haveEqualValues(array('a' => 1), array(1)); // true
156186SimpleArrayLibrary::haveEqualValues(array(), array()); // true
157187SimpleArrayLibrary::haveEqualValues(array(1), array(2)); // false
158188```
189+ insertSubArray
190+ ------------------------------
191+ Inserts the sub-array into the array in the place to which sub-array's keys point to
192+
193+ Third and forth parameters must be booleans, otherwise exception is thrown. If third parameter is set to true, insertion will overwrite
194+ existing value inside the array, pointed to the sub-array's keys, if any. If third parameter is set to true, forth parameter is not used.
195+ If forth parameter is set to true nad third parameter is set to false, existing value will be left unchanged, but if both third and
196+ forth parameters were set to false and sub-array keys point to already existing value, exception will be thrown
197+ ```
198+ SimpleArrayLibrary::insertSubArray(array('foo' => 1), array('bar' => 2)); // array('foo' => 1, 'bar' => 2)
199+ SimpleArrayLibrary::insertSubArray(array('foo' => 1), array('foo' => 2), true, false); // array('foo' => 2)
200+ SimpleArrayLibrary::insertSubArray(array('foo' => 1), array('foo' => 2), false, true); // array('foo' => 1)
201+ SimpleArrayLibrary::insertSubArray(array('foo' => 1), array('foo' => 2), false, false); // exception
202+ ```
159203isAssociative
160204------------------------------
161205Checks whether array has any associative keys.
@@ -174,16 +218,16 @@ SimpleArrayLibrary::isSubArray(array(2, 1), array(2)); // true
174218SimpleArrayLibrary::isSubArray(array('a' => 1, 'b' => array(1)), array('c' => 1)); // false
175219SimpleArrayLibrary::isSubArray(array('a' => 1, 'b' => array(1)), array('a' => 2)); // false
176220```
177- castColumns
221+ setColumn
178222------------------------------
179- Attempts to cast specified columns in all rows of the two dimensional array to specified type. Allowed types are represented by
180- constants: TYPE_INT, TYPE_STRING, TYPE_FLOAT, TYPE_BOOL, TYPE_ARRAY, TYPE_OBJECT. Any other type will cause exception to be thrown.
181- User should know how type casting works in PHP as this method provides no protection from possible casting errors.
223+ Sets values to columns of the multi-dimensional array (is meant for two-dimensional arrays in particular, will work for three or more
224+ dimension, but will only change elements on the second level) to the specified value
182225
183- Third parameter must be a boolean, otherwise exception is thrown. If third parameter is set to true, columns defined as keys in the
184- second parameter have to be present, otherwise exception is thrown.
226+ Forth and fifth parameters must be booleans, otherwise exception is thrown. If forth parameter is set to true, column will be added to
227+ the rows in which it's missing. If fifth parameter is set to true, value of the column will be overwritten in rows in which it already
228+ exists
185229```
186- SimpleArrayLibrary::castColumns (array(array('a ' => '2')) , array('a' => SimpleArrayLibrary::TYPE_INT)) ; // array(array('a ' => 2 ))
187- SimpleArrayLibrary::castColumns (array(array()) , array('a' => SimpleArrayLibrary::TYPE_INT) , false); // array(array())
188- SimpleArrayLibrary::castColumns (array(array('a ' => '1' ), array('b' => 'foo' )), array('a' => SimpleArrayLibrary::TYPE_INT) , false); // array(array('a ' => 1 ), array('b ' => 'foo' ))
230+ SimpleArrayLibrary::setColumn (array(array('foo ' => 2) , array()), 'foo', 1) ; // array(array('foo ' => 1), array('foo' => 1 ))
231+ SimpleArrayLibrary::setColumn (array(array('foo' => 2) , array()), 'foo', 1, false , false); // array(array('foo' => 2), array())
232+ SimpleArrayLibrary::setColumn (array(array('foo ' => 2 ), array()), 'foo', 1, true , false); // array(array('foo ' => 2 ), array('foo ' => 1 ))
189233```
0 commit comments