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
|
|
|
* @method \Illuminate\Database\Eloquent\Relations\MorphOne morphOne(string $related, string $name, string $type = null, string $id = null, string $localKey = null) |
14
|
|
|
*/ |
15
|
|
|
trait HasOneNote |
16
|
|
|
{ |
17
|
|
|
/* ----------------------------------------------------------------- |
18
|
|
|
| Traits |
19
|
|
|
| ----------------------------------------------------------------- |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
use ConfigHelper; |
23
|
|
|
|
24
|
|
|
/* ----------------------------------------------------------------- |
25
|
|
|
| Relationships |
26
|
|
|
| ----------------------------------------------------------------- |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Relation to ONE note. |
31
|
|
|
* |
32
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphOne |
33
|
|
|
*/ |
34
|
15 |
|
public function note() |
35
|
|
|
{ |
36
|
15 |
|
return $this->morphOne($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
|
15 |
|
public function createNote($content, $author = null, $reload = true) |
54
|
|
|
{ |
55
|
15 |
|
if ($this->note) $this->note->delete(); |
56
|
|
|
|
57
|
|
|
/** @var \Arcanedev\LaravelNotes\Models\Note $note */ |
58
|
15 |
|
$note = $this->note()->create( |
59
|
15 |
|
$this->prepareNoteAttributes($content, $author) |
60
|
5 |
|
); |
61
|
|
|
|
62
|
15 |
|
if ($reload) $this->load(['note']); |
|
|
|
|
63
|
|
|
|
64
|
15 |
|
return $note; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Update a note. |
69
|
|
|
* |
70
|
|
|
* @param string $content |
71
|
|
|
* @param \Illuminate\Database\Eloquent\Model|null $author |
72
|
|
|
* @param bool $reload |
73
|
|
|
* |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
3 |
|
public function updateNote($content, Model $author = null, $reload = true) |
77
|
|
|
{ |
78
|
3 |
|
$updated = $this->note->update( |
79
|
3 |
|
$this->prepareNoteAttributes($content, $author) |
80
|
1 |
|
); |
81
|
|
|
|
82
|
3 |
|
if ($reload) $this->load(['note']); |
|
|
|
|
83
|
|
|
|
84
|
3 |
|
return $updated; |
85
|
1 |
|
} |
86
|
|
|
|
87
|
|
|
/* ----------------------------------------------------------------- |
88
|
|
|
| Other Methods |
89
|
|
|
| ----------------------------------------------------------------- |
90
|
|
|
*/ |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Prepare note attributes. |
94
|
|
|
* |
95
|
|
|
* @param string $content |
96
|
|
|
* @param \Illuminate\Database\Eloquent\Model|null $author |
97
|
|
|
* |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
15 |
|
protected function prepareNoteAttributes($content, Model $author = null) |
101
|
|
|
{ |
102
|
|
|
return [ |
103
|
15 |
|
'author_id' => is_null($author) ? $this->getCurrentAuthorId() : $author->getKey(), |
104
|
15 |
|
'content' => $content, |
105
|
5 |
|
]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get the current author's id. |
110
|
|
|
* |
111
|
|
|
* @return int|null |
112
|
|
|
*/ |
113
|
12 |
|
protected function getCurrentAuthorId() |
114
|
|
|
{ |
115
|
12 |
|
return null; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
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.