Skip to content

Commit 71f6742

Browse files
committed
2.3.0
Adding lodash like functions to Arrays Fixing Debug issues
1 parent a5410c1 commit 71f6742

File tree

2 files changed

+192
-115
lines changed

2 files changed

+192
-115
lines changed

src/Arrays.php

Lines changed: 184 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,180 @@ static function toArray($var,$divider=',\s*'){
1919
}
2020
return (array)$var;
2121
}
22-
/// Checks if elemen of an arbitrarily deep array is set
22+
/// lodash pick
23+
static function pick($src, $props){
24+
$props = self::toArray($props);
25+
$dest = [];
26+
foreach($props as $prop){
27+
$dest[$prop] = $src[$prop];
28+
}
29+
return $dest;
30+
}
31+
/// lodash omit
32+
static function omit($src, $props){
33+
$props = self::toArray($props);
34+
$dest = [];
35+
foreach($src as $key=>$value){
36+
if(!in_array($key, $props)){
37+
$dest[$key] = $value;
38+
}
39+
}
40+
return $dest;
41+
}
42+
43+
/// lodash get. Works with arrays and objects. Specially handling for part which is a obj.method
44+
function get($collection, $path){
45+
foreach(explode('.', $path) as $part){
46+
if(is_object($collection)){
47+
if(isset($collection->$part)){
48+
$collection = $collection->$part;
49+
}elseif(is_callable([$collection, $part])){
50+
$collection = [$collection, $part]; # turn it into a callable form
51+
}else{
52+
return null;
53+
}
54+
}elseif(is_array($collection)){
55+
if(isset($collection[$part])){
56+
$collection = $collection[$part];
57+
}else{
58+
return null;
59+
}
60+
}else{
61+
throw new Exception('Part is not a traverseable structure');
62+
}
63+
}
64+
return $collection;
65+
}
66+
67+
/// change the name of some keys
68+
static function remap($src, $remap){
69+
foreach($remap as $k=>$v){
70+
$dest[$v] = $src[$k];
71+
unset($src[$k]);
72+
}
73+
return array_merge($src,$dest);
74+
}
75+
76+
static function each($src, $fn){
77+
78+
}
79+
80+
81+
///removes all instances of value from an array
82+
/**
83+
@param value the value to be removed
84+
@param array the array to be modified
85+
*/
86+
static function remove(&$array,$value = false,$strict = false){
87+
if(!is_array($array)){
88+
throw new \Exception('Parameter must be an array');
89+
}
90+
$existingKey = array_search($value,$array);
91+
while($existingKey !== false){
92+
unset($array[$existingKey]);
93+
$existingKey = array_search($value,$array,$strict);
94+
}
95+
return $array;
96+
}
97+
98+
# ensure a value is in array. If not, append array.
99+
static function ensure(&$array, $value){
100+
if(array_search($value, $array) === false){
101+
$array[] = $value;
102+
}
103+
return $array;
104+
}
105+
106+
///takes an array of keys to extract out of one array and into another array
107+
/**
108+
@param forceKeys will cause keys not in extractee to be set to null in the return
109+
*/
110+
static function &extract($keys,$extactee,&$extractTo=null,$forceKeys=true){
111+
if(!is_array($extractTo)){
112+
$extractTo = array();
113+
}
114+
foreach($keys as $key){
115+
if(array_key_exists($key,$extactee) || $forceKeys){
116+
$extractTo[$key] = $extactee[$key];
117+
}
118+
}
119+
return $extractTo;
120+
}
121+
///takes an array and maps its values to the keys of another array
122+
/**
123+
@param map array {<newKey> : <oldKey>, <newKey> : <oldKey>, <straight map>}
124+
@param $numberDefault wherein if the newKey is a number, assume the oldKey is both the oldKey and the newKey
125+
126+
ex
127+
$bob = ['sue'=>'a','bill'=>'b'];
128+
Arrays::map(['test'=>'sue','bill'],$bob,$x=null,true);
129+
[[test] => a [bill] => b]
130+
*/
131+
static function &map($map,$extractee,$straightMap=false,&$extractTo=null){
132+
if(!is_array($extractTo)){
133+
$extractTo = array();
134+
}
135+
if(!$straightMap){
136+
foreach($map as $to=>$from){
137+
$extractTo[$to] = $extractee[$from];
138+
}
139+
}else{
140+
foreach($map as $to=>$from){
141+
if(is_int($to)){
142+
$extractTo[$from] = $extractee[$from];
143+
}else{
144+
$extractTo[$to] = $extractee[$from];
145+
}
146+
}
147+
}
148+
149+
return $extractTo;
150+
}
151+
152+
///apply a callback to an array, returning the result, with optional arrayed parameters
153+
/**
154+
@param callback function($k,$v,optional params...)
155+
*/
156+
static function apply($array,$callback,$parameters = []){
157+
$parameters = (array)$parameters;
158+
$newArray = [];
159+
foreach($array as $k=>$v){
160+
$newArray[$k] = call_user_func_array($callback,array_merge([$k,$v],$parameters));
161+
}
162+
return $newArray;
163+
}
164+
165+
166+
167+
#++ Depth path functions {
168+
169+
/// takes an array and flattens it to one level using separator to indicate key deepness
170+
/**
171+
@param array a deep array to flatten
172+
@param separator the string used to indicate in the flat array a level of deepenss between key strings
173+
@param keyPrefix used to prefix the key at the current level of deepness
174+
@return array
175+
*/
176+
static function flatten($array,$separator='_',$keyPrefix=null){
177+
foreach($array as $k=>$v){
178+
if($fK){
179+
$key = $keyPrefix.$separator.$k;
180+
}else{
181+
$key = $k;
182+
}
183+
if(is_array($v)){
184+
$sArrays = self::arrayFlatten($v,$key,$separator);
185+
foreach($sArrays as $k2 => $v2){
186+
$sArray[$k2] = $v2;
187+
}
188+
}else{
189+
$sArray[$key] = $v;
190+
}
191+
}
192+
return $sArray;
193+
}
194+
195+
/// Checks if element of an arbitrarily deep array is set
23196
/**
24197
@param keys array comma separated list of keys to traverse
25198
@param array array The array which is parsed for the element
@@ -139,31 +312,10 @@ static function replaceAllParents($value,$replacement,$array,$parentDepth=1,&$fo
139312
unset($element);
140313
return $array;
141314
}
142-
/// takes an array and flattens it to one level using separator to indicate key deepness
143-
/**
144-
@param array a deep array to flatten
145-
@param separator the string used to indicate in the flat array a level of deepenss between key strings
146-
@param keyPrefix used to prefix the key at the current level of deepness
147-
@return array
148-
*/
149-
static function flatten($array,$separator='_',$keyPrefix=null){
150-
foreach($array as $k=>$v){
151-
if($fK){
152-
$key = $keyPrefix.$separator.$k;
153-
}else{
154-
$key = $k;
155-
}
156-
if(is_array($v)){
157-
$sArrays = self::arrayFlatten($v,$key,$separator);
158-
foreach($sArrays as $k2 => $v2){
159-
$sArray[$k2] = $v2;
160-
}
161-
}else{
162-
$sArray[$key] = $v;
163-
}
164-
}
165-
return $sArray;
166-
}
315+
316+
#++ }
317+
318+
167319
///Takes an arary of arbitrary deepness and turns the keys into tags and values into data
168320
/**
169321
@param array array to be turned into xml
@@ -198,6 +350,7 @@ static function homogenise($array,$type='key'){
198350
}
199351
return $newA;
200352
}
353+
201354
///php collapses numbered or number string indexes on merge, this does not
202355
static function indexMerge($x,$y){
203356
if(is_array($x)){
@@ -236,7 +389,7 @@ static function firstAvailableKey($array){
236389
}
237390
return $key;
238391
}
239-
///if false or null key, append. Otherwise, add at key. Optionally, append to key=>array().
392+
/// if no key (false or null), append, otherwise, use the key. Account for collisions with optional append.
240393
/**
241394
@param key can be null or key or array. If null, value added to end of array
242395
@param value value to add to array
@@ -260,7 +413,7 @@ static function addOnKey($key,$value,&$array,$append=false){
260413
return count($array) - 1;
261414
}
262415
}
263-
///adds to the array and overrides duplicate elements
416+
/// adds to the array and overrides duplicate elements
264417
/**removes all instances of some value in an array then adds the value according to the key
265418
@param value the value to be removed then added
266419
@param array the array to be modified
@@ -271,29 +424,7 @@ static function addOverride($value,&$array,$key=null){
271424
self::addOnKey($key,$value,$array);
272425
return $array;
273426
}
274-
///removes all instances of value from an array
275-
/**
276-
@param value the value to be removed
277-
@param array the array to be modified
278-
*/
279-
static function remove(&$array,$value = false,$strict = false){
280-
if(!is_array($array)){
281-
throw new \Exception('Parameter must be an array');
282-
}
283-
$existingKey = array_search($value,$array);
284-
while($existingKey !== false){
285-
unset($array[$existingKey]);
286-
$existingKey = array_search($value,$array,$strict);
287-
}
288-
return $array;
289-
}
290-
# ensure a value is in array. If not, append array.
291-
static function ensure(&$array, $value){
292-
if(array_search($value, $array) === false){
293-
$array[] = $value;
294-
}
295-
return $array;
296-
}
427+
297428
///separate certain keys from an array and put them into another, returned array
298429
static function separate($keys,&$array){
299430
$separated = array();
@@ -303,7 +434,7 @@ static function separate($keys,&$array){
303434
}
304435
return $separated;
305436
}
306-
/// see examples
437+
/// Take some normal list of rows and make it a id-keyed list pointing to either a value or the row remainder
307438
/**
308439
Ex
309440
1
@@ -376,51 +507,7 @@ static function implode($separator,$array){
376507
Arrays::remove($array);
377508
return implode($separator,$array);
378509
}
379-
///takes an array of keys to extract out of one array and into another array
380-
/**
381-
@param forceKeys will cause keys not in extractee to be set to null in the return
382-
*/
383-
static function &extract($keys,$extactee,&$extractTo=null,$forceKeys=true){
384-
if(!is_array($extractTo)){
385-
$extractTo = array();
386-
}
387-
foreach($keys as $key){
388-
if(array_key_exists($key,$extactee) || $forceKeys){
389-
$extractTo[$key] = $extactee[$key];
390-
}
391-
}
392-
return $extractTo;
393-
}
394-
///takes an array and maps its values to the keys of another array
395-
/**
396-
@param map array {<newKey> : <oldKey>, <newKey> : <oldKey>, <straight map>}
397-
@param $numberDefault wherein if the newKey is a number, assume the oldKey is both the oldKey and the newKey
398510

399-
ex
400-
$bob = ['sue'=>'a','bill'=>'b'];
401-
Arrays::map(['test'=>'sue','bill'],$bob,$x=null,true);
402-
[[test] => a [bill] => b]
403-
*/
404-
static function &map($map,$extractee,$straightMap=false,&$extractTo=null){
405-
if(!is_array($extractTo)){
406-
$extractTo = array();
407-
}
408-
if(!$straightMap){
409-
foreach($map as $to=>$from){
410-
$extractTo[$to] = $extractee[$from];
411-
}
412-
}else{
413-
foreach($map as $to=>$from){
414-
if(is_int($to)){
415-
$extractTo[$from] = $extractee[$from];
416-
}else{
417-
$extractTo[$to] = $extractee[$from];
418-
}
419-
}
420-
}
421-
422-
return $extractTo;
423-
}
424511
///checks if $subset is a sub set of $set starting at $start
425512
/*
426513
@@ -458,7 +545,7 @@ static function countIn($value,$array,$max=null){
458545
}
459546
return $count;
460547
}
461-
//takes and object and converts it into an array. Ignores nested objects
548+
/// takes an object and converts it into an array. Ignores nested objects
462549
static function convert($variable,$parseObject=true){
463550
if(is_object($variable)){
464551
if($parseObject){
@@ -479,16 +566,4 @@ static function convert($variable,$parseObject=true){
479566
return $variable;
480567
}
481568
}
482-
///apply a callback to an array, returning the result, with optional arrayed parameters
483-
/**
484-
@param callback function($k,$v,optional params...)
485-
*/
486-
static function apply($array,$callback,$parameters = []){
487-
$parameters = (array)$parameters;
488-
$newArray = [];
489-
foreach($array as $k=>$v){
490-
$newArray[$k] = call_user_func_array($callback,array_merge([$k,$v],$parameters));
491-
}
492-
return $newArray;
493-
}
494569
}

src/Debug.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/// used for basic debuging
99
/** For people other than me, things don't always go perfectly. As such, this class is exclusively for you. Measure things. Find new and unexpected features. Explore the error messages*/
1010
class Debug{
11-
static $env;
11+
static $env = ['mode'=>'dev debug info error warning notice','error_detail'=>2,'stack_exclusion'=>[], 'abbreviations'=>[]];
1212
/**
1313
@param env {
1414
mode: < string against which log calls with mode is regex matched, Ex: "debug info error" >
@@ -21,12 +21,11 @@ class Debug{
2121
}
2222
*/
2323
static function configure($env=[]){
24-
$env = Arrays::merge(['mode'=>'dev debug info error warning notice','error_detail'=>2,'stack_exclusion'=>[], 'abbreviations'=>[]], $env);
24+
self::$env = Arrays::merge(self::$env, $env);
2525

26-
if(!$env['log_file'] || !$env['err_file']){
26+
if(!self::$env['log_file'] || !self::$env['err_file']){
2727
throw new \Exception('Must specify both log_file and err_file');
2828
}
29-
self::$env = $env;
3029
}
3130

3231
///provided for convenience to place various user debugging related values
@@ -283,8 +282,11 @@ static function toString($variable,$objectMaxDepth=2,$depth=0,$objectDepth=0){
283282
}
284283
}
285284
static function abbreviateFilePath($path){
286-
foreach(self::$env['abbreviations'] as $name=>$abbr)
287-
return preg_replace('@'.$abbr.'@',$name.':',$path);
285+
foreach(self::$env['abbreviations'] as $name=>$abbr){
286+
$path = preg_replace('@'.$abbr.'@',$name.':',$path);
287+
}
288+
return $path;
289+
288290
}
289291
///print a variable to self::toString and quit
290292
/** Clears butter, outputs variable without context

0 commit comments

Comments
 (0)