1 | <?php |
||
2 | |||
3 | namespace MaksimM\CompositePrimaryKeys\Eloquent\Relationships; |
||
4 | |||
5 | use Illuminate\Database\Eloquent\Collection; |
||
6 | use Illuminate\Database\Eloquent\Model; |
||
7 | use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
||
8 | use Illuminate\Support\Collection as BaseCollection; |
||
9 | use MaksimM\CompositePrimaryKeys\Http\Traits\CompositeRelationships; |
||
10 | |||
11 | class CompositeBelongsToMany extends BelongsToMany |
||
12 | { |
||
13 | use CompositeRelationships; |
||
14 | |||
15 | private function normalizeValue($value) |
||
16 | { |
||
17 | return ctype_xdigit($value) ? $this->getQuery()->getModel()->recoverBinaryKey($this->relatedKey, $value) : $value; |
||
18 | } |
||
19 | |||
20 | private function normalizeIds($ids) |
||
21 | { |
||
22 | return $this->getQuery()->getModel()->hexBinaryColumns() ? (is_array($ids) ? array_map(function ($id) { |
||
23 | return $this->normalizeValue($id); |
||
24 | }, $ids) : $this->normalizeValue($ids)) : $ids; |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * Get all of the IDs from the given mixed value. |
||
29 | * |
||
30 | * @param mixed $value |
||
31 | * |
||
32 | * @return array |
||
33 | */ |
||
34 | protected function parseIds($value) |
||
35 | { |
||
36 | if ($value instanceof Model) { |
||
37 | return [$this->executeWithinOptionalBinaryTransformation(function () use ($value) { |
||
38 | return $value->{$this->relatedKey}; |
||
39 | }, $value)]; |
||
40 | } |
||
41 | |||
42 | if ($value instanceof Collection) { |
||
43 | return $this->normalizeIds($value->pluck($this->relatedKey)->all()); |
||
44 | } |
||
45 | |||
46 | if ($value instanceof BaseCollection) { |
||
47 | return $this->normalizeIds($value->toArray()); |
||
48 | } |
||
49 | |||
50 | return (array) $this->normalizeIds($value); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Get the ID from the given mixed value. |
||
55 | * |
||
56 | * @param mixed $value |
||
57 | * |
||
58 | * @return mixed |
||
59 | */ |
||
60 | protected function parseId($value) |
||
61 | { |
||
62 | return $value instanceof Model ? $this->executeWithinOptionalBinaryTransformation(function () use ($value) { |
||
63 | $value->{$this->relatedKey}; |
||
64 | }, $value) : $value; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Create a new query builder for the pivot table. |
||
69 | * |
||
70 | * @return \Illuminate\Database\Query\Builder |
||
71 | */ |
||
72 | protected function newPivotQuery() |
||
73 | { |
||
74 | $query = $this->newPivotStatement(); |
||
75 | |||
76 | foreach ($this->pivotWheres as $arguments) { |
||
77 | call_user_func_array([$query, 'where'], $arguments); |
||
78 | } |
||
79 | |||
80 | foreach ($this->pivotWhereIns as $arguments) { |
||
81 | call_user_func_array([$query, 'whereIn'], $arguments); |
||
82 | } |
||
83 | |||
84 | return $this->executeWithinOptionalBinaryTransformation(function () use ($query) { |
||
85 | return $query->where($this->foreignPivotKey, $this->parent->{$this->parentKey}); |
||
86 | }, $this->parent); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Create a new pivot attachment record. |
||
91 | * |
||
92 | * @param int $id |
||
93 | * @param bool $timed |
||
94 | * |
||
95 | * @return array |
||
96 | */ |
||
97 | protected function baseAttachRecord($id, $timed) |
||
98 | { |
||
99 | $record[$this->relatedPivotKey] = $id; |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
100 | |||
101 | $record[$this->foreignPivotKey] = $this->executeWithinOptionalBinaryTransformation(function () { |
||
102 | return $this->parent->{$this->parentKey}; |
||
103 | }, $this->parent); |
||
104 | |||
105 | // If the record needs to have creation and update timestamps, we will make |
||
106 | // them by calling the parent model's "freshTimestamp" method which will |
||
107 | // provide us with a fresh timestamp in this model's preferred format. |
||
108 | if ($timed) { |
||
109 | $record = $this->addTimestampsToAttachment($record); |
||
110 | } |
||
111 | |||
112 | foreach ($this->pivotValues as $value) { |
||
113 | $record[$value['column']] = $value['value']; |
||
114 | } |
||
115 | |||
116 | return $record; |
||
117 | } |
||
118 | } |
||
119 |