Categorizable::categorize()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 6
1
<?php namespace Arcanedev\Taxonomies\Traits;
2
3
use Arcanedev\Taxonomies\Models\Category;
4
use Illuminate\Database\Eloquent\Model;
5
use Illuminate\Support\Arr;
6
7
/**
8
 * Class     Categorizable
9
 *
10
 * @package  Arcanedev\Taxonomies\Traits
11
 * @author   ARCANEDEV <[email protected]>
12
 *
13
 * @property  \Illuminate\Database\Eloquent\Collection  $categories
14
 *
15
 * @method    \Illuminate\Database\Eloquent\Relations\MorphToMany  morphToMany(string $related, string $name, string $table = null, string $foreignKey = null, $otherKey = null, $inverse = false)
16
 */
17
trait Categorizable
18
{
19
    /* ------------------------------------------------------------------------------------------------
20
     |  Relationships
21
     | ------------------------------------------------------------------------------------------------
22
     */
23
    /**
24
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
25
     */
26
    public function categories()
27
    {
28
        $configs = config('taxonomies.categories', []);
29
30
        return $this->morphToMany(
31
            Arr::get($configs, 'model',       Category::class),
32
            Arr::get($configs, 'morph.name',  'categorizable'),
33
            Arr::get($configs, 'morph.table', 'categories_relations')
34
        );
35
    }
36
37
    /* ------------------------------------------------------------------------------------------------
38
     |  Main Functions
39
     | ------------------------------------------------------------------------------------------------
40
     */
41
    /**
42
     * @param  array  $categories
43
     */
44
    public function categorize($categories)
45
    {
46
        foreach ($categories as $category) {
47
            $this->attachCategory($category);
48
        }
49
    }
50
51
    /**
52
     * Detach all categories.
53
     *
54
     * @param  array  $categories
55
     */
56
    public function uncategorize($categories)
57
    {
58
        foreach ($categories as $category) {
59
            $this->detachCategory($category);
60
        }
61
    }
62
63
    /**
64
     * @param  array  $categories
65
     */
66
    public function recategorize($categories)
67
    {
68
        $this->categories()->sync([]);
69
70
        $this->categorize($categories);
71
    }
72
73
    /**
74
     * Attach a category.
75
     *
76
     * @param  \Illuminate\Database\Eloquent\Model  $category
77
     */
78
    public function attachCategory(Model $category)
79
    {
80
        if ( ! $this->categories->contains($category->getKey())) {
81
            $this->categories()->attach($category);
82
        }
83
    }
84
85
    /**
86
     * Detach a category.
87
     *
88
     * @param  \Illuminate\Database\Eloquent\Model  $category
89
     */
90
    public function detachCategory(Model $category)
91
    {
92
        $this->categories()->detach($category);
93
    }
94
95
    /**
96
     * Get the categories as a list with [id => name].
97
     *
98
     * @return array
99
     */
100
    public function categoriesList()
101
    {
102
        return $this->categories()
103
            ->get()
104
            ->pluck('name', 'id')
105
            ->toArray();
106
    }
107
}
108