1 | <?php |
||
8 | class BelongsToRelationship |
||
9 | { |
||
10 | /** |
||
11 | * @var ElasticsearchModel |
||
12 | */ |
||
13 | protected $child; |
||
14 | |||
15 | /** |
||
16 | * @var ElasticsearchModel |
||
17 | */ |
||
18 | protected $parentClass; |
||
19 | |||
20 | 6 | public function __construct(ElasticsearchModel $child, $parentClass) |
|
25 | |||
26 | /** |
||
27 | * Associate parent document. |
||
28 | * |
||
29 | * @param ElasticsearchModel $parent |
||
30 | */ |
||
31 | 2 | public function associate(ElasticsearchModel $parent) |
|
35 | |||
36 | /** |
||
37 | * Return parent model. |
||
38 | * |
||
39 | * @return ElasticsearchModel|null |
||
40 | */ |
||
41 | 4 | public function get() |
|
42 | { |
||
43 | 4 | $parent = $this->child->getParent(); |
|
44 | |||
45 | 4 | if ($parent) { |
|
46 | return $parent; |
||
47 | } |
||
48 | |||
49 | 4 | $parentClass = $this->parentClass; |
|
50 | |||
51 | 4 | $parentId = $this->child->getParentId(); |
|
52 | |||
53 | 4 | $innerHits = $this->child->getInnerHits(); |
|
54 | |||
55 | 4 | if ($innerHits) { |
|
56 | 2 | $attributes = $innerHits->getParent($parentClass::getType()); |
|
57 | 2 | $parent = new $parentClass($attributes); |
|
58 | 4 | } elseif ($parentId) { |
|
59 | 2 | $parent = $parentClass::find($parentId); |
|
60 | 2 | } |
|
61 | |||
62 | 4 | if ($parent) { |
|
63 | 4 | $this->child->setParent($parent); |
|
64 | 4 | } |
|
65 | |||
66 | 4 | return $parent; |
|
67 | } |
||
68 | |||
69 | /** |
||
70 | * Return parent model. |
||
71 | * |
||
72 | * @throws ParentModelNotFoundException |
||
73 | * @return ElasticsearchModel |
||
74 | */ |
||
75 | 4 | public function getOrFail() |
|
85 | } |
||
86 |