Passed
Push — master ( cca94e...624685 )
by Maksim
02:35
created

CompositeBelongsToMany::normalizeValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 1
c 1
b 1
f 0
dl 0
loc 2
rs 10
cc 2
nc 2
nop 1
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
        return ctype_xdigit($value) ? $this->getQuery()->getModel()->recoverBinaryKey($this->relatedKey, $value) : $value;
17
    }
18
19
    private function normalizeIds($ids)
20
    {
21
        return $this->getQuery()->getModel()->hexBinaryColumns() ? (is_array($ids) ? array_map(function ($id) {
22
            return $this->normalizeValue($id);
23
        }, $ids) : $this->normalizeValue($ids)) : $ids;
24
    }
25
26
    /**
27
     * Get all of the IDs from the given mixed value.
28
     *
29
     * @param mixed $value
30
     *
31
     * @return array
32
     */
33
    protected function parseIds($value)
34
    {
35
        if ($value instanceof Model) {
36
            return [$this->executeWithinOptionalBinaryTransformation(function () use ($value) {
37
                return $value->{$this->relatedKey};
38
            }, $value)];
39
        }
40
41
        if ($value instanceof Collection) {
42
            return $this->normalizeIds($value->pluck($this->relatedKey)->all());
43
        }
44
45
        if ($value instanceof BaseCollection) {
46
            return $this->normalizeIds($value->toArray());
47
        }
48
49
        return (array)$this->normalizeIds($value);
50
    }
51
52
    /**
53
     * Get the ID from the given mixed value.
54
     *
55
     * @param mixed $value
56
     *
57
     * @return mixed
58
     */
59
    protected function parseId($value)
60
    {
61
        return $value instanceof Model ? $this->executeWithinOptionalBinaryTransformation(function () use ($value) {
62
            $value->{$this->relatedKey};
63
        }, $value) : $value;
64
    }
65
66
    /**
67
     * Create a new query builder for the pivot table.
68
     *
69
     * @return \Illuminate\Database\Query\Builder
70
     */
71
    protected function newPivotQuery()
72
    {
73
        $query = $this->newPivotStatement();
74
75
        foreach ($this->pivotWheres as $arguments) {
76
            call_user_func_array([$query, 'where'], $arguments);
77
        }
78
79
        foreach ($this->pivotWhereIns as $arguments) {
80
            call_user_func_array([$query, 'whereIn'], $arguments);
81
        }
82
83
        return $this->executeWithinOptionalBinaryTransformation(function () use ($query) {
84
            return $query->where($this->foreignPivotKey, $this->parent->{$this->parentKey});
85
        }, $this->parent);
86
    }
87
88
    /**
89
     * Create a new pivot attachment record.
90
     *
91
     * @param int  $id
92
     * @param bool $timed
93
     *
94
     * @return array
95
     */
96
    protected function baseAttachRecord($id, $timed)
97
    {
98
        $record[$this->relatedPivotKey] = $id;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$record was never initialized. Although not strictly required by PHP, it is generally a good practice to add $record = array(); before regardless.
Loading history...
99
100
        $record[$this->foreignPivotKey] = $this->executeWithinOptionalBinaryTransformation(function () {
101
            return $this->parent->{$this->parentKey};
102
        }, $this->parent);
103
104
        // If the record needs to have creation and update timestamps, we will make
105
        // them by calling the parent model's "freshTimestamp" method which will
106
        // provide us with a fresh timestamp in this model's preferred format.
107
        if ($timed) {
108
            $record = $this->addTimestampsToAttachment($record);
109
        }
110
111
        foreach ($this->pivotValues as $value) {
112
            $record[$value['column']] = $value['value'];
113
        }
114
115
        return $record;
116
    }
117
}