| 1: | <?php |
| 2: | |
| 3: | namespace Baril\Bonsai\Concerns; |
| 4: | |
| 5: | use Illuminate\Database\Eloquent\Builder; |
| 6: | use Illuminate\Database\Eloquent\Model; |
| 7: | |
| 8: | trait HasDescendants |
| 9: | { |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | public function children() |
| 22: | { |
| 23: | return $this->hasMany(static::class, $this->getParentForeignKeyName()); |
| 24: | } |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | public function descendants() |
| 32: | { |
| 33: | return |
| 34: | $this->belongsToManyThroughClosures( |
| 35: | static::class, |
| 36: | $this->getClosureTable(), |
| 37: | 'ancestor_id', |
| 38: | 'descendant_id', |
| 39: | 'descendants' |
| 40: | ) |
| 41: | ->withoutSelf() |
| 42: | ->closes('children'); |
| 43: | } |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | public function descendingClosures() |
| 51: | { |
| 52: | return $this->hasManyClosuresWith( |
| 53: | static::class, |
| 54: | $this->getClosureTable(), |
| 55: | 'ancestor_id', |
| 56: | 'descendant_id' |
| 57: | ); |
| 58: | } |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | public function scopeWithDescendants(Builder $query, $depth = null, $constraints = null) |
| 73: | { |
| 74: | $this->scopeWithManyThroughClosures($query, 'descendants', $depth, $constraints); |
| 75: | } |
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: | public function scopeWithHeight(Builder $query, $as = 'height') |
| 83: | { |
| 84: | $query->withMax("descendants as $as", $this->getClosureTable() . '.depth'); |
| 85: | } |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | public function scopeWhereIsLeaf(Builder $query, $bool = true) |
| 95: | { |
| 96: | $this->scopeHasChildren($query, !$bool); |
| 97: | } |
| 98: | |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | public function scopeOnlyLeaves(Builder $query) |
| 104: | { |
| 105: | $this->scopeHasChildren($query, false); |
| 106: | } |
| 107: | |
| 108: | |
| 109: | |
| 110: | |
| 111: | |
| 112: | public function scopeWithoutLeaves(Builder $query) |
| 113: | { |
| 114: | $this->scopeHasChildren($query); |
| 115: | } |
| 116: | |
| 117: | |
| 118: | |
| 119: | |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | public function scopeWhereHasChildren(Builder $query, $bool = true) |
| 125: | { |
| 126: | $this->scopeHasChildren($query, $bool); |
| 127: | } |
| 128: | |
| 129: | |
| 130: | |
| 131: | |
| 132: | |
| 133: | |
| 134: | public function scopeHasChildren(Builder $query, $bool = true) |
| 135: | { |
| 136: | if ($bool) { |
| 137: | $query->has('children'); |
| 138: | } else { |
| 139: | $query->doesntHave('children'); |
| 140: | } |
| 141: | } |
| 142: | |
| 143: | |
| 144: | |
| 145: | |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | |
| 151: | |
| 152: | public function scopeWhereIsAncestorOf(Builder $query, $descendant, $maxDepth = null, $withSelf = false) |
| 153: | { |
| 154: | $this->scopeAncestorsOf($query, $descendant, $maxDepth, $withSelf); |
| 155: | } |
| 156: | |
| 157: | |
| 158: | |
| 159: | |
| 160: | |
| 161: | |
| 162: | |
| 163: | |
| 164: | public function scopeAncestorsOf(Builder $query, $descendant, $maxDepth = null, $withSelf = false) |
| 165: | { |
| 166: | $this->scopeWhereHasClosuresWith( |
| 167: | $query, |
| 168: | $descendant, |
| 169: | 'descendingClosures', |
| 170: | 'whereDescendant', |
| 171: | $maxDepth, |
| 172: | $withSelf |
| 173: | ); |
| 174: | } |
| 175: | |
| 176: | |
| 177: | |
| 178: | |
| 179: | |
| 180: | |
| 181: | |
| 182: | |
| 183: | public function isLeaf() |
| 184: | { |
| 185: | return !$this->hasChildren(); |
| 186: | } |
| 187: | |
| 188: | |
| 189: | |
| 190: | |
| 191: | public function hasChildren() |
| 192: | { |
| 193: | return $this->children()->exists(); |
| 194: | } |
| 195: | |
| 196: | |
| 197: | |
| 198: | |
| 199: | |
| 200: | public function isParentOf(Model $node) |
| 201: | { |
| 202: | return $node->isChildOf($this); |
| 203: | } |
| 204: | |
| 205: | |
| 206: | |
| 207: | |
| 208: | |
| 209: | public function isAncestorOf($node) |
| 210: | { |
| 211: | return $this->descendingClosures() |
| 212: | ->whereDescendant($node) |
| 213: | ->exists(); |
| 214: | } |
| 215: | |
| 216: | |
| 217: | |
| 218: | |
| 219: | |
| 220: | |
| 221: | |
| 222: | |
| 223: | public function getSubtreeDepth() |
| 224: | { |
| 225: | return $this->getHeight(); |
| 226: | } |
| 227: | |
| 228: | |
| 229: | |
| 230: | |
| 231: | |
| 232: | |
| 233: | public function getHeight() |
| 234: | { |
| 235: | return (int) $this->descendants()->max('depth'); |
| 236: | } |
| 237: | } |
| 238: | |