1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BestServedCold\PhalueObjects\ExtendedArray; |
4
|
|
|
|
5
|
|
|
use BestServedCold\PhalueObjects\String\StringTrait; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class ExtendedArrayTrait |
9
|
|
|
* |
10
|
|
|
* @package BestServedCold\PhalueObjects\ExtendedArray |
11
|
|
|
* @author Adam Lewis <[email protected]> |
12
|
|
|
* @copyright Copyright (c) 2015 Best Served Cold Media Limited |
13
|
|
|
* @license http://http://opensource.org/licenses/GPL-3.0 GPL License |
14
|
|
|
* @link http://bestservedcold.com |
15
|
|
|
* @since 0.0.1-alpha |
16
|
|
|
* @version 0.0.8-alpha |
17
|
|
|
*/ |
18
|
|
|
trait ExtendedArrayTrait |
19
|
|
|
{ |
20
|
|
|
use StringTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param array $array |
24
|
|
|
* @param Integer $spaces |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
|
|
public function arrayToCommaString(array $array, $spaces) |
28
|
11 |
|
{ |
29
|
|
|
return implode(",{$this->integerToSpace($spaces)}", $array); |
|
|
|
|
30
|
11 |
|
} |
31
|
|
|
|
32
|
|
|
public function arrayToPairString(array $array) |
33
|
|
|
{ |
34
|
|
|
return implode(',', array_map(function ($key, $value) { |
35
|
|
|
return $key . '=' . $value; |
36
|
|
|
}, array_keys($array), $array)); |
37
|
3 |
|
} |
38
|
|
|
|
39
|
3 |
|
/** |
40
|
|
|
* @param $json |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
|
|
public static function getArrayUsingJsonNotation($json) |
44
|
|
|
{ |
45
|
|
|
return explode('.', $json); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get an item from an array using "dot" notation. |
50
|
|
|
* |
51
|
2 |
|
* @param array $array |
52
|
|
|
* @param string $key |
|
|
|
|
53
|
|
|
* @param mixed $default |
54
|
|
|
* |
55
|
|
|
* @return mixed |
56
|
2 |
|
*/ |
57
|
1 |
|
public static function getFromArrayUsingJsonNotation( |
58
|
|
|
$array, |
59
|
|
|
$key = null, |
60
|
2 |
|
$default = null |
61
|
2 |
|
) { |
62
|
|
|
if (is_null($key)) { |
63
|
|
|
return $array; |
64
|
2 |
|
} |
65
|
2 |
|
|
66
|
1 |
|
if (isset($array[ $key ])) { |
67
|
|
|
return $array[ $key ]; |
68
|
2 |
|
} |
69
|
2 |
|
|
70
|
|
|
foreach (self::getArrayUsingJsonNotation($key) as $segment) { |
71
|
2 |
|
if (!is_array($array) || !array_key_exists($segment, $array)) { |
72
|
|
|
return $default; |
73
|
|
|
} |
74
|
|
|
$array = $array[ $segment ]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $array; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param array $array |
82
|
|
|
* @param bool|false $key |
83
|
|
|
* @return null |
84
|
|
|
*/ |
85
|
|
|
public static function nullIfNotSet(array $array, $key = false) |
86
|
|
|
{ |
87
|
|
|
return isset($array[$key]) ? $array[$key] : null; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param array $array |
92
|
|
|
* @param bool|false $key |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
|
public static function falseIfNotSet(array $array, $key = false) |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
return isset($array[$key]) ? $array[$key] : false; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: