1 <?php
2 3 4 5 6 7
8 namespace Cross\Core;
9
10 11 12 13 14
15 class CrossArray
16 {
17 18 19
20 protected $data;
21
22 23 24
25 protected static $instance;
26
27 28 29 30 31
32 private function __construct(array &$data)
33 {
34 $this->data = &$data;
35 }
36
37 38 39 40 41
42 static function init(array &$data, $cache_key = null)
43 {
44 if (null === $cache_key) {
45 $cache_key = md5(json_encode($data));
46 }
47
48 if (!isset(self::$instance[$cache_key])) {
49 self::$instance[$cache_key] = new self($data);
50 }
51
52 return self::$instance[$cache_key];
53 }
54
55 56 57 58 59 60 61
62 function get($config, $name = '')
63 {
64 if (isset($this->data[$config])) {
65 if ($name) {
66 if (is_array($name)) {
67 $result = array();
68 foreach ($name as $n) {
69 if (isset($this->data[$config][$n])) {
70 $result[$n] = $this->data[$config][$n];
71 }
72 }
73 return $result;
74 } elseif (isset($this->data[$config][$name])) {
75 return $this->data[$config][$name];
76 }
77
78 return false;
79 }
80
81 return $this->data[$config];
82 }
83 return false;
84 }
85
86 87 88 89 90 91 92
93 function set($index, $values = '')
94 {
95 if (is_array($values)) {
96 if (isset($this->data[$index])) {
97 $this->data[$index] = array_merge($this->data[$index], $values);
98 } else {
99 $this->data[$index] = $values;
100 }
101 } else {
102 $this->data[$index] = $values;
103 }
104
105 return true;
106 }
107
108 109 110 111 112 113
114 function getAll($obj = false)
115 {
116 if ($obj) {
117 return self::arrayToObject($this->data);
118 }
119
120 return $this->data;
121 }
122
123 124 125 126 127 128
129 static function arrayToObject($data)
130 {
131 if (is_array($data)) {
132 return (object)array_map('self::arrayToObject', $data);
133 } else {
134 return $data;
135 }
136 }
137 }
138