-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProxy.php
More file actions
72 lines (58 loc) · 1.85 KB
/
Proxy.php
File metadata and controls
72 lines (58 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php namespace Jlem\ArrayOk;
class Proxy
{
public static function flip(array $input)
{
return new ArrayOk(array_flip($input));
}
public static function order($target, $sequence, $cut = true)
{
$sequence = static::normalizeSequence($sequence);
$sequence = array_flip($sequence);
if ($cut) {
$target = array_intersect_key($target, $sequence);
}
return static::replace($sequence, $target);
}
public static function intersectKeys(array $these, array $andThese)
{
return new ArrayOk(call_user_func_array('array_intersect_key', func_get_args()));
}
public static function cut(array $these, array $andThese)
{
return static::intersectKeys($these, array_flip($andThese));
}
public static function replace(array $toBeReplaced, array $replaceWith)
{
return new ArrayOk(call_user_func_array('array_replace', func_get_args()));
}
public static function isAssociativeArray($array)
{
return (bool)count(array_filter(array_keys($array), 'is_string'));
}
public static function isAok($data)
{
return $data instanceof ArrayOk;
}
public static function json($data)
{
$data = new ArrayOk($data); // Wrap it as an ArrayOk object in case it's only a partial object
return $data->toJson();
}
protected static function normalizeSequence($sequence)
{
return is_array($sequence) ? $sequence : static::commasToArray($sequence);
}
protected static function commasToArray($string)
{
return explode(',', $string);
}
protected static function dotsToArray($string)
{
return explode('.', $string);
}
protected static function normalizeArray($array)
{
return static::isAok($array) ? $array : new ArrayOk($array);
}
}