1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Larafolio\Models\ContentTraits; |
4
|
|
|
|
5
|
|
|
use Larafolio\Models\TextLine; |
6
|
|
|
|
7
|
|
View Code Duplication |
trait HasLines |
|
|
|
|
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Get a model from a relationship by model name. |
11
|
|
|
* |
12
|
|
|
* @param string $relationship Name of relationship. |
13
|
|
|
* @param string $name Name of model to get. |
14
|
|
|
* |
15
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null |
16
|
|
|
*/ |
17
|
|
|
abstract protected function getFromRelationshipByName($relationship, $name); |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Define a polymorphic one-to-many relationship. |
21
|
|
|
* |
22
|
|
|
* @param string $related |
23
|
|
|
* @param string $name |
24
|
|
|
* @param string $type |
25
|
|
|
* @param string $id |
26
|
|
|
* @param string $localKey |
27
|
|
|
* |
28
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
29
|
|
|
*/ |
30
|
|
|
abstract public function morphMany($related, $name, $type = null, $id = null, $localKey = null); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* A resource has many text lines. |
34
|
|
|
* |
35
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
36
|
|
|
*/ |
37
|
|
|
public function lines() |
38
|
|
|
{ |
39
|
|
|
return $this->morphMany(TextLine::class, 'resource'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Return true if project has lines. |
44
|
|
|
* |
45
|
|
|
* @return bool |
46
|
|
|
*/ |
47
|
|
|
public function hasLines() |
48
|
|
|
{ |
49
|
|
|
return !$this->lines->isEmpty(); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get a text line by name, if exists. |
54
|
|
|
* |
55
|
|
|
* @param string $name Name of text line to get. |
56
|
|
|
* |
57
|
|
|
* @return \Larafolio\Models\TextBlock|null |
58
|
|
|
*/ |
59
|
|
|
public function line($name) |
60
|
|
|
{ |
61
|
|
|
return $this->getFromRelationshipByName('lines', $name); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get line text by line name, if line exists. |
66
|
|
|
* |
67
|
|
|
* @param string $name Name of text line to get. |
68
|
|
|
* @param bool $formatted If true, return formmated text. |
69
|
|
|
* |
70
|
|
|
* @return string|null |
71
|
|
|
*/ |
72
|
|
|
public function lineText($name, $formatted = true) |
73
|
|
|
{ |
74
|
|
|
if (!$line = $this->line($name)) { |
75
|
|
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if ($formatted) { |
79
|
|
|
return $line->formattedText(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $line->text(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.