-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTypeQueryTrait.php
126 lines (114 loc) · 3.76 KB
/
TypeQueryTrait.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
<?php
/**
* Copyright (c) 2015, Muhannad Shelleh
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
namespace ItvisionSy\EsMapper;
/**
* This trait implements extra methods and override parent ones to implement the
* typed-level query class.
*
* @package ItvisionSy\EsMapper
* @author Muhannad Shelleh <[email protected]>
*
*/
trait TypeQueryTrait {
/**
* Overrides the parent class _all method to omit the type in parameters.
* @see Query::_all for details.
*
* @return Result|Model[]
*/
protected function _all() {
return parent::_all($this->type());
}
/**
* Overrides the parent class _query method to omit the type in parameters.
* @see Query::_query for details.
*
* @param array $query
* @return Result|Model[]
*/
protected function _query(array $query = array()) {
return parent::_query($this->type(), $query);
}
/**
* Overrides the parent class _find method to omit the type in parameters.
* @see Query::_find for details.
*
* @param mixed $id
* @return Result|Model[]
*/
protected function _find($id) {
return parent::_find($this->type(), $id);
}
/**
* Overrides the parent class _create method to omit the type in parameters.
* @see Query::_create for details.
*
* @param mixed $id
* @return Result|Model[]
*/
protected function _create(array $data, $id = null, array $parameters = []) {
return parent::_create($data, $this->type(), $id, $parameters);
}
/**
* Creates the full model class name
*
* It uses the query::modelClassNamePattern and typequery::modelClassName
* methods to create the full classname.
*
* @return string
*/
protected function _fullModelClass() {
$baseClassName = $this->modelClassName();
$classNamePattern = $this->modelClassNamePattern();
if ($classNamePattern && strpos($classNamePattern, '{type}') !== false) {
$fullClassName = str_replace("{type}", $baseClassName, $classNamePattern);
} elseif ($classNamePattern) {
$fullClassName = "{$classNamePattern}{$baseClassName}";
} else {
$fullClassName = "\\{$baseClassName}";
}
return $fullClassName;
}
/**
* Overrides the parent class _makeResult method to pass the correct model
* class name.
* @see Query::_makeResult for details.
*
* @param array $result
* @return Result|Model[]
*/
protected function _makeResult(array $result) {
return Result::make($result, $this->getClient())->setModelClass($this->_fullModelClass());
}
/**
* Overrides the parent class _makeModel method to pass the correct model
* class name.
* @see Query::_makeModel for details.
*
* @param array $source
* @return Model
*/
protected function _makeModel(array $source) {
return Model::makeOfType($source, $this->_fullModelClass(), $this->getClient());
}
/**
* Overrides the parent class _delete method to pass the correct type
* @see Query::_delete for details
*
* @param scalar $id
* @return array The elastic command result
*/
protected function _delete($id) {
return parent::_delete($this->type(), $id);
}
}