1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Larafolio\Models\Traits; |
4
|
|
|
|
5
|
|
|
use Larafolio\Models\Image; |
6
|
|
|
|
7
|
|
|
trait HasImages |
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
|
|
|
* A project has many images. |
21
|
|
|
* |
22
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
23
|
|
|
*/ |
24
|
|
|
public function images() |
25
|
|
|
{ |
26
|
|
|
return $this->hasMany(Image::class); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Return true if project has images. |
31
|
|
|
* |
32
|
|
|
* @return bool |
33
|
|
|
*/ |
34
|
|
|
public function hasImages() |
35
|
|
|
{ |
36
|
|
|
return !$this->images->isEmpty(); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get image by name, if exists. |
41
|
|
|
* |
42
|
|
|
* @param string $name Name of image to get. |
43
|
|
|
* |
44
|
|
|
* @return Larafolio\Models\Image|null |
45
|
|
|
*/ |
46
|
|
|
public function image($name) |
47
|
|
|
{ |
48
|
|
|
return $this->getFromRelationshipByName('images', $name); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get image url for given size. |
53
|
|
|
* |
54
|
|
|
* @param string $name Name of image to get url for. |
55
|
|
|
* @param string $size Size of image. |
56
|
|
|
* |
57
|
|
|
* @return string|null |
58
|
|
|
*/ |
59
|
|
|
public function imageUrl($name, $size = 'medium') |
60
|
|
|
{ |
61
|
|
|
if (!$image = $this->image($name)) { |
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $image->{$size}(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get caption for image. |
70
|
|
|
* |
71
|
|
|
* @param string $name Name of image to get caption for. |
72
|
|
|
* |
73
|
|
|
* @return string|null |
74
|
|
|
*/ |
75
|
|
|
public function imageCaption($name) |
76
|
|
|
{ |
77
|
|
|
if (!$image = $this->image($name)) { |
78
|
|
|
return; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $image->caption(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get alt for image. |
86
|
|
|
* |
87
|
|
|
* @param string $name Name of image to get caption for. |
88
|
|
|
* |
89
|
|
|
* @return string|null |
90
|
|
|
*/ |
91
|
|
|
public function imageAlt($name) |
92
|
|
|
{ |
93
|
|
|
if (!$image = $this->image($name)) { |
94
|
|
|
return; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $image->alt(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Return images with all props needed for javascript. |
102
|
|
|
* |
103
|
|
|
* @return Collection |
104
|
|
|
*/ |
105
|
|
|
public function imagesWithProps() |
106
|
|
|
{ |
107
|
|
|
return $this->images |
108
|
|
|
->map(function (Image $image) { |
109
|
|
|
return $image->generateProps(); |
110
|
|
|
})->reverse()->values(); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
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.