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