MongoResult.php
3.23 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
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
116
117
118
119
120
<?php
/**
* Created by PhpStorm.
* User: Zip
* Date: 15/6/14
* Time: 上午12:38
*/
namespace Hood\Dao\MongoDB;
class MongoResult
{
private $_attrs = array();
public function lastInsertId()
{
return $this->__get('_id')->__toString();
}
public function lastInsertMongoID()
{
return $this->__get('_id');
}
/**
* @return bool
*/
public function getStatus()
{
$status = $this->__get('status');
if (!empty($status['ok']) && $status['ok'] == 1) {
return true;
}
return false;
}
public function getStatusData()
{
return $this->__get('status');
}
public function getAttrs()
{
return $this->__get('attrs');
}
public function __get($name)
{
return $this->arrayGet($this->_attrs, $name);
}
public function __set($name, $val)
{
$this->_attrs = $this->arraySet($this->_attrs, $name, $val);
return $this;
}
private function arraySet(array $array, $keys, $value)
{
if (is_array($keys) && empty($keys)) {
return $array;
}
if (!is_array($keys)) {
if (strstr($keys, "`")) {
$keys = preg_replace_callback("/`(.+)`/U", create_function('$match', 'return str_replace(".", "\.", $match[1]);'), $keys);
}
$keys = preg_split("/(?<!\\\\)\\./", $keys);
}
if (count($keys) == 1) {
$firstKey = array_pop($keys);
$firstKey = str_replace("\\.", ".", $firstKey);
$array[$firstKey] = $value;
return $array;
}
$lastKey = array_pop($keys);
$lastKey = str_replace("\\.", ".", $lastKey);
$lastConfig = &$array;
foreach ($keys as $key) {
$key = str_replace("\\.", ".", $key);
if (!isset($lastConfig[$key]) || !is_array($lastConfig[$key])) {
$lastConfig[$key] = array();
}
$lastConfig = &$lastConfig[$key];
}
$lastConfig[$lastKey] = $value;
return $array;
}
private function arrayGet(array $array, $keys)
{
if (is_array($keys) && empty($keys)) {
return $array;
}
if (!is_array($keys)) {
if (strstr($keys, "`")) {
$keys = preg_replace_callback("/`(.+)`/U", create_function('$match', 'return str_replace(".", "\.", $match[1]);'), $keys);
}
$keys = preg_split("/(?<!\\\\)\\./", $keys);
}
if (count($keys) == 1) {
$firstKey = array_pop($keys);
$firstKey = str_replace("\\.", ".", $firstKey);
return array_key_exists($firstKey, $array) ? $array[$firstKey] : null;
}
$lastKey = array_pop($keys);
$lastKey = str_replace("\\.", ".", $lastKey);
$lastArray = $array;
foreach ($keys as $key) {
$key = str_replace("\\.", ".", $key);
if (is_array($lastArray) && array_key_exists($key, $lastArray)) {
$lastArray = $lastArray[$key];
} else {
return null;
}
}
return (is_array($lastArray) && array_key_exists($lastKey, $lastArray)) ? $lastArray[$lastKey] : null;
}
}