Completed
Push — master ( d42b43...5ac77a )
by ARCANEDEV
8s
created

Permission::checkSlug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php namespace Arcanedev\LaravelAuth\Models;
2
3
use Arcanedev\LaravelAuth\Bases\Model;
4
use Arcanedev\LaravelAuth\Models\Relationships\PermissionRelationships;
5
use Arcanedev\LaravelAuth\Traits\AuthRoleTrait;
6
use Arcanesoft\Contracts\Auth\Models\Permission as PermissionContract;
7
use Illuminate\Support\Str;
8
9
/**
10
 * Class     Permission
11
 *
12
 * @package  Arcanedev\LaravelAuth\Models
13
 * @author   ARCANEDEV <[email protected]>
14
 *
15
 * @property  int                                            id
16
 * @property  int                                            group_id
17
 * @property  string                                         name
18
 * @property  string                                         slug
19
 * @property  string                                         description
20
 * @property  \Carbon\Carbon                                 created_at
21
 * @property  \Carbon\Carbon                                 updated_at
22
 * @property  \Illuminate\Database\Eloquent\Collection       roles
23
 * @property  \Arcanedev\LaravelAuth\Models\PermissionsGroup group
24
 */
25
class Permission extends Model implements PermissionContract
26
{
27
    /* ------------------------------------------------------------------------------------------------
28
     |  Traits
29
     | ------------------------------------------------------------------------------------------------
30
     */
31
    use PermissionRelationships, AuthRoleTrait;
32
33
    /* ------------------------------------------------------------------------------------------------
34
     |  Properties
35
     | ------------------------------------------------------------------------------------------------
36
     */
37
    /**
38
     * The attributes that are mass assignable.
39
     *
40
     * @var array
41
     */
42
    protected $fillable = ['group_id', 'name', 'slug', 'description'];
43
44
    /* ------------------------------------------------------------------------------------------------
45
     |  Constructor
46
     | ------------------------------------------------------------------------------------------------
47
     */
48
    /**
49
     * Create a new Eloquent model instance.
50
     *
51
     * @param  array  $attributes
52
     */
53 528
    public function __construct(array $attributes = [])
54
    {
55 528
        $this->setTable(config('laravel-auth.permissions.table', 'permissions'));
56
57 528
        parent::__construct($attributes);
58 528
    }
59
60
    /* ------------------------------------------------------------------------------------------------
61
     |  Relationships
62
     | ------------------------------------------------------------------------------------------------
63
     */
64
    /**
65
     * Permission belongs to one group.
66
     *
67
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
68
     */
69 8
    public function group()
70
    {
71 8
        return $this->belongsTo(
72
            config('laravel-auth.permissions-groups.model', PermissionsGroup::class),
73
            'group_id'
74
        );
75
    }
76
77
    /* ------------------------------------------------------------------------------------------------
78
     |  Setters & Getters
79
     | ------------------------------------------------------------------------------------------------
80
     */
81
    /**
82
     * Set the slug attribute.
83 232
     *
84
     * @param  string  $slug
85 232
     */
86 232
    public function setSlugAttribute($slug)
87
    {
88
        $this->attributes['slug'] = $this->slugify($slug);
89
    }
90
91
    /* ------------------------------------------------------------------------------------------------
92
     |  Check Functions
93
     | ------------------------------------------------------------------------------------------------
94
     */
95
    /**
96
     * Check if slug is the same as the given value.
97
     *
98
     * @param  string  $value
99
     *
100
     * @return bool
101
     */
102
    public function checkSlug($value)
103
    {
104
        return $this->slug === $this->slugify($value);
105
    }
106
107
    /* ------------------------------------------------------------------------------------------------
108
     |  Other Functions
109
     | ------------------------------------------------------------------------------------------------
110
     */
111
    /**
112
     * Slugify the value.
113
     *
114
     * @param  string  $value
115
     *
116
     * @return string
117
     */
118
    protected function slugify($value)
119
    {
120
        return Str::slug($value, config('laravel-auth.permissions.slug-separator', '.'));
121
    }
122
}
123