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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
namespace Khansia\Generic\Objects;
class Mapper extends \Khansia\Generic\Objects {
const MODE_EXCLUDE = 0;
const MODE_INCLUDE = 1;
const CASE_SENSITIVE = 0;
const CASE_INSENSITIVE = 1;
private $_case;
private $_map = array();
public function __construct($data = array(), $map = array(), $case = self::CASE_SENSITIVE) {
$this->_case = $case;
$properties = array();
foreach ($map as $key => $item) {
if ($item instanceof Map) {
$this->_map[($case == self::CASE_INSENSITIVE) ? strtolower($item->real) : $item->real] = $item;
$properties[] = $item->alias;
} else {
$this->_map[($case == self::CASE_INSENSITIVE) ? strtolower($key) : $key] = new Map($key, $item);
$properties[] = $item;
}
}
parent::__construct($data, $properties);
}
public function map($real, $alias = null, $pseudo = false, $data = null, $store = true) {
$map = new Map($real, $alias, $pseudo);
if ($store) {
$this->_map[($this->_case == self::CASE_INSENSITIVE) ? strtolower($real) : $real] = $map;
parent::extend($alias == null ? $real : $alias, $data);
}
return $map;
}
public function get($name) {
return $this->__get($this->_map[$name]->alias);
}
public function set($name, $value) {
return $this->__set($this->_map[$name]->alias, $value);
}
public function push($data, $map = true) {
if ($map && $this->_hasProperties) {
if (is_array($data)) {
foreach ($data as $name => $value) {
$name = ($this->_case == self::CASE_INSENSITIVE) ? strtolower($name) : $name;
if (array_key_exists($name, $this->_map) === false) {
parent::notice('push', $name);
} else {
$this->_data[$this->_map[$name]->alias] = $value;
}
}
}
} else {
/* raw push */
parent::push($data);
}
}
public function pull($mode = self::MODE_EXCLUDE) {
$properties = array();
/* init output */
$data = array();
if ($mode == self::MODE_INCLUDE) {
/* include only data in properties */
foreach ($this->_map as $key => $value) {
if (!$value->pseudo) {
if (array_search($key, $properties) !== false) {
$data[$key] = $this->_data[$value->alias];
}
}
}
} else {
/* exclude data in properties array */
foreach ($this->_map as $key => $value) {
if (!$value->pseudo) {
if (array_search($key, $properties) === false) {
$data[$key] = $this->_data[$value->alias];
}
}
}
}
return $data;
}
}