|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Fureev\Trees\Healthy; |
| 6 | + |
| 7 | +use Fureev\Trees\QueryBuilderV2; |
| 8 | +use Illuminate\Database\Query\Builder; |
| 9 | + |
| 10 | +final readonly class WrongParentCheck extends AbstractCheck |
| 11 | +{ |
| 12 | + protected function query(): Builder |
| 13 | + { |
| 14 | + $table = $this->model->wrappedTable(); |
| 15 | + $keyName = $this->model->wrappedKey(); |
| 16 | + |
| 17 | + $grammar = $this->model->getQuery()->getGrammar(); |
| 18 | + |
| 19 | + $parentIdName = $grammar->wrap((string)$this->model->parentAttribute()); |
| 20 | + |
| 21 | + $parentAlias = 'p'; |
| 22 | + $childAlias = 'c'; |
| 23 | + $intermAlias = 'i'; |
| 24 | + |
| 25 | + $waParent = $grammar->wrapTable($parentAlias); |
| 26 | + $waChild = $grammar->wrapTable($childAlias); |
| 27 | + $waInterm = $grammar->wrapTable($intermAlias); |
| 28 | + |
| 29 | + $isMulti = $this->model->isMulti(); |
| 30 | + |
| 31 | + /** @var QueryBuilderV2 $query */ |
| 32 | + $query = $this->model->newNestedSetQuery($childAlias); |
| 33 | + |
| 34 | + $query |
| 35 | + ->toBase() |
| 36 | + ->from($this->model->getQuery()->raw("$table as $waChild, $table as $waParent, $table as $waInterm")) |
| 37 | + ->when( |
| 38 | + $isMulti, |
| 39 | + function (Builder $q) use ($waChild, $waParent, $waInterm) { |
| 40 | + $tid = (string)$this->model->treeAttribute(); |
| 41 | + $q |
| 42 | + ->whereRaw("$waChild.$tid = $waParent.$tid") |
| 43 | + ->whereRaw("$waInterm.$tid = $waParent.$tid"); |
| 44 | + } |
| 45 | + ) |
| 46 | + ->whereRaw("$waChild.$parentIdName=$waParent.$keyName") |
| 47 | + ->whereRaw("$waInterm.$keyName <> $waParent.$keyName") |
| 48 | + ->whereRaw("$waInterm.$keyName <> $waChild.$keyName") |
| 49 | + ->whereNested( |
| 50 | + function (Builder $inner) use ($waInterm, $waChild, $waParent, $query) { |
| 51 | + [ |
| 52 | + $lft, |
| 53 | + $rgt, |
| 54 | + ] = $query->wrappedColumns(); |
| 55 | + |
| 56 | + $inner |
| 57 | + ->whereRaw("$waChild.$lft not between $waParent.$lft and $waParent.$rgt") |
| 58 | + ->orWhereRaw("$waChild.$lft between $waInterm.$lft and $waInterm.$rgt") |
| 59 | + ->whereRaw("$waInterm.$lft between $waParent.$lft and $waParent.$rgt"); |
| 60 | + } |
| 61 | + ); |
| 62 | + |
| 63 | + return $this->model->applyNestedSetScope( |
| 64 | + $this->model->applyNestedSetScope($query, $parentAlias), |
| 65 | + $intermAlias |
| 66 | + )->getQuery(); |
| 67 | + } |
| 68 | +} |
0 commit comments