1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Respect\Structural\Driver\MongoDb; |
4
|
|
|
|
5
|
|
|
use Respect\Data\CollectionIterator; |
6
|
|
|
use Respect\Data\Collections\Collection; |
7
|
|
|
use Respect\Structural\Driver as BaseDriver; |
8
|
|
|
|
9
|
|
|
abstract class AbstractDriver implements BaseDriver |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @return mixed |
13
|
|
|
*/ |
14
|
|
|
abstract public function getDatabase(); |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param int|string $id |
18
|
|
|
* |
19
|
|
|
* @return mixed |
20
|
|
|
*/ |
21
|
|
|
abstract public function createObjectId($id); |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param Collection $collection |
25
|
|
|
* |
26
|
|
|
* @return array |
27
|
|
|
*/ |
28
|
6 |
|
public function generateQuery(Collection $collection) |
29
|
|
|
{ |
30
|
6 |
|
return $this->parseConditions($collection); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param Collection $collection |
35
|
|
|
* |
36
|
|
|
* @return array |
37
|
|
|
*/ |
38
|
6 |
|
protected function parseConditions(Collection $collection) |
39
|
|
|
{ |
40
|
6 |
|
$allCollections = CollectionIterator::recursive($collection); |
41
|
6 |
|
$allCollections = iterator_to_array($allCollections); |
42
|
6 |
|
$allCollections = array_slice($allCollections, 1); |
43
|
|
|
|
44
|
6 |
|
$condition = $this->getConditionArray($collection); |
45
|
|
|
|
46
|
6 |
|
foreach ($allCollections as $coll) { |
47
|
2 |
|
$condition += $this->getConditionArray($coll, true); |
48
|
6 |
|
} |
49
|
|
|
|
50
|
6 |
|
return $condition; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param Collection $collection |
55
|
|
|
* @param bool|false $prefix |
56
|
|
|
* |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
6 |
|
protected function getConditionArray(Collection $collection, $prefix = false) |
60
|
|
|
{ |
61
|
6 |
|
$condition = $collection->getCondition(); |
62
|
|
|
|
63
|
6 |
|
if (!is_array($condition)) { |
64
|
4 |
|
$condition = ['_id' => $this->createObjectId($condition)]; |
65
|
4 |
|
} |
66
|
|
|
|
67
|
6 |
|
if ($prefix) { |
68
|
2 |
|
$condition = static::prefixArrayKeys($condition, $collection->getName() . '.'); |
69
|
2 |
|
} |
70
|
|
|
|
71
|
6 |
|
return $condition; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param array $array |
76
|
|
|
* @param string $prefix |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
2 |
|
protected static function prefixArrayKeys(array $array, $prefix) |
81
|
|
|
{ |
82
|
2 |
|
return array_combine( |
83
|
2 |
|
array_map( |
84
|
2 |
|
function ($key) use ($prefix) { |
85
|
2 |
|
return "{$prefix}{$key}"; |
86
|
2 |
|
}, array_keys($array)), |
87
|
|
|
$array |
88
|
2 |
|
); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|