1
|
|
|
<?php namespace Arcanedev\LaravelNotes\Traits; |
2
|
|
|
|
3
|
|
|
use Illuminate\Database\Eloquent\Model; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class HasManyNotes |
7
|
|
|
* |
8
|
|
|
* @package Arcanedev\LaravelNotes\Traits |
9
|
|
|
* @author ARCANEDEV <[email protected]> |
10
|
|
|
* |
11
|
|
|
* @property \Illuminate\Database\Eloquent\Collection notes |
12
|
|
|
* |
13
|
|
|
* @method \Illuminate\Database\Eloquent\Relations\MorphMany morphMany(string $related, string $name, string $type = null, string $id = null, string $localKey = null) |
14
|
|
|
*/ |
15
|
|
|
trait HasManyNotes |
16
|
|
|
{ |
17
|
|
|
/* ----------------------------------------------------------------- |
18
|
|
|
| Traits |
19
|
|
|
| ----------------------------------------------------------------- |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
use ConfigHelper; |
23
|
|
|
|
24
|
|
|
/* ----------------------------------------------------------------- |
25
|
|
|
| Relationships |
26
|
|
|
| ----------------------------------------------------------------- |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The notes relationship. |
31
|
|
|
* |
32
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
33
|
|
|
*/ |
34
|
9 |
|
public function notes() |
35
|
|
|
{ |
36
|
9 |
|
return $this->morphMany($this->getModelFromConfig('notes'), 'noteable'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/* ----------------------------------------------------------------- |
40
|
|
|
| Main Methods |
41
|
|
|
| ----------------------------------------------------------------- |
42
|
|
|
*/ |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Create a note. |
46
|
|
|
* |
47
|
|
|
* @param string $content |
48
|
|
|
* @param \Illuminate\Database\Eloquent\Model|null $author |
49
|
|
|
* @param bool $reload |
50
|
|
|
* |
51
|
|
|
* @return \Arcanedev\LaravelNotes\Models\Note |
52
|
|
|
*/ |
53
|
9 |
|
public function createNote($content, $author = null, $reload = true) |
54
|
|
|
{ |
55
|
|
|
/** @var \Arcanedev\LaravelNotes\Models\Note $note */ |
56
|
9 |
|
$note = $this->notes()->create( |
57
|
9 |
|
$this->prepareNoteAttributes($content, $author) |
58
|
3 |
|
); |
59
|
|
|
|
60
|
9 |
|
if ($reload) $this->load(['notes']); |
|
|
|
|
61
|
|
|
|
62
|
9 |
|
return $note; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Retrieve a note by its ID. |
67
|
|
|
* |
68
|
|
|
* @param int $id |
69
|
|
|
* |
70
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
71
|
|
|
*/ |
72
|
3 |
|
public function findNote($id) |
73
|
|
|
{ |
74
|
3 |
|
return $this->notes()->where('id', $id)->first(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/* ----------------------------------------------------------------- |
78
|
|
|
| Other Methods |
79
|
|
|
| ----------------------------------------------------------------- |
80
|
|
|
*/ |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Prepare note attributes. |
84
|
|
|
* |
85
|
|
|
* @param string $content |
86
|
|
|
* @param \Illuminate\Database\Eloquent\Model|null $author |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
9 |
|
protected function prepareNoteAttributes($content, Model $author = null) |
91
|
|
|
{ |
92
|
|
|
return [ |
93
|
9 |
|
'author_id' => is_null($author) ? $this->getCurrentAuthorId() : $author->getKey(), |
94
|
9 |
|
'content' => $content, |
95
|
3 |
|
]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get the current author's id. |
100
|
|
|
* |
101
|
|
|
* @return int|null |
102
|
|
|
*/ |
103
|
6 |
|
protected function getCurrentAuthorId() |
104
|
|
|
{ |
105
|
6 |
|
return null; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
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
Idable
provides a methodequalsId
that 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.