|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Tinyissue package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Mohamed Alsharaf <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Tinyissue\Model\Traits\Tag; |
|
13
|
|
|
|
|
14
|
|
|
use Illuminate\Database\Eloquent; |
|
15
|
|
|
use Tinyissue\Model\Tag; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* CrudTrait is trait class containing the methods for adding/editing/deleting the Tag model. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
|
21
|
|
|
* |
|
22
|
|
|
* @method Eloquent\Model where($column, $operator = null, $value = null, $boolean = 'and') |
|
23
|
|
|
*/ |
|
24
|
|
|
trait CrudTrait |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Create a new tag. |
|
28
|
|
|
* |
|
29
|
|
|
* @param array $input |
|
30
|
|
|
* |
|
31
|
|
|
* @return mixed |
|
32
|
|
|
*/ |
|
33
|
1 |
|
public function createTag(array $input) |
|
34
|
|
|
{ |
|
35
|
1 |
|
return $this->fill($this->prepareTagToSave($input))->save(); |
|
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Update the tags in the database. |
|
40
|
|
|
* |
|
41
|
|
|
* @param array $attributes |
|
42
|
|
|
* |
|
43
|
|
|
* @return bool|int |
|
44
|
|
|
*/ |
|
45
|
1 |
|
public function update(array $attributes = []) |
|
46
|
|
|
{ |
|
47
|
1 |
|
return parent::update($this->prepareTagToSave($attributes)); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Create new tag from string of group name and tag name. |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $tagFullName |
|
54
|
|
|
* |
|
55
|
|
|
* @return $this|bool |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public function createTagFromString($tagFullName) |
|
58
|
|
|
{ |
|
59
|
1 |
|
list($groupName, $tagName) = explode(':', $tagFullName); |
|
60
|
|
|
|
|
61
|
|
|
// Check if group name is valid |
|
62
|
1 |
|
$groupTag = $this->validOrCreate($groupName); |
|
63
|
|
|
|
|
64
|
1 |
|
if (!$groupTag) { |
|
65
|
|
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// Create new tag or return existing one |
|
69
|
1 |
|
return $this->validOrCreate($tagName, $groupTag); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Create a new tag if valid or return existing one. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $name |
|
76
|
|
|
* @param null|Tag $parent |
|
77
|
|
|
* |
|
78
|
|
|
* @return bool|$this |
|
79
|
|
|
*/ |
|
80
|
1 |
|
public function validOrCreate($name, Tag $parent = null) |
|
81
|
|
|
{ |
|
82
|
1 |
|
$group = $parent === null ? true : false; |
|
83
|
1 |
|
$tag = $this->where('name', '=', $name)->first(); |
|
84
|
1 |
|
if ($tag && $tag->group != $group) { |
|
85
|
|
|
return false; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
if (!$tag) { |
|
89
|
1 |
|
$tag = new Tag(); |
|
90
|
1 |
|
$tag->name = $name; |
|
91
|
1 |
|
$tag->group = $group; |
|
92
|
1 |
|
if (!is_null($parent)) { |
|
93
|
1 |
|
$tag->parent_id = $parent->id; |
|
94
|
1 |
|
$tag->setRelation('parent', $parent); |
|
95
|
|
|
} |
|
96
|
1 |
|
$tag->save(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
return $tag; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Prepare tag details to save. |
|
104
|
|
|
* |
|
105
|
|
|
* @param array $input |
|
106
|
|
|
* |
|
107
|
|
|
* @return array |
|
108
|
|
|
*/ |
|
109
|
2 |
|
protected function prepareTagToSave(array $input) |
|
110
|
|
|
{ |
|
111
|
|
|
// If parent id is set, then tag is not group |
|
112
|
2 |
|
if (array_key_exists('parent_id', $input) && $input['parent_id'] > 0) { |
|
113
|
2 |
|
$input['group'] = 0; |
|
114
|
|
|
} else { |
|
115
|
|
|
$input['group'] = 1; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
2 |
|
return $input; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.