-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCRObject.class.php
More file actions
executable file
·69 lines (59 loc) · 1.33 KB
/
CRObject.class.php
File metadata and controls
executable file
·69 lines (59 loc) · 1.33 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
<?php
class CRObject implements JsonSerializable
{
private $map;
/*
* @param $map: key-value array
*/
public function __construct(array $map = array())
{
$this->map = $map;
}
public function set($key, $value)
{
$this->map[$key] = $value;
return true;
}
public function get($key, $default = null)
{
if (isset($this->map[$key]) && !is_null($this->map[$key])) {
return $this->map[$key];
}
return $default;
}
public function getInt($key, $default = null)
{
if (isset($this->map[$key]) && !is_null($this->map[$key]) && is_numeric($this->map[$key])) {
return intval($this->map[$key]);
}
return $default;
}
public function getBool($key, $default = false)
{
if (isset($this->map[$key]) && !is_null($this->map[$key])) {
return $this->map[$key] === true;
}
return $default === true;
}
public function toArray()
{
return $this->map;
}
/*
* set $this[$key] if isset($obj[$key])&&!isset($this[$key])
* (new CRObject(['k1' => 'v1'])).union(new CRObject(['k1' => 'v1.1', 'k2' => 'v2']))
* = new CRObject(['k1' => 'v1', 'k2' => 'v2'])
*/
public function union(CRObject $obj)
{
$keys = array_keys($obj->toArray());
foreach ($keys as $key) {
$this->set($key, $this->get($key, $obj->get($key)));
}
return $this;
}
public function jsonSerialize()
{
return $this->toArray();
}
}