| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Umbrellio\LTree\Traits; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Illuminate\Database\Eloquent\Concerns\HasRelationships; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Illuminate\Database\Eloquent\Model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Umbrellio\LTree\Exceptions\InvalidTraitInjectionClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Umbrellio\LTree\Interfaces\LTreeModelInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Umbrellio\LTree\Relations\AbstractBelongsToTree; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Umbrellio\LTree\Relations\BelongsToAncestorsTree; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Umbrellio\LTree\Relations\BelongsToDescendantsTree; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @mixin HasRelationships | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @mixin LTreeModelTrait | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @mixin Model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | trait HasTreeRelationships | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 3 |  |     final protected function belongsToAncestorsTree( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         string $related, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         string $throwRelation, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         ?string $foreignKey = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $ownerKey = null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 3 |  |         return $this->belongsToTree(BelongsToAncestorsTree::class, $related, $throwRelation, $foreignKey, $ownerKey); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 1 |  |     final protected function belongsToDescendantsTree( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         string $related, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         string $throwRelation, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         ?string $foreignKey = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $ownerKey = null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 1 |  |         return $this->belongsToTree( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 1 |  |             BelongsToDescendantsTree::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 1 |  |             $related, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 1 |  |             $throwRelation, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 1 |  |             $foreignKey, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 1 |  |             $ownerKey | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 1 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 4 |  |     final protected function belongsToTree( | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         string $relationClass, | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         string $related, | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         string $throwRelation, | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         ?string $foreignKey = null, | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         $ownerKey = null | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     ): AbstractBelongsToTree { | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 4 |  |         $instance = $this->newRelatedInstance($related); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 4 |  |         if (!$instance instanceof LTreeModelInterface) { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 1 |  |             throw new InvalidTraitInjectionClass(sprintf( | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 1 |  |                 'A class using this trait must implement an interface %s', | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 1 |  |                 LTreeModelInterface::class | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 1 |  |             )); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 | 3 |  |         if ($foreignKey === null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 64 | 3 |  |             $foreignKey = $this | 
            
                                                                        
                            
            
                                    
            
            
                | 65 | 3 |  |                 ->{$throwRelation}() | 
            
                                                                        
                            
            
                                    
            
            
                | 66 | 3 |  |                 ->getForeignKeyName(); | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 | 3 |  |         $ownerKey = $ownerKey ?: $instance->getKeyName(); | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 | 3 |  |         return new $relationClass($instance->newQuery(), $this, $throwRelation, $foreignKey, $ownerKey); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 73 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |  |