1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Dictionary class |
5
|
|
|
* |
6
|
|
|
* The dictionary class allow to handle a repository of key-values data |
7
|
|
|
* Values are accessibles via a dot notation key path. |
8
|
|
|
* |
9
|
|
|
* Example: |
10
|
|
|
* <code> |
11
|
|
|
* class MyConfig extends Dictionary {} |
12
|
|
|
* MyConfig::set('user',[ 'name' => 'Frank', 'surname' => 'Castle' ]); |
13
|
|
|
* echo "Hello, my name is ",MyConfig::get('user.name'),' ',MyConfig::get('user.surname'); |
14
|
|
|
* </code> |
15
|
|
|
* |
16
|
|
|
* @package core |
17
|
|
|
* @author [email protected] |
18
|
|
|
* @copyright Caffeina srl - 2016 - http://caffeina.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
abstract class Dictionary { |
22
|
|
|
use Events; |
23
|
|
|
use Filters; |
24
|
|
|
|
25
|
|
|
protected static $fields = null; |
26
|
|
|
|
27
|
|
|
public static function & all(){ |
28
|
|
|
if (!static::$fields) static::$fields = new Map(); |
29
|
|
|
return static::$fields->all(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public static function get($key, $default=null){ |
33
|
|
|
if (!static::$fields) static::$fields = new Map(); |
34
|
|
|
if (is_array($key)){ |
35
|
|
|
$results = []; |
36
|
|
|
foreach ($key as $_dst_key => $_src_key) |
37
|
|
|
$results[$_dst_key] = static::$fields->get($_src_key); |
38
|
|
|
return $results; |
39
|
|
|
} else { |
40
|
|
|
return static::$fields->get($key, $default); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public static function set($key, $value=null){ |
45
|
|
|
if (!static::$fields) static::$fields = new Map(); |
46
|
|
|
return static::$fields->set($key, $value); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public static function delete($key, $compact=true){ |
50
|
|
|
if (!static::$fields) static::$fields = new Map(); |
51
|
|
|
static::$fields->delete($key, $compact); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public static function exists($key){ |
55
|
|
|
if (!static::$fields) static::$fields = new Map(); |
56
|
|
|
return static::$fields->exists($key); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public static function clear(){ |
60
|
|
|
if (!static::$fields) static::$fields = new Map(); |
61
|
|
|
static::$fields->clear(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public static function load($fields){ |
65
|
|
|
if (!static::$fields) static::$fields = new Map(); |
66
|
|
|
static::$fields->load($fields); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public static function merge(array $array, $merge_back=false){ |
70
|
|
|
if (!static::$fields) static::$fields = new Map(); |
71
|
|
|
static::$fields->merge($array, $merge_back); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected static function compact(){ |
75
|
|
|
if (!static::$fields) static::$fields = new Map(); |
76
|
|
|
static::$fields->compact(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
protected static function & find($path, $create=false, callable $operation=null) { |
80
|
|
|
return static::$fields->find($path, $create, $operation); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function jsonSerialize(){ |
84
|
|
|
if (!static::$fields) static::$fields = new Map(); |
85
|
|
|
return static::$fields->jsonSerialize(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|