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 2.7.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|\FluentCommunity\Framework\Database\Orm\Relations\HasOne |
| 27 | */ |
| 28 | protected $localRelationship; |
| 29 | |
| 30 | /** |
| 31 | * Create a pending has-many-through or has-one-through relationship. |
| 32 | * |
| 33 | * @param mixed $rootModel |
| 34 | * @param \FluentCommunity\Framework\Database\Orm\Relations\HasMany|\FluentCommunity\Framework\Database\Orm\Relations\HasOne $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 | * @param string|callable $callback Either the distant relationship name or a callback returning the local relation. |
| 47 | * @return \FluentCommunity\Framework\Database\Orm\Relations\HasManyThrough|\FluentCommunity\Framework\Database\Orm\Relations\HasOneThrough |
| 48 | * The distant relationship instance. |
| 49 | */ |
| 50 | public function has($callback) |
| 51 | { |
| 52 | if (is_string($callback)) { |
| 53 | $callback = fn () => $this->localRelationship->getRelated()->{$callback}(); |
| 54 | } |
| 55 | |
| 56 | $distantRelation = $callback($this->localRelationship->getRelated()); |
| 57 | |
| 58 | if ($distantRelation instanceof HasMany) { |
| 59 | $returnedRelation = $this->rootModel->hasManyThrough( |
| 60 | get_class($distantRelation->getRelated()), |
| 61 | get_class($this->localRelationship->getRelated()), |
| 62 | $this->localRelationship->getForeignKeyName(), |
| 63 | $distantRelation->getForeignKeyName(), |
| 64 | $this->localRelationship->getLocalKeyName(), |
| 65 | $distantRelation->getLocalKeyName(), |
| 66 | ); |
| 67 | } else { |
| 68 | $returnedRelation = $this->rootModel->hasOneThrough( |
| 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 | } |
| 77 | |
| 78 | if ($this->localRelationship instanceof MorphOneOrMany) { |
| 79 | $returnedRelation->where($this->localRelationship->getQualifiedMorphType(), $this->localRelationship->getMorphClass()); |
| 80 | } |
| 81 | |
| 82 | return $returnedRelation; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Handle dynamic method calls into the model. |
| 87 | * |
| 88 | * @param string $method |
| 89 | * @param array $parameters |
| 90 | * @return mixed |
| 91 | */ |
| 92 | public function __call($method, $parameters) |
| 93 | { |
| 94 | if (Str::startsWith($method, 'has')) { |
| 95 | return $this->has(Str::of($method)->after('has')->lcfirst()->toString()); |
| 96 | } |
| 97 | |
| 98 | throw new BadMethodCallException(sprintf( |
| 99 | 'Call to undefined method %s::%s()', static::class, $method |
| 100 | )); |
| 101 | } |
| 102 | } |
| 103 |