Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
72.73% |
8 / 11 |
CRAP | |
52.94% |
18 / 34 |
| AbstractQmpMoloquent | |
0.00% |
0 / 1 |
|
72.73% |
8 / 11 |
47.12 | |
52.94% |
18 / 34 |
| pipeline | |
100.00% |
1 / 1 |
2 | |
100.00% |
4 / 4 |
|||
| setPipeline | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| __get | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
| newEloquentBuilder | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| newBaseQueryBuilder | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| dotGet | |
0.00% |
0 / 1 |
30 | |
0.00% |
0 / 14 |
|||
| setKeyFilter | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| pullFilter | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| setKey | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| pushFilter | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| useQueryBuilder | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace Qmp\Laravel\MongoLow\Models; |
| 4 | |
| 5 | use Illuminate\Support\Facades\Log; |
| 6 | use Qmp\Laravel\MongoLow\MongoPipeline; |
| 7 | use Qmp\Laravel\MongoLow\Query\Builder as QueryBuilder; |
| 8 | use Qmp\Laravel\MongoLow\Eloquent\Builder; |
| 9 | |
| 10 | abstract class AbstractQmpMoloquent extends \MoloquentOrigin |
| 11 | { |
| 12 | /** |
| 13 | * The pipeline associated with the model |
| 14 | * |
| 15 | * @var Qmp\Laravel\MongoLow\MongoPipeline |
| 16 | */ |
| 17 | protected $pipeline; |
| 18 | |
| 19 | /** |
| 20 | * Return model pipeline for aggregation |
| 21 | * |
| 22 | * @return Qmp\Laravel\MongoLow\MongoPipeline |
| 23 | */ |
| 24 | public static function pipeline($model = null) |
| 25 | { |
| 26 | |
| 27 | |
| 28 | $model = $model ?? resolve(get_called_class()); |
| 29 | |
| 30 | if (!$model->pipeline) { |
| 31 | $model->setPipeline(new MongoPipeline($model)); |
| 32 | } |
| 33 | return $model->pipeline; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Set Model Pipeline |
| 38 | * |
| 39 | * @param MongoPipeline $pipeline |
| 40 | * @return void |
| 41 | */ |
| 42 | public function setPipeline(MongoPipeline $pipeline) |
| 43 | { |
| 44 | $this->pipeline = $pipeline; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Intercept pipeline or return parent property |
| 49 | * |
| 50 | * @param String $property |
| 51 | * @return mixed |
| 52 | */ |
| 53 | public function __get($property) |
| 54 | { |
| 55 | if ($property === 'pipeline') { |
| 56 | return $this->pipeline; |
| 57 | } |
| 58 | return parent::__get($property); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @inheritdoc |
| 63 | */ |
| 64 | public function newEloquentBuilder($query) |
| 65 | { |
| 66 | return new Builder($query); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @inheritdoc |
| 71 | */ |
| 72 | protected function newBaseQueryBuilder() |
| 73 | { |
| 74 | $connection = $this->getConnection(); |
| 75 | |
| 76 | return new QueryBuilder($connection, $connection->getPostProcessor()); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Get item if exist with dot chain |
| 81 | * |
| 82 | * example: |
| 83 | * $model = new Model(); |
| 84 | * $model->a = [ |
| 85 | * 'b' => ['c' => 'test !'] |
| 86 | * ]; |
| 87 | * $model->dotGet($model, 'a.b.c'); |
| 88 | * // Return 'test !' |
| 89 | * |
| 90 | * @param mixed $model |
| 91 | * @param string $chain |
| 92 | * @return mixed|null |
| 93 | */ |
| 94 | public function dotGet(string $chain, $model = null) |
| 95 | { |
| 96 | $arrayChain = explode('.', $chain); |
| 97 | $toSearch = array_shift($arrayChain); |
| 98 | $childs = implode('.', $arrayChain); |
| 99 | |
| 100 | if ($model === null) { |
| 101 | $model = $this; |
| 102 | } else if (is_array($model)) { |
| 103 | $model = (object) $model; |
| 104 | } |
| 105 | |
| 106 | try { |
| 107 | $child = $model->$toSearch; |
| 108 | } catch (\ErrorException $e) { |
| 109 | Log::debug('Unable to get chain:' . var_export(['chain' => $chain, 'model' => $model, 'message' => $e->getMessage(), 'line' => $e->getLine(), 'file' => $e->getFile()], true)); |
| 110 | return null; |
| 111 | } |
| 112 | |
| 113 | if (!empty($arrayChain)) { |
| 114 | return self::dotGet($childs, $child); |
| 115 | } |
| 116 | |
| 117 | return $child; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Add or set a key at position with arrayFilter. |
| 122 | * @param mixed $column |
| 123 | * @param mixed $value |
| 124 | * @param array $options |
| 125 | * @param bool $unique |
| 126 | * @return int |
| 127 | */ |
| 128 | public function setKeyFilter($column, $key, $value = null, $options = [], $unique = false) |
| 129 | { |
| 130 | return $this->useQueryBuilder('setKeyFilter', func_get_args()); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Remove one or more values from an array with arrayFilter. |
| 135 | * @param mixed $column |
| 136 | * @param mixed $value |
| 137 | * @param array $options |
| 138 | * @return int |
| 139 | */ |
| 140 | public function pullFilter($column, $value = null, $options = []) |
| 141 | { |
| 142 | return $this->useQueryBuilder('pullFilter', func_get_args()); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Set key at position |
| 147 | * |
| 148 | * @param mixed $column |
| 149 | * @param mixed $key |
| 150 | * @param mixed $value |
| 151 | * @return int |
| 152 | */ |
| 153 | public function setKey($key, $value) |
| 154 | { |
| 155 | return $this->useQueryBuilder('setKey', func_get_args()); |
| 156 | } |
| 157 | |
| 158 | /** Append one or more values to an array with arrayFilter. |
| 159 | * @param mixed $column |
| 160 | * @param mixed $value |
| 161 | * @param array $options |
| 162 | * @param bool $unique |
| 163 | * @return int |
| 164 | */ |
| 165 | public function pushFilter($column, $value = null, $options = [], $unique = false) |
| 166 | { |
| 167 | return $this->useQueryBuilder('pushFilter', func_get_args()); |
| 168 | } |
| 169 | |
| 170 | |
| 171 | protected function useQueryBuilder($method, $params) |
| 172 | { |
| 173 | $query = $this->setKeysForSaveQuery($this->newQuery()); |
| 174 | |
| 175 | $return = $query->{$method}(...$params); |
| 176 | $this->refresh(); |
| 177 | |
| 178 | return $return; |
| 179 | } |
| 180 | } |