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
|
|
|
| Relationships |
19
|
|
|
| ----------------------------------------------------------------- |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The notes relationship. |
24
|
|
|
* |
25
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
26
|
|
|
*/ |
27
|
15 |
|
public function notes() |
28
|
|
|
{ |
29
|
15 |
|
return $this->morphMany(config('notes.notes.model'), 'noteable'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/* ----------------------------------------------------------------- |
33
|
|
|
| Main Methods |
34
|
|
|
| ----------------------------------------------------------------- |
35
|
|
|
*/ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Create a note. |
39
|
|
|
* |
40
|
|
|
* @param string $content |
41
|
|
|
* @param \Illuminate\Database\Eloquent\Model|null $author |
42
|
|
|
* @param bool $reload |
43
|
|
|
* |
44
|
|
|
* @return \Arcanedev\LaravelNotes\Models\Note |
45
|
|
|
*/ |
46
|
15 |
|
public function createNote($content, $author = null, $reload = true) |
47
|
|
|
{ |
48
|
|
|
/** @var \Arcanedev\LaravelNotes\Models\Note $note */ |
49
|
15 |
|
$note = $this->notes()->create( |
50
|
15 |
|
$this->prepareNoteAttributes($content, $author) |
51
|
|
|
); |
52
|
|
|
|
53
|
15 |
|
$relations = array_merge(['notes'], method_exists($this, 'authoredNotes') ? ['authoredNotes'] : []); |
54
|
|
|
|
55
|
15 |
|
if ($reload) $this->load($relations); |
|
|
|
|
56
|
|
|
|
57
|
15 |
|
return $note; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Retrieve a note by its ID. |
62
|
|
|
* |
63
|
|
|
* @param int $id |
64
|
|
|
* |
65
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
66
|
|
|
*/ |
67
|
3 |
|
public function findNote($id) |
68
|
|
|
{ |
69
|
3 |
|
return $this->notes()->where('id', $id)->first(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/* ----------------------------------------------------------------- |
73
|
|
|
| Other Methods |
74
|
|
|
| ----------------------------------------------------------------- |
75
|
|
|
*/ |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Prepare note attributes. |
79
|
|
|
* |
80
|
|
|
* @param string $content |
81
|
|
|
* @param \Illuminate\Database\Eloquent\Model|null $author |
82
|
|
|
* |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
15 |
|
protected function prepareNoteAttributes($content, Model $author = null) |
86
|
|
|
{ |
87
|
|
|
return [ |
88
|
15 |
|
'author_id' => is_null($author) ? $this->getCurrentAuthorId() : $author->getKey(), |
89
|
15 |
|
'content' => $content, |
90
|
|
|
]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get the current author's id. |
95
|
|
|
* |
96
|
|
|
* @return int|null |
97
|
|
|
*/ |
98
|
6 |
|
protected function getCurrentAuthorId() |
99
|
|
|
{ |
100
|
6 |
|
return null; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
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.