| 1: | <?php |
| 2: | |
| 3: | namespace Baril\Bonsai\Concerns; |
| 4: | |
| 5: | use Baril\Bonsai\Relations\HasManySiblings; |
| 6: | use Baril\Bonsai\TreeException; |
| 7: | use Illuminate\Database\Eloquent\Builder; |
| 8: | use Illuminate\Database\Eloquent\Model; |
| 9: | |
| 10: | trait HasAncestors |
| 11: | { |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | public function getParentForeignKeyName() |
| 18: | { |
| 19: | return property_exists($this, 'parentForeignKey') |
| 20: | ? $this->parentForeignKey |
| 21: | : 'parent_id'; |
| 22: | } |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | public function getParentKey() |
| 30: | { |
| 31: | return $this->parent()->getParentKey(); |
| 32: | } |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | public function parent() |
| 44: | { |
| 45: | return $this->belongsTo( |
| 46: | static::class, |
| 47: | $this->getParentForeignKeyName() |
| 48: | ); |
| 49: | } |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | public function ancestors() |
| 57: | { |
| 58: | return |
| 59: | $this->belongsToManyThroughClosures( |
| 60: | static::class, |
| 61: | $this->getClosureTable() |
| 62: | ) |
| 63: | ->withoutSelf() |
| 64: | ->closes('parent'); |
| 65: | } |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | public function ascendingClosures() |
| 73: | { |
| 74: | return $this->hasManyClosuresWith(static::class, $this->getClosureTable()); |
| 75: | } |
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: | public function siblings() |
| 83: | { |
| 84: | return $this->hasManySiblings($this->getParentForeignKeyName()) |
| 85: | ->withoutSelf(); |
| 86: | } |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | public function hasManySiblings($parentForeignKey = 'parent_id') |
| 98: | { |
| 99: | $instance = $this->newRelatedInstance(static::class); |
| 100: | |
| 101: | return new HasManySiblings( |
| 102: | $instance->newQuery(), |
| 103: | $this, |
| 104: | $instance->qualifyColumn($parentForeignKey), |
| 105: | $parentForeignKey |
| 106: | ); |
| 107: | } |
| 108: | |
| 109: | |
| 110: | |
| 111: | |
| 112: | |
| 113: | |
| 114: | |
| 115: | |
| 116: | |
| 117: | |
| 118: | |
| 119: | |
| 120: | |
| 121: | |
| 122: | public function scopeWithAncestors(Builder $query, $depth = null, $constraints = null) |
| 123: | { |
| 124: | $this->scopeWithManyThroughClosures($query, 'ancestors', $depth, $constraints); |
| 125: | } |
| 126: | |
| 127: | |
| 128: | |
| 129: | |
| 130: | |
| 131: | |
| 132: | public function scopeWithDepth(Builder $query, $as = 'depth') |
| 133: | { |
| 134: | $query->withCount("ancestors as $as"); |
| 135: | } |
| 136: | |
| 137: | |
| 138: | |
| 139: | |
| 140: | |
| 141: | |
| 142: | |
| 143: | |
| 144: | |
| 145: | |
| 146: | public function scopeWhereIsDescendantOf(Builder $query, $ancestor, $maxDepth = null, $withSelf = false) |
| 147: | { |
| 148: | $this->scopeDescendantsOf($query, $ancestor, $maxDepth, $withSelf); |
| 149: | } |
| 150: | |
| 151: | |
| 152: | |
| 153: | |
| 154: | |
| 155: | |
| 156: | |
| 157: | |
| 158: | public function scopeDescendantsOf(Builder $query, $ancestor, $maxDepth = null, $withSelf = false) |
| 159: | { |
| 160: | $this->scopeWhereHasClosuresWith( |
| 161: | $query, |
| 162: | $ancestor, |
| 163: | 'ascendingClosures', |
| 164: | 'whereAncestor', |
| 165: | $maxDepth, |
| 166: | $withSelf |
| 167: | ); |
| 168: | } |
| 169: | |
| 170: | |
| 171: | |
| 172: | |
| 173: | |
| 174: | |
| 175: | |
| 176: | |
| 177: | |
| 178: | public function isChildOf($node) |
| 179: | { |
| 180: | $nodeId = $node instanceof Model ? $node->getKey() : $node; |
| 181: | |
| 182: | return $nodeId == $this->getParentKey(); |
| 183: | } |
| 184: | |
| 185: | |
| 186: | |
| 187: | |
| 188: | |
| 189: | public function isDescendantOf($node) |
| 190: | { |
| 191: | return $this->ascendingClosures() |
| 192: | ->whereAncestor($node) |
| 193: | ->exists(); |
| 194: | } |
| 195: | |
| 196: | |
| 197: | |
| 198: | |
| 199: | |
| 200: | public function isSiblingOf(Model $node) |
| 201: | { |
| 202: | return $node->getParentKey() == $this->getParentKey(); |
| 203: | } |
| 204: | |
| 205: | |
| 206: | |
| 207: | |
| 208: | |
| 209: | |
| 210: | |
| 211: | |
| 212: | |
| 213: | public function findCommonAncestorWith($node) |
| 214: | { |
| 215: | return $this->newCommonAncestorQuery($node) |
| 216: | ->orderByDepth() |
| 217: | ->first(); |
| 218: | } |
| 219: | |
| 220: | |
| 221: | |
| 222: | |
| 223: | |
| 224: | public function hasCommonAncestorWith($node) |
| 225: | { |
| 226: | return $this->newCommonAncestorQuery($node) |
| 227: | ->exists(); |
| 228: | } |
| 229: | |
| 230: | |
| 231: | |
| 232: | |
| 233: | |
| 234: | protected function newCommonAncestorQuery($node) |
| 235: | { |
| 236: | return $this->ancestors() |
| 237: | ->withSelf() |
| 238: | ->ancestorsOf($node, null, true); |
| 239: | } |
| 240: | |
| 241: | |
| 242: | |
| 243: | |
| 244: | |
| 245: | |
| 246: | |
| 247: | |
| 248: | |
| 249: | |
| 250: | public function getDistanceTo($node) |
| 251: | { |
| 252: | $commonAncestor = $this->findCommonAncestorWith($node); |
| 253: | if (!$commonAncestor) { |
| 254: | throw new TreeException('The items have no common ancestor!'); |
| 255: | } |
| 256: | |
| 257: | return $commonAncestor |
| 258: | ->descendingClosures() |
| 259: | ->whereDescendant([$this, $node]) |
| 260: | ->sum('depth'); |
| 261: | } |
| 262: | |
| 263: | |
| 264: | |
| 265: | |
| 266: | |
| 267: | |
| 268: | public function getDepth() |
| 269: | { |
| 270: | return $this->ancestors()->count(); |
| 271: | } |
| 272: | } |
| 273: | |