Passed
Push — master ( f743e5...0a7266 )
by Maksim
02:50
created

CompositeBelongsToMany::parseId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 3
c 1
b 1
f 0
dl 0
loc 5
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
4
namespace MaksimM\CompositePrimaryKeys\Eloquent\Relationships;
5
6
7
use Illuminate\Database\Eloquent\Collection;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
10
use Illuminate\Support\Collection as BaseCollection;
11
use MaksimM\CompositePrimaryKeys\Http\Traits\CompositeRelationships;
12
13
class CompositeBelongsToMany extends BelongsToMany
14
{
15
16
    use CompositeRelationships;
17
18
19
    /**
20
     * Get all of the IDs from the given mixed value.
21
     *
22
     * @param  mixed  $value
23
     * @return array
24
     */
25
    protected function parseIds($value)
26
    {
27
        if ($value instanceof Model) {
28
            return [$this->executeWithinOptionalBinaryTransformation(function() use ($value) {
29
                return $value->{$this->relatedKey};
30
            }, $value)];
31
        }
32
33
        if ($value instanceof Collection) {
34
            return $value->pluck($this->relatedKey)->all();
35
        }
36
37
        if ($value instanceof BaseCollection) {
38
            return $value->toArray();
39
        }
40
41
        return (array) $value;
42
    }
43
44
    /**
45
     * Get the ID from the given mixed value.
46
     *
47
     * @param  mixed  $value
48
     * @return mixed
49
     */
50
    protected function parseId($value)
51
    {
52
        return $value instanceof Model ? $this->executeWithinOptionalBinaryTransformation(function() use ($value) {
53
            $value->{$this->relatedKey};
54
        }, $value): $value;
55
    }
56
57
}