Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Expr often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Expr, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Expr |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * The query criteria array. |
||
| 21 | * |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | private $query = []; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The "new object" array containing either a full document or a number of |
||
| 28 | * atomic update operators. |
||
| 29 | * |
||
| 30 | * @see docs.mongodb.org/manual/reference/method/db.collection.update/#update-parameter |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | private $newObj = []; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The current field we are operating on. |
||
| 37 | * |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | private $currentField; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The DocumentManager instance for this query |
||
| 44 | * |
||
| 45 | * @var DocumentManager |
||
| 46 | */ |
||
| 47 | private $dm; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * The ClassMetadata instance for the document being queried |
||
| 51 | * |
||
| 52 | * @var ClassMetadata |
||
| 53 | */ |
||
| 54 | private $class; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param DocumentManager $dm |
||
| 58 | */ |
||
| 59 | 404 | public function __construct(DocumentManager $dm) |
|
| 63 | |||
| 64 | /** |
||
| 65 | * Add one or more $and clauses to the current query. |
||
| 66 | * |
||
| 67 | * @see Builder::addAnd() |
||
| 68 | * @see http://docs.mongodb.org/manual/reference/operator/and/ |
||
| 69 | * @param array|Expr $expression |
||
| 70 | * @return $this |
||
| 71 | */ |
||
| 72 | 3 | View Code Duplication | public function addAnd($expression /*, $expression2, ... */) |
| 90 | |||
| 91 | /** |
||
| 92 | * Add one or more $nor clauses to the current query. |
||
| 93 | * |
||
| 94 | * @see Builder::addNor() |
||
| 95 | * @see http://docs.mongodb.org/manual/reference/operator/nor/ |
||
| 96 | * @param array|Expr $expression |
||
| 97 | * @return $this |
||
| 98 | */ |
||
| 99 | View Code Duplication | public function addNor($expression /* , $expression2, ... */) |
|
| 100 | { |
||
| 101 | if (! isset($this->query['$nor'])) { |
||
| 102 | $this->query['$nor'] = []; |
||
| 103 | } |
||
| 104 | |||
| 105 | $this->query['$nor'] = array_merge( |
||
| 106 | 1 | $this->query['$nor'], |
|
| 107 | array_map(function ($expression) { return $expression instanceof Expr ? $expression->getQuery() : $expression; }, func_get_args()) |
||
| 108 | 1 | ); |
|
| 109 | 1 | ||
| 110 | 1 | return $this; |
|
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Add one or more $or clauses to the current query. |
||
| 115 | * |
||
| 116 | * @see Builder::addOr() |
||
| 117 | * @see http://docs.mongodb.org/manual/reference/operator/or/ |
||
| 118 | * @param array|Expr $expression |
||
| 119 | * @return $this |
||
| 120 | */ |
||
| 121 | 1 | View Code Duplication | public function addOr($expression /* , $expression2, ... */) |
| 134 | |||
| 135 | /** |
||
| 136 | * Append one or more values to the current array field only if they do not |
||
| 137 | * already exist in the array. |
||
| 138 | * |
||
| 139 | * If the field does not exist, it will be set to an array containing the |
||
| 140 | * unique value(s) in the argument. If the field is not an array, the query |
||
| 141 | * will yield an error. |
||
| 142 | * |
||
| 143 | 5 | * Multiple values may be specified by provided an Expr object and using |
|
| 144 | * {@link Expr::each()}. |
||
| 145 | 5 | * |
|
| 146 | 5 | * @see Builder::addToSet() |
|
| 147 | * @see http://docs.mongodb.org/manual/reference/operator/addToSet/ |
||
| 148 | * @see http://docs.mongodb.org/manual/reference/operator/each/ |
||
| 149 | 5 | * @param mixed|Expr $valueOrExpression |
|
| 150 | 5 | * @return $this |
|
| 151 | */ |
||
| 152 | View Code Duplication | public function addToSet($valueOrExpression) |
|
| 153 | { |
||
| 154 | 5 | if ($valueOrExpression instanceof Expr) { |
|
| 155 | $valueOrExpression = $valueOrExpression->getQuery(); |
||
| 156 | } |
||
| 157 | |||
| 158 | $this->requiresCurrentField(); |
||
| 159 | $this->newObj['$addToSet'][$this->currentField] = $valueOrExpression; |
||
| 160 | return $this; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Specify $all criteria for the current field. |
||
| 165 | * |
||
| 166 | * @see Builder::all() |
||
| 167 | * @see http://docs.mongodb.org/manual/reference/operator/all/ |
||
| 168 | * @param array $values |
||
| 169 | * @return $this |
||
| 170 | */ |
||
| 171 | public function all(array $values) |
||
| 172 | { |
||
| 173 | return $this->operator('$all', (array) $values); |
||
| 174 | 5 | } |
|
| 175 | |||
| 176 | 5 | /** |
|
| 177 | 1 | * Apply a bitwise operation on the current field |
|
| 178 | * |
||
| 179 | * @see http://docs.mongodb.org/manual/reference/operator/update/bit/ |
||
| 180 | 5 | * @param string $operator |
|
| 181 | 5 | * @param int $value |
|
| 182 | 5 | * @return $this |
|
| 183 | */ |
||
| 184 | protected function bit($operator, $value) |
||
| 185 | { |
||
| 186 | $this->requiresCurrentField(); |
||
| 187 | $this->newObj['$bit'][$this->currentField][$operator] = $value; |
||
| 188 | return $this; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Apply a bitwise and operation on the current field. |
||
| 193 | 2 | * |
|
| 194 | * @see Builder::bitAnd() |
||
| 195 | 2 | * @see http://docs.mongodb.org/manual/reference/operator/update/bit/ |
|
| 196 | * @param int $value |
||
| 197 | * @return $this |
||
| 198 | */ |
||
| 199 | public function bitAnd($value) |
||
| 200 | { |
||
| 201 | return $this->bit('and', $value); |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Apply a bitwise or operation on the current field. |
||
| 206 | 3 | * |
|
| 207 | * @see Builder::bitOr() |
||
| 208 | 3 | * @see http://docs.mongodb.org/manual/reference/operator/update/bit/ |
|
| 209 | 3 | * @param int $value |
|
| 210 | 3 | * @return $this |
|
| 211 | */ |
||
| 212 | public function bitOr($value) |
||
| 213 | { |
||
| 214 | return $this->bit('or', $value); |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Matches documents where all of the bit positions given by the query are |
||
| 219 | * clear. |
||
| 220 | * |
||
| 221 | 1 | * @see Builder::bitsAllClear() |
|
| 222 | * @see https://docs.mongodb.org/manual/reference/operator/query/bitsAllClear/ |
||
| 223 | 1 | * @param int|array|\MongoBinData $value |
|
| 224 | * @return $this |
||
| 225 | */ |
||
| 226 | public function bitsAllClear($value) |
||
| 227 | { |
||
| 228 | $this->requiresCurrentField(); |
||
| 229 | return $this->operator('$bitsAllClear', $value); |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Matches documents where all of the bit positions given by the query are |
||
| 234 | 1 | * set. |
|
| 235 | * |
||
| 236 | 1 | * @see Builder::bitsAllSet() |
|
| 237 | * @see https://docs.mongodb.org/manual/reference/operator/query/bitsAllSet/ |
||
| 238 | * @param int|array|\MongoBinData $value |
||
| 239 | * @return $this |
||
| 240 | */ |
||
| 241 | public function bitsAllSet($value) |
||
| 242 | { |
||
| 243 | $this->requiresCurrentField(); |
||
| 244 | return $this->operator('$bitsAllSet', $value); |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Matches documents where any of the bit positions given by the query are |
||
| 249 | * clear. |
||
| 250 | * |
||
| 251 | * @see Builder::bitsAnyClear() |
||
| 252 | * @see https://docs.mongodb.org/manual/reference/operator/query/bitsAnyClear/ |
||
| 253 | * @param int|array|\MongoBinData $value |
||
| 254 | * @return $this |
||
| 255 | */ |
||
| 256 | public function bitsAnyClear($value) |
||
| 257 | { |
||
| 258 | $this->requiresCurrentField(); |
||
| 259 | return $this->operator('$bitsAnyClear', $value); |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Matches documents where any of the bit positions given by the query are |
||
| 264 | * set. |
||
| 265 | * |
||
| 266 | * @see Builder::bitsAnySet() |
||
| 267 | * @see https://docs.mongodb.org/manual/reference/operator/query/bitsAnySet/ |
||
| 268 | * @param int|array|\MongoBinData $value |
||
| 269 | * @return $this |
||
| 270 | */ |
||
| 271 | public function bitsAnySet($value) |
||
| 272 | { |
||
| 273 | $this->requiresCurrentField(); |
||
| 274 | return $this->operator('$bitsAnySet', $value); |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Apply a bitwise xor operation on the current field. |
||
| 279 | * |
||
| 280 | * @see Builder::bitXor() |
||
| 281 | * @see http://docs.mongodb.org/manual/reference/operator/update/bit/ |
||
| 282 | * @param int $value |
||
| 283 | * @return $this |
||
| 284 | */ |
||
| 285 | public function bitXor($value) |
||
| 286 | { |
||
| 287 | return $this->bit('xor', $value); |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * A boolean flag to enable or disable case sensitive search for $text |
||
| 292 | * criteria. |
||
| 293 | * |
||
| 294 | * This method must be called after text(). |
||
| 295 | * |
||
| 296 | * @see Builder::caseSensitive() |
||
| 297 | * @see http://docs.mongodb.org/manual/reference/operator/text/ |
||
| 298 | * @param bool $caseSensitive |
||
| 299 | * @return $this |
||
| 300 | * @throws \BadMethodCallException if the query does not already have $text criteria |
||
| 301 | * |
||
| 302 | * @since 1.3 |
||
| 303 | */ |
||
| 304 | View Code Duplication | public function caseSensitive($caseSensitive) |
|
| 305 | { |
||
| 306 | if ( ! isset($this->query['$text'])) { |
||
| 307 | 1 | throw new \BadMethodCallException('This method requires a $text operator (call text() first)'); |
|
| 308 | } |
||
| 309 | 1 | ||
| 310 | // Remove caseSensitive option to keep support for older database versions |
||
| 311 | if ($caseSensitive) { |
||
| 312 | $this->query['$text']['$caseSensitive'] = true; |
||
| 313 | } elseif (isset($this->query['$text']['$caseSensitive'])) { |
||
| 314 | unset($this->query['$text']['$caseSensitive']); |
||
| 315 | } |
||
| 316 | |||
| 317 | return $this; |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Associates a comment to any expression taking a query predicate. |
||
| 322 | * |
||
| 323 | * @see Builder::comment() |
||
| 324 | * @see http://docs.mongodb.org/manual/reference/operator/query/comment/ |
||
| 325 | * @param string $comment |
||
| 326 | 3 | * @return $this |
|
| 327 | */ |
||
| 328 | 3 | public function comment($comment) |
|
| 329 | 1 | { |
|
| 330 | $this->query['$comment'] = $comment; |
||
| 331 | return $this; |
||
| 332 | } |
||
| 333 | 2 | ||
| 334 | 2 | /** |
|
| 335 | 1 | * Sets the value of the current field to the current date, either as a date or a timestamp. |
|
| 336 | 1 | * |
|
| 337 | * @see Builder::currentDate() |
||
| 338 | * @see http://docs.mongodb.org/manual/reference/operator/update/currentDate/ |
||
| 339 | 2 | * @param string $type |
|
| 340 | * @return $this |
||
| 341 | * @throws \InvalidArgumentException if an invalid type is given |
||
| 342 | */ |
||
| 343 | public function currentDate($type = 'date') |
||
| 344 | { |
||
| 345 | if (! in_array($type, ['date', 'timestamp'])) { |
||
| 346 | throw new \InvalidArgumentException('Type for currentDate operator must be date or timestamp.'); |
||
| 347 | } |
||
| 348 | |||
| 349 | $this->requiresCurrentField(); |
||
| 350 | $this->newObj['$currentDate'][$this->currentField]['$type'] = $type; |
||
| 351 | return $this; |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * A boolean flag to enable or disable diacritic sensitive search for $text |
||
| 356 | * criteria. |
||
| 357 | * |
||
| 358 | * This method must be called after text(). |
||
| 359 | * |
||
| 360 | * @see Builder::diacriticSensitive() |
||
| 361 | * @see http://docs.mongodb.org/manual/reference/operator/text/ |
||
| 362 | * @param bool $diacriticSensitive |
||
| 363 | * @return $this |
||
| 364 | * @throws \BadMethodCallException if the query does not already have $text criteria |
||
| 365 | 3 | * |
|
| 366 | * @since 1.3 |
||
| 367 | 3 | */ |
|
| 368 | 1 | View Code Duplication | public function diacriticSensitive($diacriticSensitive) |
| 369 | { |
||
| 370 | if ( ! isset($this->query['$text'])) { |
||
| 371 | 2 | throw new \BadMethodCallException('This method requires a $text operator (call text() first)'); |
|
| 372 | 2 | } |
|
| 373 | 2 | ||
| 374 | // Remove diacriticSensitive option to keep support for older database versions |
||
| 375 | if ($diacriticSensitive) { |
||
| 376 | $this->query['$text']['$diacriticSensitive'] = true; |
||
| 377 | } elseif (isset($this->query['$text']['$diacriticSensitive'])) { |
||
| 378 | unset($this->query['$text']['$diacriticSensitive']); |
||
| 379 | } |
||
| 380 | |||
| 381 | return $this; |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Add $each criteria to the expression for a $push operation. |
||
| 386 | * |
||
| 387 | * @see Expr::push() |
||
| 388 | * @see http://docs.mongodb.org/manual/reference/operator/each/ |
||
| 389 | * @param array $values |
||
| 390 | 3 | * @return $this |
|
| 391 | */ |
||
| 392 | 3 | public function each(array $values) |
|
| 396 | |||
| 397 | 2 | /** |
|
| 398 | 2 | * Specify $elemMatch criteria for the current field. |
|
| 399 | 1 | * |
|
| 400 | 1 | * @see Builder::elemMatch() |
|
| 401 | * @see http://docs.mongodb.org/manual/reference/operator/elemMatch/ |
||
| 402 | * @param array|Expr $expression |
||
| 403 | 2 | * @return $this |
|
| 404 | */ |
||
| 405 | public function elemMatch($expression) |
||
| 406 | { |
||
| 407 | return $this->operator('$elemMatch', $expression instanceof Expr ? $expression->getQuery() : $expression); |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Specify an equality match for the current field. |
||
| 412 | * |
||
| 413 | * @see Builder::equals() |
||
| 414 | 4 | * @param mixed $value |
|
| 415 | * @return $this |
||
| 416 | 4 | */ |
|
| 417 | public function equals($value) |
||
| 418 | { |
||
| 419 | if ($this->currentField) { |
||
| 420 | $this->query[$this->currentField] = $value; |
||
| 421 | } else { |
||
| 422 | $this->query = $value; |
||
| 423 | } |
||
| 424 | return $this; |
||
| 425 | } |
||
| 426 | |||
| 427 | 4 | /** |
|
| 428 | * Specify $exists criteria for the current field. |
||
| 429 | 4 | * |
|
| 430 | * @see Builder::exists() |
||
| 431 | * @see http://docs.mongodb.org/manual/reference/operator/exists/ |
||
| 432 | * @param boolean $bool |
||
| 433 | * @return $this |
||
| 434 | */ |
||
| 435 | public function exists($bool) |
||
| 436 | { |
||
| 437 | return $this->operator('$exists', (boolean) $bool); |
||
| 438 | } |
||
| 439 | 100 | ||
| 440 | /** |
||
| 441 | 100 | * Set the current field for building the expression. |
|
| 442 | 99 | * |
|
| 443 | * @see Builder::field() |
||
| 444 | 1 | * @param string $field |
|
| 445 | * @return $this |
||
| 446 | 100 | */ |
|
| 447 | public function field($field) |
||
| 448 | { |
||
| 449 | $this->currentField = (string) $field; |
||
| 450 | return $this; |
||
| 451 | } |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Add $geoIntersects criteria with a GeoJSON geometry to the expression. |
||
| 455 | * |
||
| 456 | * The geometry parameter GeoJSON object or an array corresponding to the |
||
| 457 | 5 | * geometry's JSON representation. |
|
| 458 | * |
||
| 459 | 5 | * @see Builder::geoIntersects() |
|
| 460 | * @see http://docs.mongodb.org/manual/reference/operator/geoIntersects/ |
||
| 461 | * @param array|Geometry $geometry |
||
| 462 | * @return $this |
||
| 463 | */ |
||
| 464 | View Code Duplication | public function geoIntersects($geometry) |
|
| 465 | { |
||
| 466 | if ($geometry instanceof Geometry) { |
||
| 467 | $geometry = $geometry->jsonSerialize(); |
||
| 468 | } |
||
| 469 | 192 | ||
| 470 | return $this->operator('$geoIntersects', ['$geometry' => $geometry]); |
||
| 471 | 192 | } |
|
| 472 | 192 | ||
| 473 | /** |
||
| 474 | * Add $geoWithin criteria with a GeoJSON geometry to the expression. |
||
| 475 | * |
||
| 476 | * The geometry parameter GeoJSON object or an array corresponding to the |
||
| 477 | * geometry's JSON representation. |
||
| 478 | * |
||
| 479 | * @see Builder::geoWithin() |
||
| 480 | * @see http://docs.mongodb.org/manual/reference/operator/geoIntersects/ |
||
| 481 | * @param array|Geometry $geometry |
||
| 482 | * @return $this |
||
| 483 | */ |
||
| 484 | View Code Duplication | public function geoWithin($geometry) |
|
| 485 | { |
||
| 486 | 2 | if ($geometry instanceof Geometry) { |
|
| 487 | $geometry = $geometry->jsonSerialize(); |
||
| 488 | 2 | } |
|
| 489 | 1 | ||
| 490 | return $this->operator('$geoWithin', ['$geometry' => $geometry]); |
||
| 491 | } |
||
| 492 | 2 | ||
| 493 | /** |
||
| 494 | * Add $geoWithin criteria with a $box shape to the expression. |
||
| 495 | * |
||
| 496 | * A rectangular polygon will be constructed from a pair of coordinates |
||
| 497 | * corresponding to the bottom left and top right corners. |
||
| 498 | * |
||
| 499 | * Note: the $box operator only supports legacy coordinate pairs and 2d |
||
| 500 | * indexes. This cannot be used with 2dsphere indexes and GeoJSON shapes. |
||
| 501 | * |
||
| 502 | * @see Builder::geoWithinBox() |
||
| 503 | * @see http://docs.mongodb.org/manual/reference/operator/box/ |
||
| 504 | * @param float $x1 |
||
| 505 | * @param float $y1 |
||
| 506 | 2 | * @param float $x2 |
|
| 507 | * @param float $y2 |
||
| 508 | 2 | * @return $this |
|
| 509 | 1 | */ |
|
| 510 | public function geoWithinBox($x1, $y1, $x2, $y2) |
||
| 511 | { |
||
| 512 | 2 | $shape = ['$box' => [[$x1, $y1], [$x2, $y2]]]; |
|
| 513 | |||
| 514 | return $this->operator('$geoWithin', $shape); |
||
| 515 | } |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Add $geoWithin criteria with a $center shape to the expression. |
||
| 519 | * |
||
| 520 | * Note: the $center operator only supports legacy coordinate pairs and 2d |
||
| 521 | * indexes. This cannot be used with 2dsphere indexes and GeoJSON shapes. |
||
| 522 | * |
||
| 523 | * @see Builider::geoWithinCenter() |
||
| 524 | * @see http://docs.mongodb.org/manual/reference/operator/center/ |
||
| 525 | * @param float $x |
||
| 526 | * @param float $y |
||
| 527 | * @param float $radius |
||
| 528 | * @return $this |
||
| 529 | */ |
||
| 530 | public function geoWithinCenter($x, $y, $radius) |
||
| 531 | { |
||
| 532 | 1 | $shape = ['$center' => [[$x, $y], $radius]]; |
|
| 533 | |||
| 534 | 1 | return $this->operator('$geoWithin', $shape); |
|
| 535 | } |
||
| 536 | 1 | ||
| 537 | /** |
||
| 538 | * Add $geoWithin criteria with a $centerSphere shape to the expression. |
||
| 539 | * |
||
| 540 | * Note: the $centerSphere operator supports both 2d and 2dsphere indexes. |
||
| 541 | * |
||
| 542 | * @see Builder::geoWithinCenterSphere() |
||
| 543 | * @see http://docs.mongodb.org/manual/reference/operator/centerSphere/ |
||
| 544 | * @param float $x |
||
| 545 | * @param float $y |
||
| 546 | * @param float $radius |
||
| 547 | * @return $this |
||
| 548 | */ |
||
| 549 | public function geoWithinCenterSphere($x, $y, $radius) |
||
| 550 | { |
||
| 551 | $shape = ['$centerSphere' => [[$x, $y], $radius]]; |
||
| 552 | 1 | ||
| 553 | return $this->operator('$geoWithin', $shape); |
||
| 554 | 1 | } |
|
| 555 | |||
| 556 | 1 | /** |
|
| 557 | * Add $geoWithin criteria with a $polygon shape to the expression. |
||
| 558 | * |
||
| 559 | * Point coordinates are in x, y order (easting, northing for projected |
||
| 560 | * coordinates, longitude, latitude for geographic coordinates). |
||
| 561 | * |
||
| 562 | * The last point coordinate is implicitly connected with the first. |
||
| 563 | * |
||
| 564 | * Note: the $polygon operator only supports legacy coordinate pairs and 2d |
||
| 565 | * indexes. This cannot be used with 2dsphere indexes and GeoJSON shapes. |
||
| 566 | * |
||
| 567 | * @see Builder::geoWithinPolygon() |
||
| 568 | * @see http://docs.mongodb.org/manual/reference/operator/polygon/ |
||
| 569 | * @param array $point,... Three or more point coordinate tuples |
||
| 570 | * @return $this |
||
| 571 | 1 | * @throws \InvalidArgumentException if less than three points are given |
|
| 572 | */ |
||
| 573 | 1 | public function geoWithinPolygon(/* array($x1, $y1), ... */) |
|
| 574 | { |
||
| 575 | 1 | if (func_num_args() < 3) { |
|
| 576 | throw new \InvalidArgumentException('Polygon must be defined by three or more points.'); |
||
| 577 | } |
||
| 578 | |||
| 579 | $shape = ['$polygon' => func_get_args()]; |
||
| 580 | |||
| 581 | return $this->operator('$geoWithin', $shape); |
||
| 582 | } |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Return the current field. |
||
| 586 | * |
||
| 587 | * @return string |
||
| 588 | */ |
||
| 589 | public function getCurrentField() |
||
| 590 | { |
||
| 591 | return $this->currentField; |
||
| 592 | } |
||
| 593 | |||
| 594 | /** |
||
| 595 | 2 | * Gets prepared newObj part of expression. |
|
| 596 | * |
||
| 597 | 2 | * @return array |
|
| 598 | 1 | */ |
|
| 599 | public function getNewObj() |
||
| 600 | { |
||
| 601 | 1 | return $this->dm->getUnitOfWork() |
|
| 602 | ->getDocumentPersister($this->class->name) |
||
| 603 | 1 | ->prepareQueryOrNewObj($this->newObj, true); |
|
| 604 | } |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Gets prepared query part of expression. |
||
| 608 | * |
||
| 609 | * @return array |
||
| 610 | */ |
||
| 611 | 2 | public function getQuery() |
|
| 612 | { |
||
| 613 | 2 | return $this->dm->getUnitOfWork() |
|
| 614 | ->getDocumentPersister($this->class->name) |
||
| 615 | ->prepareQueryOrNewObj($this->query); |
||
| 616 | } |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Specify $gt criteria for the current field. |
||
| 620 | * |
||
| 621 | 175 | * @see Builder::gt() |
|
| 622 | * @see http://docs.mongodb.org/manual/reference/operator/gt/ |
||
| 623 | 175 | * @param mixed $value |
|
| 624 | 175 | * @return $this |
|
| 625 | 175 | */ |
|
| 626 | public function gt($value) |
||
| 627 | { |
||
| 628 | return $this->operator('$gt', $value); |
||
| 629 | } |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Specify $gte criteria for the current field. |
||
| 633 | 254 | * |
|
| 634 | * @see Builder::gte() |
||
| 635 | 254 | * @see http://docs.mongodb.org/manual/reference/operator/gte/ |
|
| 636 | 254 | * @param mixed $value |
|
| 637 | 254 | * @return $this |
|
| 638 | */ |
||
| 639 | public function gte($value) |
||
| 640 | { |
||
| 641 | return $this->operator('$gte', $value); |
||
| 642 | } |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Specify $in criteria for the current field. |
||
| 646 | * |
||
| 647 | * @see Builder::in() |
||
| 648 | 2 | * @see http://docs.mongodb.org/manual/reference/operator/in/ |
|
| 649 | * @param array $values |
||
| 650 | 2 | * @return $this |
|
| 651 | */ |
||
| 652 | public function in(array $values) |
||
| 653 | { |
||
| 654 | return $this->operator('$in', array_values($values)); |
||
| 655 | } |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Increment the current field. |
||
| 659 | * |
||
| 660 | * If the field does not exist, it will be set to this value. |
||
| 661 | 2 | * |
|
| 662 | * @see Builder::inc() |
||
| 663 | 2 | * @see http://docs.mongodb.org/manual/reference/operator/inc/ |
|
| 664 | * @param float|integer $value |
||
| 665 | * @return $this |
||
| 666 | */ |
||
| 667 | public function inc($value) |
||
| 668 | { |
||
| 669 | $this->requiresCurrentField(); |
||
| 670 | $this->newObj['$inc'][$this->currentField] = $value; |
||
| 671 | return $this; |
||
| 672 | } |
||
| 673 | |||
| 674 | 31 | /** |
|
| 675 | * Checks that the current field includes a reference to the supplied document. |
||
| 676 | 31 | * |
|
| 677 | * @param object $document |
||
| 678 | * @return Expr |
||
| 679 | */ |
||
| 680 | View Code Duplication | public function includesReferenceTo($document) |
|
| 681 | { |
||
| 682 | $this->requiresCurrentField(); |
||
| 683 | $mapping = $this->getReferenceMapping(); |
||
| 684 | $reference = $this->dm->createReference($document, $mapping); |
||
| 685 | $storeAs = array_key_exists('storeAs', $mapping) ? $mapping['storeAs'] : null; |
||
| 686 | |||
| 687 | switch ($storeAs) { |
||
| 688 | case ClassMetadataInfo::REFERENCE_STORE_AS_ID: |
||
| 689 | 5 | $this->query[$mapping['name']] = $reference; |
|
| 690 | return $this; |
||
| 691 | 5 | break; |
|
| 692 | 5 | ||
| 693 | 5 | case ClassMetadataInfo::REFERENCE_STORE_AS_REF: |
|
| 694 | $keys = ['id' => true]; |
||
| 695 | break; |
||
| 696 | |||
| 697 | case ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF: |
||
| 698 | case ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF_WITH_DB: |
||
| 699 | $keys = ['$ref' => true, '$id' => true, '$db' => true]; |
||
| 700 | |||
| 701 | if ($storeAs === ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF) { |
||
| 702 | 6 | unset($keys['$db']); |
|
| 703 | } |
||
| 704 | 6 | ||
| 705 | 6 | if (isset($mapping['targetDocument'])) { |
|
| 706 | 4 | unset($keys['$ref'], $keys['$db']); |
|
| 707 | 4 | } |
|
| 708 | break; |
||
| 709 | |||
| 710 | 4 | default: |
|
| 711 | 2 | throw new \InvalidArgumentException("Reference type {$storeAs} is invalid."); |
|
| 712 | 2 | } |
|
| 713 | |||
| 714 | foreach ($keys as $key => $value) { |
||
| 715 | 3 | $this->query[$mapping['name']]['$elemMatch'][$key] = $reference[$key]; |
|
| 716 | } |
||
| 717 | |||
| 718 | return $this; |
||
| 719 | 3 | } |
|
| 720 | 1 | ||
| 721 | 3 | /** |
|
| 722 | * Set the $language option for $text criteria. |
||
| 723 | 3 | * |
|
| 724 | 2 | * This method must be called after text(). |
|
| 725 | * |
||
| 726 | * @see Builder::language() |
||
| 727 | 3 | * @see http://docs.mongodb.org/manual/reference/operator/text/ |
|
| 728 | 1 | * @param string $language |
|
| 729 | * @return $this |
||
| 730 | 3 | * @throws \BadMethodCallException if the query does not already have $text criteria |
|
| 731 | */ |
||
| 732 | public function language($language) |
||
| 733 | { |
||
| 734 | if ( ! isset($this->query['$text'])) { |
||
| 735 | throw new \BadMethodCallException('This method requires a $text operator (call text() first)'); |
||
| 736 | 3 | } |
|
| 737 | 3 | ||
| 738 | $this->query['$text']['$language'] = (string) $language; |
||
| 739 | |||
| 740 | return $this; |
||
| 741 | } |
||
| 742 | |||
| 743 | /** |
||
| 744 | * Specify $lt criteria for the current field. |
||
| 745 | 3 | * |
|
| 746 | * @see Builder::lte() |
||
| 747 | * @see http://docs.mongodb.org/manual/reference/operator/lte/ |
||
| 748 | * @param mixed $value |
||
| 749 | * @return $this |
||
| 750 | */ |
||
| 751 | public function lt($value) |
||
| 752 | { |
||
| 753 | return $this->operator('$lt', $value); |
||
| 754 | } |
||
| 755 | |||
| 756 | /** |
||
| 757 | * Specify $lte criteria for the current field. |
||
| 758 | * |
||
| 759 | 2 | * @see Builder::lte() |
|
| 760 | * @see http://docs.mongodb.org/manual/reference/operator/lte/ |
||
| 761 | 2 | * @param mixed $value |
|
| 762 | 1 | * @return $this |
|
| 763 | */ |
||
| 764 | public function lte($value) |
||
| 768 | |||
| 769 | /** |
||
| 770 | * Updates the value of the field to a specified value if the specified value is greater than the current value of the field. |
||
| 771 | * |
||
| 772 | * @see Builder::max() |
||
| 773 | * @see http://docs.mongodb.org/manual/reference/operator/update/max/ |
||
| 774 | * @param mixed $value |
||
| 775 | * @return $this |
||
| 776 | */ |
||
| 777 | public function max($value) |
||
| 778 | 4 | { |
|
| 779 | $this->requiresCurrentField(); |
||
| 780 | 4 | $this->newObj['$max'][$this->currentField] = $value; |
|
| 781 | return $this; |
||
| 782 | } |
||
| 783 | |||
| 784 | /** |
||
| 785 | * Updates the value of the field to a specified value if the specified value is less than the current value of the field. |
||
| 786 | * |
||
| 787 | * @see Builder::min() |
||
| 788 | * @see http://docs.mongodb.org/manual/reference/operator/update/min/ |
||
| 789 | * @param mixed $value |
||
| 790 | * @return $this |
||
| 791 | 2 | */ |
|
| 792 | public function min($value) |
||
| 793 | 2 | { |
|
| 794 | $this->requiresCurrentField(); |
||
| 795 | $this->newObj['$min'][$this->currentField] = $value; |
||
| 796 | return $this; |
||
| 797 | } |
||
| 798 | |||
| 799 | /** |
||
| 800 | * Specify $mod criteria for the current field. |
||
| 801 | * |
||
| 802 | * @see Builder::mod() |
||
| 803 | * @see http://docs.mongodb.org/manual/reference/operator/mod/ |
||
| 804 | * @param float|integer $divisor |
||
| 805 | * @param float|integer $remainder |
||
| 806 | * @return $this |
||
| 807 | */ |
||
| 808 | public function mod($divisor, $remainder = 0) |
||
| 809 | { |
||
| 810 | return $this->operator('$mod', [$divisor, $remainder]); |
||
| 811 | } |
||
| 812 | |||
| 813 | /** |
||
| 814 | * Multiply the current field. |
||
| 815 | * |
||
| 816 | * If the field does not exist, it will be set to 0. |
||
| 817 | * |
||
| 818 | * @see Builder::mul() |
||
| 819 | * @see http://docs.mongodb.org/manual/reference/operator/mul/ |
||
| 820 | * @param float|integer $value |
||
| 821 | * @return $this |
||
| 822 | */ |
||
| 823 | public function mul($value) |
||
| 824 | { |
||
| 825 | $this->requiresCurrentField(); |
||
| 826 | $this->newObj['$mul'][$this->currentField] = $value; |
||
| 827 | return $this; |
||
| 828 | } |
||
| 829 | |||
| 830 | /** |
||
| 831 | * Add $near criteria to the expression. |
||
| 832 | * |
||
| 833 | * A GeoJSON point may be provided as the first and only argument for |
||
| 834 | * 2dsphere queries. This single parameter may be a GeoJSON point object or |
||
| 835 | * an array corresponding to the point's JSON representation. |
||
| 836 | * |
||
| 837 | * @see Builder::near() |
||
| 838 | * @see http://docs.mongodb.org/manual/reference/operator/near/ |
||
| 839 | * @param float|array|Point $x |
||
| 840 | * @param float $y |
||
| 841 | * @return $this |
||
| 842 | */ |
||
| 843 | View Code Duplication | public function near($x, $y = null) |
|
| 844 | { |
||
| 845 | if ($x instanceof Point) { |
||
| 846 | $x = $x->jsonSerialize(); |
||
| 847 | } |
||
| 848 | |||
| 849 | if (is_array($x)) { |
||
| 850 | return $this->operator('$near', ['$geometry' => $x]); |
||
| 851 | } |
||
| 852 | |||
| 853 | return $this->operator('$near', [$x, $y]); |
||
| 854 | } |
||
| 855 | |||
| 856 | /** |
||
| 857 | * Add $nearSphere criteria to the expression. |
||
| 858 | * |
||
| 859 | * A GeoJSON point may be provided as the first and only argument for |
||
| 860 | * 2dsphere queries. This single parameter may be a GeoJSON point object or |
||
| 861 | * an array corresponding to the point's JSON representation. |
||
| 862 | * |
||
| 863 | * @see Builder::nearSphere() |
||
| 864 | * @see http://docs.mongodb.org/manual/reference/operator/nearSphere/ |
||
| 865 | * @param float|array|Point $x |
||
| 866 | * @param float $y |
||
| 867 | * @return $this |
||
| 868 | */ |
||
| 869 | View Code Duplication | public function nearSphere($x, $y = null) |
|
| 881 | |||
| 882 | /** |
||
| 883 | * Negates an expression for the current field. |
||
| 884 | * |
||
| 885 | * @see Builder::not() |
||
| 886 | * @see http://docs.mongodb.org/manual/reference/operator/not/ |
||
| 887 | * @param array|Expr $expression |
||
| 888 | * @return $this |
||
| 889 | */ |
||
| 890 | public function not($expression) |
||
| 891 | { |
||
| 892 | return $this->operator('$not', $expression instanceof Expr ? $expression->getQuery() : $expression); |
||
| 893 | } |
||
| 894 | |||
| 895 | /** |
||
| 896 | 3 | * Specify $ne criteria for the current field. |
|
| 897 | * |
||
| 898 | 3 | * @see Builder::notEqual() |
|
| 899 | 1 | * @see http://docs.mongodb.org/manual/reference/operator/ne/ |
|
| 900 | * @param mixed $value |
||
| 901 | * @return $this |
||
| 902 | 3 | */ |
|
| 903 | 2 | public function notEqual($value) |
|
| 907 | |||
| 908 | /** |
||
| 909 | * Specify $nin criteria for the current field. |
||
| 910 | * |
||
| 911 | * @see Builder::notIn() |
||
| 912 | * @see http://docs.mongodb.org/manual/reference/operator/nin/ |
||
| 913 | * @param array $values |
||
| 914 | * @return $this |
||
| 915 | */ |
||
| 916 | public function notIn(array $values) |
||
| 920 | |||
| 921 | /** |
||
| 922 | * Defines an operator and value on the expression. |
||
| 923 | * |
||
| 924 | * If there is a current field, the operator will be set on it; otherwise, |
||
| 925 | * the operator is set at the top level of the query. |
||
| 926 | * |
||
| 927 | * @param string $operator |
||
| 928 | * @param mixed $value |
||
| 929 | * @return $this |
||
| 930 | 6 | */ |
|
| 931 | public function operator($operator, $value) |
||
| 932 | 6 | { |
|
| 933 | $this->wrapEqualityCriteria(); |
||
| 934 | |||
| 935 | if ($this->currentField) { |
||
| 936 | $this->query[$this->currentField][$operator] = $value; |
||
| 937 | } else { |
||
| 938 | $this->query[$operator] = $value; |
||
| 939 | } |
||
| 940 | return $this; |
||
| 941 | } |
||
| 942 | |||
| 943 | 6 | /** |
|
| 944 | * Remove the first element from the current array field. |
||
| 945 | 6 | * |
|
| 946 | * @see Builder::popFirst() |
||
| 947 | * @see http://docs.mongodb.org/manual/reference/operator/pop/ |
||
| 948 | * @return $this |
||
| 949 | */ |
||
| 950 | public function popFirst() |
||
| 951 | { |
||
| 952 | $this->requiresCurrentField(); |
||
| 953 | $this->newObj['$pop'][$this->currentField] = 1; |
||
| 954 | return $this; |
||
| 955 | } |
||
| 956 | |||
| 957 | /** |
||
| 958 | 85 | * Remove the last element from the current array field. |
|
| 959 | * |
||
| 960 | 85 | * @see Builder::popLast() |
|
| 961 | * @see http://docs.mongodb.org/manual/reference/operator/pop/ |
||
| 962 | 85 | * @return $this |
|
| 963 | 56 | */ |
|
| 964 | public function popLast() |
||
| 965 | 31 | { |
|
| 966 | $this->requiresCurrentField(); |
||
| 967 | 85 | $this->newObj['$pop'][$this->currentField] = -1; |
|
| 968 | return $this; |
||
| 969 | } |
||
| 970 | |||
| 971 | /** |
||
| 972 | * Add $position criteria to the expression for a $push operation. |
||
| 973 | * |
||
| 974 | * This is useful in conjunction with {@link Expr::each()} for a |
||
| 975 | * {@link Expr::push()} operation. |
||
| 976 | * |
||
| 977 | 1 | * @see http://docs.mongodb.org/manual/reference/operator/update/position/ |
|
| 978 | * @param integer $position |
||
| 979 | 1 | * @return $this |
|
| 980 | 1 | */ |
|
| 981 | 1 | public function position($position) |
|
| 982 | { |
||
| 983 | return $this->operator('$position', $position); |
||
| 984 | } |
||
| 985 | |||
| 986 | /** |
||
| 987 | * Remove all elements matching the given value or expression from the |
||
| 988 | * current array field. |
||
| 989 | * |
||
| 990 | * @see Builder::pull() |
||
| 991 | * @see http://docs.mongodb.org/manual/reference/operator/pull/ |
||
| 992 | * @param mixed|Expr $valueOrExpression |
||
| 993 | * @return $this |
||
| 994 | */ |
||
| 995 | View Code Duplication | public function pull($valueOrExpression) |
|
| 996 | { |
||
| 997 | if ($valueOrExpression instanceof Expr) { |
||
| 998 | $valueOrExpression = $valueOrExpression->getQuery(); |
||
| 999 | } |
||
| 1000 | |||
| 1001 | $this->requiresCurrentField(); |
||
| 1002 | $this->newObj['$pull'][$this->currentField] = $valueOrExpression; |
||
| 1003 | return $this; |
||
| 1004 | } |
||
| 1005 | |||
| 1006 | /** |
||
| 1007 | * Remove all elements matching any of the given values from the current |
||
| 1008 | 1 | * array field. |
|
| 1009 | * |
||
| 1010 | 1 | * @see Builder::pullAll() |
|
| 1011 | * @see http://docs.mongodb.org/manual/reference/operator/pullAll/ |
||
| 1012 | * @param array $values |
||
| 1013 | * @return $this |
||
| 1014 | */ |
||
| 1015 | public function pullAll(array $values) |
||
| 1016 | { |
||
| 1017 | $this->requiresCurrentField(); |
||
| 1018 | $this->newObj['$pullAll'][$this->currentField] = $values; |
||
| 1019 | return $this; |
||
| 1020 | } |
||
| 1021 | |||
| 1022 | 2 | /** |
|
| 1023 | * Append one or more values to the current array field. |
||
| 1024 | 2 | * |
|
| 1025 | 1 | * If the field does not exist, it will be set to an array containing the |
|
| 1026 | * value(s) in the argument. If the field is not an array, the query |
||
| 1027 | * will yield an error. |
||
| 1028 | 2 | * |
|
| 1029 | 2 | * Multiple values may be specified by providing an Expr object and using |
|
| 1030 | 2 | * {@link Expr::each()}. {@link Expr::slice()} and {@link Expr::sort()} may |
|
| 1031 | * also be used to limit and order array elements, respectively. |
||
| 1032 | * |
||
| 1033 | * @see Builder::push() |
||
| 1034 | * @see http://docs.mongodb.org/manual/reference/operator/push/ |
||
| 1035 | * @see http://docs.mongodb.org/manual/reference/operator/each/ |
||
| 1036 | * @see http://docs.mongodb.org/manual/reference/operator/slice/ |
||
| 1037 | * @see http://docs.mongodb.org/manual/reference/operator/sort/ |
||
| 1038 | * @param mixed|Expr $valueOrExpression |
||
| 1039 | * @return $this |
||
| 1040 | */ |
||
| 1041 | View Code Duplication | public function push($valueOrExpression) |
|
| 1042 | { |
||
| 1043 | if ($valueOrExpression instanceof Expr) { |
||
| 1044 | $valueOrExpression = array_merge( |
||
| 1045 | ['$each' => []], |
||
| 1046 | $valueOrExpression->getQuery() |
||
| 1047 | ); |
||
| 1048 | } |
||
| 1049 | |||
| 1050 | $this->requiresCurrentField(); |
||
| 1051 | $this->newObj['$push'][$this->currentField] = $valueOrExpression; |
||
| 1052 | return $this; |
||
| 1053 | } |
||
| 1054 | |||
| 1055 | /** |
||
| 1056 | * Specify $gte and $lt criteria for the current field. |
||
| 1057 | * |
||
| 1058 | * This method is shorthand for specifying $gte criteria on the lower bound |
||
| 1059 | * and $lt criteria on the upper bound. The upper bound is not inclusive. |
||
| 1060 | * |
||
| 1061 | * @see Builder::range() |
||
| 1062 | * @param mixed $start |
||
| 1063 | * @param mixed $end |
||
| 1064 | * @return $this |
||
| 1065 | */ |
||
| 1066 | public function range($start, $end) |
||
| 1067 | { |
||
| 1068 | 8 | return $this->operator('$gte', $start)->operator('$lt', $end); |
|
| 1069 | } |
||
| 1070 | 8 | ||
| 1071 | 3 | /** |
|
| 1072 | 3 | * Checks that the value of the current field is a reference to the supplied document. |
|
| 1073 | 3 | * |
|
| 1074 | * @param object $document |
||
| 1075 | * @return Expr |
||
| 1076 | */ |
||
| 1077 | 8 | View Code Duplication | public function references($document) |
| 1078 | 8 | { |
|
| 1079 | 8 | $this->requiresCurrentField(); |
|
| 1080 | $mapping = $this->getReferenceMapping(); |
||
| 1081 | $reference = $this->dm->createReference($document, $mapping); |
||
| 1082 | $storeAs = array_key_exists('storeAs', $mapping) ? $mapping['storeAs'] : null; |
||
| 1083 | |||
| 1084 | switch ($storeAs) { |
||
| 1085 | case ClassMetadataInfo::REFERENCE_STORE_AS_ID: |
||
| 1086 | $this->query[$mapping['name']] = $reference; |
||
| 1087 | return $this; |
||
| 1088 | break; |
||
| 1089 | |||
| 1090 | case ClassMetadataInfo::REFERENCE_STORE_AS_REF: |
||
| 1091 | $keys = ['id' => true]; |
||
| 1092 | break; |
||
| 1093 | |||
| 1094 | case ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF: |
||
| 1095 | case ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF_WITH_DB: |
||
| 1096 | $keys = ['$ref' => true, '$id' => true, '$db' => true]; |
||
| 1097 | |||
| 1098 | if ($storeAs === ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF) { |
||
| 1099 | unset($keys['$db']); |
||
| 1100 | } |
||
| 1101 | |||
| 1102 | if (isset($mapping['targetDocument'])) { |
||
| 1103 | unset($keys['$ref'], $keys['$db']); |
||
| 1104 | } |
||
| 1105 | break; |
||
| 1106 | |||
| 1107 | default: |
||
| 1108 | throw new \InvalidArgumentException("Reference type {$storeAs} is invalid."); |
||
| 1109 | } |
||
| 1110 | |||
| 1111 | foreach ($keys as $key => $value) { |
||
| 1112 | $this->query[$mapping['name'] . '.' . $key] = $reference[$key]; |
||
| 1113 | } |
||
| 1114 | |||
| 1115 | 2 | return $this; |
|
| 1116 | } |
||
| 1117 | 2 | ||
| 1118 | /** |
||
| 1119 | * Rename the current field. |
||
| 1120 | * |
||
| 1121 | * @see Builder::rename() |
||
| 1122 | * @see http://docs.mongodb.org/manual/reference/operator/rename/ |
||
| 1123 | * @param string $name |
||
| 1124 | * @return $this |
||
| 1125 | */ |
||
| 1126 | 13 | public function rename($name) |
|
| 1127 | { |
||
| 1128 | 13 | $this->requiresCurrentField(); |
|
| 1129 | 13 | $this->newObj['$rename'][$this->currentField] = $name; |
|
| 1130 | 11 | return $this; |
|
| 1131 | 11 | } |
|
| 1132 | |||
| 1133 | /** |
||
| 1134 | 11 | * Set the current field to a value. |
|
| 1135 | 4 | * |
|
| 1136 | 4 | * This is only relevant for insert, update, or findAndUpdate queries. For |
|
| 1137 | * update and findAndUpdate queries, the $atomic parameter will determine |
||
| 1138 | * whether or not a $set operator is used. |
||
| 1139 | 8 | * |
|
| 1140 | * @see Builder::set() |
||
| 1141 | * @see http://docs.mongodb.org/manual/reference/operator/set/ |
||
| 1142 | * @param mixed $value |
||
| 1143 | 8 | * @param boolean $atomic |
|
| 1144 | 3 | * @return $this |
|
| 1145 | 8 | */ |
|
| 1146 | public function set($value, $atomic = true) |
||
| 1147 | 8 | { |
|
| 1148 | 5 | $this->requiresCurrentField(); |
|
| 1149 | |||
| 1150 | if ($atomic) { |
||
| 1151 | 8 | $this->newObj['$set'][$this->currentField] = $value; |
|
| 1152 | 4 | return $this; |
|
| 1153 | } |
||
| 1154 | 8 | ||
| 1155 | if (strpos($this->currentField, '.') === false) { |
||
| 1156 | $this->newObj[$this->currentField] = $value; |
||
| 1157 | return $this; |
||
| 1158 | } |
||
| 1159 | |||
| 1160 | 8 | $keys = explode('.', $this->currentField); |
|
| 1161 | 8 | $current = &$this->newObj; |
|
| 1162 | foreach ($keys as $key) { |
||
| 1163 | $current = &$current[$key]; |
||
| 1164 | } |
||
| 1165 | $current = $value; |
||
| 1166 | |||
| 1167 | return $this; |
||
| 1168 | } |
||
| 1169 | 8 | ||
| 1170 | /** |
||
| 1171 | * Sets ClassMetadata for document being queried. |
||
| 1172 | * |
||
| 1173 | * @param ClassMetadata $class |
||
| 1174 | */ |
||
| 1175 | public function setClassMetadata(ClassMetadata $class) |
||
| 1176 | { |
||
| 1177 | $this->class = $class; |
||
| 1178 | } |
||
| 1179 | |||
| 1180 | /** |
||
| 1181 | * Set the "new object". |
||
| 1182 | * |
||
| 1183 | * @see Builder::setNewObj() |
||
| 1184 | * @param array $newObj |
||
| 1185 | * @return $this |
||
| 1186 | */ |
||
| 1187 | public function setNewObj(array $newObj) |
||
| 1188 | { |
||
| 1189 | $this->newObj = $newObj; |
||
| 1190 | return $this; |
||
| 1191 | } |
||
| 1192 | |||
| 1193 | /** |
||
| 1194 | * Set the current field to the value if the document is inserted in an |
||
| 1195 | * upsert operation. |
||
| 1196 | * |
||
| 1197 | * If an update operation with upsert: true results in an insert of a |
||
| 1198 | * document, then $setOnInsert assigns the specified values to the fields in |
||
| 1199 | * the document. If the update operation does not result in an insert, |
||
| 1200 | 19 | * $setOnInsert does nothing. |
|
| 1201 | * |
||
| 1202 | 19 | * @see Builder::setOnInsert() |
|
| 1203 | * @see https://docs.mongodb.org/manual/reference/operator/update/setOnInsert/ |
||
| 1204 | 19 | * @param mixed $value |
|
| 1205 | 16 | * @return $this |
|
| 1206 | 16 | */ |
|
| 1207 | public function setOnInsert($value) |
||
| 1208 | { |
||
| 1209 | 3 | $this->requiresCurrentField(); |
|
| 1210 | 2 | $this->newObj['$setOnInsert'][$this->currentField] = $value; |
|
| 1211 | 2 | ||
| 1212 | return $this; |
||
| 1213 | } |
||
| 1214 | 2 | ||
| 1215 | 2 | /** |
|
| 1216 | 2 | * Set the query criteria. |
|
| 1217 | 2 | * |
|
| 1218 | * @see Builder::setQueryArray() |
||
| 1219 | 2 | * @param array $query |
|
| 1220 | * @return $this |
||
| 1221 | 2 | */ |
|
| 1222 | public function setQuery(array $query) |
||
| 1223 | { |
||
| 1224 | $this->query = $query; |
||
| 1225 | return $this; |
||
| 1226 | } |
||
| 1227 | |||
| 1228 | /** |
||
| 1229 | 402 | * Specify $size criteria for the current field. |
|
| 1230 | * |
||
| 1231 | 402 | * @see Builder::size() |
|
| 1232 | 402 | * @see http://docs.mongodb.org/manual/reference/operator/size/ |
|
| 1233 | * @param integer $size |
||
| 1234 | * @return $this |
||
| 1235 | */ |
||
| 1236 | public function size($size) |
||
| 1237 | { |
||
| 1238 | return $this->operator('$size', (integer) $size); |
||
| 1239 | } |
||
| 1240 | |||
| 1241 | /** |
||
| 1242 | * Add $slice criteria to the expression for a $push operation. |
||
| 1243 | * |
||
| 1244 | * This is useful in conjunction with {@link Expr::each()} for a |
||
| 1245 | * {@link Expr::push()} operation. {@link Builder::selectSlice()} should be |
||
| 1246 | * used for specifying $slice for a query projection. |
||
| 1247 | * |
||
| 1248 | * @see http://docs.mongodb.org/manual/reference/operator/slice/ |
||
| 1249 | * @param integer $slice |
||
| 1250 | * @return $this |
||
| 1251 | */ |
||
| 1252 | public function slice($slice) |
||
| 1253 | { |
||
| 1254 | return $this->operator('$slice', $slice); |
||
| 1255 | } |
||
| 1256 | |||
| 1257 | /** |
||
| 1258 | * Add $sort criteria to the expression for a $push operation. |
||
| 1259 | * |
||
| 1260 | * If sorting by multiple fields, the first argument should be an array of |
||
| 1261 | 1 | * field name (key) and order (value) pairs. |
|
| 1262 | * |
||
| 1263 | 1 | * This is useful in conjunction with {@link Expr::each()} for a |
|
| 1264 | 1 | * {@link Expr::push()} operation. {@link Builder::sort()} should be used to |
|
| 1265 | * sort the results of a query. |
||
| 1266 | 1 | * |
|
| 1267 | * @see http://docs.mongodb.org/manual/reference/operator/sort/ |
||
| 1268 | * @param array|string $fieldName Field name or array of field/order pairs |
||
| 1269 | * @param int|string $order Field order (if one field is specified) |
||
| 1270 | * @return $this |
||
| 1271 | */ |
||
| 1272 | public function sort($fieldName, $order = null) |
||
| 1273 | { |
||
| 1274 | $fields = is_array($fieldName) ? $fieldName : [$fieldName => $order]; |
||
| 1275 | |||
| 1276 | 18 | return $this->operator('$sort', array_map([$this, 'normalizeSortOrder'], $fields)); |
|
| 1277 | } |
||
| 1278 | 18 | ||
| 1279 | 18 | /** |
|
| 1280 | * Specify $text criteria for the current query. |
||
| 1281 | * |
||
| 1282 | * The $language option may be set with {@link Expr::language()}. |
||
| 1283 | * |
||
| 1284 | * @see Builder::text() |
||
| 1285 | * @see http://docs.mongodb.org/master/reference/operator/query/text/ |
||
| 1286 | * @param string $search |
||
| 1287 | * @return $this |
||
| 1288 | */ |
||
| 1289 | public function text($search) |
||
| 1290 | { |
||
| 1291 | $this->query['$text'] = ['$search' => (string) $search]; |
||
| 1292 | return $this; |
||
| 1293 | } |
||
| 1294 | |||
| 1295 | /** |
||
| 1296 | * Specify $type criteria for the current field. |
||
| 1297 | * |
||
| 1298 | * @todo Remove support for string $type argument in 2.0 |
||
| 1299 | * @see Builder::type() |
||
| 1300 | * @see http://docs.mongodb.org/manual/reference/operator/type/ |
||
| 1301 | * @param integer $type |
||
| 1302 | * @return $this |
||
| 1303 | */ |
||
| 1304 | public function type($type) |
||
| 1334 | |||
| 1335 | /** |
||
| 1336 | * Unset the current field. |
||
| 1337 | * |
||
| 1338 | * The field will be removed from the document (not set to null). |
||
| 1339 | * |
||
| 1340 | * @see Builder::unsetField() |
||
| 1341 | * @see http://docs.mongodb.org/manual/reference/operator/unset/ |
||
| 1342 | * @return $this |
||
| 1343 | 6 | */ |
|
| 1344 | public function unsetField() |
||
| 1345 | 6 | { |
|
| 1346 | 6 | $this->requiresCurrentField(); |
|
| 1347 | $this->newObj['$unset'][$this->currentField] = 1; |
||
| 1348 | return $this; |
||
| 1349 | } |
||
| 1350 | |||
| 1351 | /** |
||
| 1352 | * Specify a JavaScript expression to use for matching documents. |
||
| 1353 | * |
||
| 1354 | * @see Builder::where() |
||
| 1355 | * @see http://docs.mongodb.org/manual/reference/operator/where/ |
||
| 1356 | * @param string|\MongoCode $javascript |
||
| 1357 | * @return $this |
||
| 1358 | 1 | */ |
|
| 1359 | public function where($javascript) |
||
| 1360 | 1 | { |
|
| 1361 | $this->query['$where'] = $javascript; |
||
| 1362 | 1 | return $this; |
|
| 1363 | } |
||
| 1364 | |||
| 1365 | /** |
||
| 1366 | * Gets reference mapping for current field from current class or its descendants. |
||
| 1367 | * |
||
| 1368 | * @return array |
||
| 1369 | * @throws MappingException |
||
| 1370 | */ |
||
| 1371 | private function getReferenceMapping() |
||
| 1372 | { |
||
| 1373 | $mapping = null; |
||
| 1374 | try { |
||
| 1375 | $mapping = $this->class->getFieldMapping($this->currentField); |
||
| 1376 | } catch (MappingException $e) { |
||
| 1377 | if (empty($this->class->discriminatorMap)) { |
||
| 1378 | throw $e; |
||
| 1379 | } |
||
| 1380 | $foundIn = null; |
||
| 1381 | foreach ($this->class->discriminatorMap as $child) { |
||
| 1382 | $childClass = $this->dm->getClassMetadata($child); |
||
| 1383 | 1 | if ($childClass->hasAssociation($this->currentField)) { |
|
| 1384 | if ($mapping !== null && $mapping !== $childClass->getFieldMapping($this->currentField)) { |
||
| 1385 | throw MappingException::referenceFieldConflict($this->currentField, $foundIn->name, $childClass->name); |
||
| 1386 | 1 | } |
|
| 1387 | $mapping = $childClass->getFieldMapping($this->currentField); |
||
| 1388 | $foundIn = $childClass; |
||
| 1389 | } |
||
| 1390 | } |
||
| 1391 | if ($mapping === null) { |
||
| 1392 | throw MappingException::mappingNotFoundInClassNorDescendants($this->class->name, $this->currentField); |
||
| 1393 | } |
||
| 1394 | } |
||
| 1395 | return $mapping; |
||
| 1396 | } |
||
| 1397 | |||
| 1398 | 3 | /** |
|
| 1399 | * @param int|string $order |
||
| 1400 | 3 | * |
|
| 1401 | 3 | * @return int |
|
| 1402 | 3 | */ |
|
| 1403 | private function normalizeSortOrder($order): int |
||
| 1411 | |||
| 1412 | /** |
||
| 1413 | 3 | * Ensure that a current field has been set. |
|
| 1414 | * |
||
| 1415 | 3 | * @throws \LogicException if a current field has not been set |
|
| 1416 | 3 | */ |
|
| 1417 | private function requiresCurrentField() |
||
| 1423 | |||
| 1424 | /** |
||
| 1425 | * Wraps equality criteria with an operator. |
||
| 1426 | * |
||
| 1427 | * If equality criteria was previously specified for a field, it cannot be |
||
| 1428 | * merged with other operators without first being wrapped in an operator of |
||
| 1429 | * its own. Ideally, we would wrap it with $eq, but that is only available |
||
| 1430 | * in MongoDB 2.8. Using a single-element $in is backwards compatible. |
||
| 1431 | 1 | * |
|
| 1432 | * @see Expr::operator() |
||
| 1433 | 1 | */ |
|
| 1434 | private function wrapEqualityCriteria() |
||
| 1460 | } |
||
| 1461 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.