/
MockMapper.php
186 lines (164 loc) · 3.53 KB
/
MockMapper.php
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/**
* An array mapper for mocking tests
*
* $mapper->find(array('name' => 'Luke'));
* $mapper->find(array('name' => 'Luke'), 'Admin');
*
* @package TheCure
* @category Mapper
* @copyright Gignite, 2012
* @license MIT
*/
namespace TheCure\Mappers;
use TheCure\Models\Model;
abstract class MockMapper extends Mapper {
public $data = array();
/**
* [!!] Using self::$data so that all mapper data is
* shared against Mapper_Array and not sub classes.
*
* @param array|null $collection
* @return array
*/
protected function collection(array $collection = NULL)
{
if ($collection === NULL)
{
return $this->data;
}
$this->data = $collection;
}
/**
* @static
* @param $collection
* @param $where
* @param $callback
*/
public static function eachWhere($collection, $where, $callback)
{
foreach ($collection as $_key => $_row)
{
foreach ($where as $_field => $_needle)
{
$value = $_row->get($_field);
if (is_array($_needle)
AND isset($_needle['$in'])
AND in_array($value, $_needle['$in']))
{
// We were doing an $in query (like mongo)
}
elseif (is_array($value) AND in_array($_needle, $value))
{
// We were doing a contains query (like mongo)
}
elseif (is_string($_needle) AND strpos($_needle, '/') === 0 AND preg_match($_needle, $value))
{
// Regex search, and it matched
}
elseif ($value === NULL OR $value !== $_needle)
{
// We skip this result as it is either NULL
// or doesn't match our criteria
continue 2;
}
}
if (call_user_func($callback, $_key) === FALSE)
{
break;
}
}
}
/**
* @param array $where
* @param array|null $where
* @return Model|Collection
*/
public function find(array $where = NULL, $suffix = NULL)
{
$collection = $this->collection();
return $this->createCollection(
$where,
$suffix,
function($where) use ($collection)
{
$found = array();
MockMapper::eachWhere(
$collection,
$where,
function ($record) use ($collection, & $found)
{
$found[$record] = $collection[$record];
});
return new \ArrayIterator($found);
});
}
/**
* @param null $suffix
* @param null $where
* @return mixed
*/
public function findOne($where = NULL, $suffix = NULL)
{
$collection = $this->collection();
return $this->createModel(
$where,
$suffix,
function ($where) use ($collection)
{
if (isset($where['_id']))
{
if (isset($collection[$where['_id']]))
{
return $collection[$where['_id']];
}
}
elseif ($where)
{
$found = NULL;
MockMapper::eachWhere(
$collection,
$where,
function ($record) use ($collection, & $found)
{
$found = $collection[$record];
return FALSE;
});
return $found;
}
elseif ($first = current($collection))
{
return $first;
}
});
}
/**
* @param Model $model
*/
public function save(Model $model)
{
$collection = $this->collection();
$this->saveModel($model, function ($object) use (& $collection)
{
if ( ! isset($object->_id))
{
$object->_id = count($collection);
}
$collection[$object->_id] = $object;
return $object;
});
$this->collection($collection);
}
/**
* @param $model
*/
public function delete($model)
{
$collection = $this->collection();
$this->deleteModel($model, function ($where) use (& $collection)
{
unset($collection[$where['_id']]);
});
$this->collection($collection);
}
}