Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
621 lars 1
<?php
2
 
3
namespace Illuminate\Database\Eloquent;
4
 
5
use BadMethodCallException;
6
use Illuminate\Database\Eloquent\Relations\HasMany;
7
use Illuminate\Support\Str;
8
 
9
class PendingHasThroughRelationship
10
{
11
    /**
12
     * The root model that the relationship exists on.
13
     *
14
     * @var \Illuminate\Database\Eloquent\Model
15
     */
16
    protected $rootModel;
17
 
18
    /**
19
     * The local relationship.
20
     *
21
     * @var \Illuminate\Database\Eloquent\Relations\HasMany|\Illuminate\Database\Eloquent\Relations\HasOne
22
     */
23
    protected $localRelationship;
24
 
25
    /**
26
     * Create a pending has-many-through or has-one-through relationship.
27
     *
28
     * @param  \Illuminate\Database\Eloquent\Model  $rootModel
29
     * @param  \Illuminate\Database\Eloquent\Relations\HasMany|\Illuminate\Database\Eloquent\Relations\HasOne  $localRelationship
30
     */
31
    public function __construct($rootModel, $localRelationship)
32
    {
33
        $this->rootModel = $rootModel;
34
 
35
        $this->localRelationship = $localRelationship;
36
    }
37
 
38
    /**
39
     * Define the distant relationship that this model has.
40
     *
41
     * @param  string|(callable(\Illuminate\Database\Eloquent\Model): (\Illuminate\Database\Eloquent\Relations\HasOne|\Illuminate\Database\Eloquent\Relations\HasMany))  $callback
42
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough|\Illuminate\Database\Eloquent\Relations\HasOneThrough
43
     */
44
    public function has($callback)
45
    {
46
        if (is_string($callback)) {
47
            $callback = fn () => $this->localRelationship->getRelated()->{$callback}();
48
        }
49
 
50
        $distantRelation = $callback($this->localRelationship->getRelated());
51
 
52
        if ($distantRelation instanceof HasMany) {
53
            return $this->rootModel->hasManyThrough(
54
                $distantRelation->getRelated()::class,
55
                $this->localRelationship->getRelated()::class,
56
                $this->localRelationship->getForeignKeyName(),
57
                $distantRelation->getForeignKeyName(),
58
                $this->localRelationship->getLocalKeyName(),
59
                $distantRelation->getLocalKeyName(),
60
            );
61
        }
62
 
63
        return $this->rootModel->hasOneThrough(
64
            $distantRelation->getRelated()::class,
65
            $this->localRelationship->getRelated()::class,
66
            $this->localRelationship->getForeignKeyName(),
67
            $distantRelation->getForeignKeyName(),
68
            $this->localRelationship->getLocalKeyName(),
69
            $distantRelation->getLocalKeyName(),
70
        );
71
    }
72
 
73
    /**
74
     * Handle dynamic method calls into the model.
75
     *
76
     * @param  string  $method
77
     * @param  array  $parameters
78
     * @return mixed
79
     */
80
    public function __call($method, $parameters)
81
    {
82
        if (Str::startsWith($method, 'has')) {
83
            return $this->has(Str::of($method)->after('has')->lcfirst()->toString());
84
        }
85
 
86
        throw new BadMethodCallException(sprintf(
87
            'Call to undefined method %s::%s()', static::class, $method
88
        ));
89
    }
90
}