1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Moo\FlashCard\Entity; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Moo\FlashCard\Traits\Validatable; |
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Category is the entity class that represents a record from the database. |
11
|
|
|
* |
12
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class Category extends Model |
15
|
|
|
{ |
16
|
|
|
use Validatable; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Constant for card active status |
20
|
|
|
* |
21
|
|
|
* @var bool |
22
|
|
|
*/ |
23
|
|
|
const ACTIVE = true; |
24
|
|
|
/** |
25
|
|
|
* Constant for card inactive status |
26
|
|
|
* |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
const INACTIVE = false; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Timestamp enabled. |
33
|
|
|
* |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
public $timestamps = true; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Name of database table. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $table = 'card_category'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* List of allowed columns to be used in $this->fill(). |
47
|
|
|
* |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected $fillable = ['title', 'description', 'parent', 'active', 'color']; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get collection of cards in the category. |
54
|
|
|
*/ |
55
|
|
|
public function cards() |
56
|
|
|
{ |
57
|
|
|
return $this->hasMany(Card::class); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get collection of children categories |
62
|
|
|
*/ |
63
|
|
|
public function children() |
64
|
|
|
{ |
65
|
|
|
return $this->hasMany(Category::class, 'parent'); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get the parent category |
70
|
|
|
*/ |
71
|
|
|
public function parent() |
72
|
|
|
{ |
73
|
|
|
return $this->belongsTo(Category::class, 'parent'); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get number of cards in a category |
78
|
|
|
* |
79
|
|
|
* @return int |
80
|
|
|
*/ |
81
|
|
|
public function countCards() |
82
|
|
|
{ |
83
|
|
|
return $this->cards()->count(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get collection of validation rules for model attributes |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
protected function getRules(): array |
92
|
|
|
{ |
93
|
|
|
return [ |
94
|
|
|
'title' => 'required|max:255|min:5', |
95
|
|
|
]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Scope to filter by active categories |
100
|
|
|
* |
101
|
|
|
* @param Builder $query |
102
|
|
|
* @return Builder |
103
|
|
|
*/ |
104
|
|
|
public function scopeActive(Builder $query): Builder |
105
|
|
|
{ |
106
|
|
|
return $query->where('active', '=', static::ACTIVE); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Score to filter by LIKE search |
111
|
|
|
* |
112
|
|
|
* @param Builder $query |
113
|
|
|
* @param string $keyword |
114
|
|
|
* @return Builder |
115
|
|
|
*/ |
116
|
|
View Code Duplication |
public function scopeSearch(Builder $query, string $keyword): Builder |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$query->where('title', 'LIKE', '%' . $keyword . '%'); |
119
|
|
|
$query->orWhere('description', 'LIKE', '%' . $keyword . '%'); |
120
|
|
|
|
121
|
|
|
return $query; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
This check looks for accesses to local static members using the fully qualified name instead of
self::
.While this is perfectly valid, the fully qualified name of
Certificate::TRIPLEDES_CBC
could just as well be replaced byself::TRIPLEDES_CBC
. Referencing local members withself::
assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.