fluent-community
/
vendor
/
wpfluent
/
framework
/
src
/
WPFluent
/
Database
/
Orm
/
PendingHasThroughRelationship.php
PendingHasThroughRelationship.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 1.1.0, at vendor/wpfluent/framework/src/WPFluent/Database/Orm/PendingHasThroughRelationship.php
| 1 | <?php |
| 2 | |
| 3 | namespace FluentCommunity\Framework\Database\Orm; |
| 4 | |
| 5 | use BadMethodCallException; |
| 6 | use FluentCommunity\Framework\Support\Str; |
| 7 | use FluentCommunity\Framework\Database\Orm\Relations\HasMany; |
| 8 | use FluentCommunity\Framework\Database\Orm\Relations\MorphOneOrMany; |
| 9 | |
| 10 | /** |
| 11 | * @template TIntermediateModel of \FluentCommunity\Framework\Database\Orm\Model |
| 12 | * @template TDeclaringModel of \FluentCommunity\Framework\Database\Orm\Model |
| 13 | */ |
| 14 | class PendingHasThroughRelationship |
| 15 | { |
| 16 | /** |
| 17 | * The root model that the relationship exists on. |
| 18 | * |
| 19 | * @var TDeclaringModel |
| 20 | */ |
| 21 | protected $rootModel; |
| 22 | |
| 23 | /** |
| 24 | * The local relationship. |
| 25 | * |
| 26 | * @var \FluentCommunity\Framework\Database\Orm\Relations\HasMany<TIntermediateModel, TDeclaringModel>|\FluentCommunity\Framework\Database\Orm\Relations\HasOne<TIntermediateModel, TDeclaringModel> |
| 27 | */ |
| 28 | protected $localRelationship; |
| 29 | |
| 30 | /** |
| 31 | * Create a pending has-many-through or has-one-through relationship. |
| 32 | * |
| 33 | * @param TDeclaringModel $rootModel |
| 34 | * @param \FluentCommunity\Framework\Database\Orm\Relations\HasMany<TIntermediateModel, TDeclaringModel>|\FluentCommunity\Framework\Database\Orm\Relations\HasOne<TIntermediateModel, TDeclaringModel> $localRelationship |
| 35 | */ |
| 36 | public function __construct($rootModel, $localRelationship) |
| 37 | { |
| 38 | $this->rootModel = $rootModel; |
| 39 | |
| 40 | $this->localRelationship = $localRelationship; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Define the distant relationship that this model has. |
| 45 | * |
| 46 | * @template TRelatedModel of \FluentCommunity\Framework\Database\Orm\Model |
| 47 | * |
| 48 | * @param string|(callable(TIntermediateModel): (\FluentCommunity\Framework\Database\Orm\Relations\HasOne<TRelatedModel, TIntermediateModel>|\FluentCommunity\Framework\Database\Orm\Relations\HasMany<TRelatedModel, TIntermediateModel>|\FluentCommunity\Framework\Database\Orm\Relations\MorphOneOrMany<TRelatedModel, TIntermediateModel>)) $callback |
| 49 | * @return ( |
| 50 | * $callback is string |
| 51 | * ? \FluentCommunity\Framework\Database\Orm\Relations\HasManyThrough<\FluentCommunity\Framework\Database\Orm\Model, TIntermediateModel, TDeclaringModel>|\FluentCommunity\Framework\Database\Orm\Relations\HasOneThrough<\FluentCommunity\Framework\Database\Orm\Model, TIntermediateModel, TDeclaringModel> |
| 52 | * : ( |
| 53 | * $callback is callable(TIntermediateModel): \FluentCommunity\Framework\Database\Orm\Relations\HasOne<TRelatedModel, TIntermediateModel> |
| 54 | * ? \FluentCommunity\Framework\Database\Orm\Relations\HasOneThrough<TRelatedModel, TIntermediateModel, TDeclaringModel> |
| 55 | * : \FluentCommunity\Framework\Database\Orm\Relations\HasManyThrough<TRelatedModel, TIntermediateModel, TDeclaringModel> |
| 56 | * ) |
| 57 | * ) |
| 58 | */ |
| 59 | public function has($callback) |
| 60 | { |
| 61 | if (is_string($callback)) { |
| 62 | $callback = fn () => $this->localRelationship->getRelated()->{$callback}(); |
| 63 | } |
| 64 | |
| 65 | $distantRelation = $callback($this->localRelationship->getRelated()); |
| 66 | |
| 67 | if ($distantRelation instanceof HasMany) { |
| 68 | $returnedRelation = $this->rootModel->hasManyThrough( |
| 69 | get_class($distantRelation->getRelated()), |
| 70 | get_class($this->localRelationship->getRelated()), |
| 71 | $this->localRelationship->getForeignKeyName(), |
| 72 | $distantRelation->getForeignKeyName(), |
| 73 | $this->localRelationship->getLocalKeyName(), |
| 74 | $distantRelation->getLocalKeyName(), |
| 75 | ); |
| 76 | } else { |
| 77 | $returnedRelation = $this->rootModel->hasOneThrough( |
| 78 | get_class($distantRelation->getRelated()), |
| 79 | get_class($this->localRelationship->getRelated()), |
| 80 | $this->localRelationship->getForeignKeyName(), |
| 81 | $distantRelation->getForeignKeyName(), |
| 82 | $this->localRelationship->getLocalKeyName(), |
| 83 | $distantRelation->getLocalKeyName(), |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | if ($this->localRelationship instanceof MorphOneOrMany) { |
| 88 | $returnedRelation->where($this->localRelationship->getQualifiedMorphType(), $this->localRelationship->getMorphClass()); |
| 89 | } |
| 90 | |
| 91 | return $returnedRelation; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Handle dynamic method calls into the model. |
| 96 | * |
| 97 | * @param string $method |
| 98 | * @param array $parameters |
| 99 | * @return mixed |
| 100 | */ |
| 101 | public function __call($method, $parameters) |
| 102 | { |
| 103 | if (Str::startsWith($method, 'has')) { |
| 104 | return $this->has(Str::of($method)->after('has')->lcfirst()->toString()); |
| 105 | } |
| 106 | |
| 107 | throw new BadMethodCallException(sprintf( |
| 108 | 'Call to undefined method %s::%s()', static::class, $method |
| 109 | )); |
| 110 | } |
| 111 | } |
| 112 |