|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @file |
|
5
|
|
|
* Contains \Drupal\crop\Entity\CropType. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Drupal\crop\Entity; |
|
9
|
|
|
|
|
10
|
|
|
use Drupal\Core\Config\Entity\ConfigEntityBundleBase; |
|
11
|
|
|
use Drupal\Core\Entity\EntityConstraintViolationList; |
|
12
|
|
|
use Drupal\crop\CropTypeInterface; |
|
13
|
|
|
use Symfony\Component\Validator\ConstraintViolationList; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Defines the Crop type configuration entity. |
|
17
|
|
|
* |
|
18
|
|
|
* @ConfigEntityType( |
|
19
|
|
|
* id = "crop_type", |
|
20
|
|
|
* label = @Translation("Crop type"), |
|
21
|
|
|
* handlers = { |
|
22
|
|
|
* "form" = { |
|
23
|
|
|
* "add" = "Drupal\crop\Form\CropTypeForm", |
|
24
|
|
|
* "edit" = "Drupal\crop\Form\CropTypeForm", |
|
25
|
|
|
* "delete" = "Drupal\crop\Form\CropTypeDeleteForm" |
|
26
|
|
|
* }, |
|
27
|
|
|
* "list_builder" = "Drupal\crop\CropTypeListBuilder", |
|
28
|
|
|
* }, |
|
29
|
|
|
* admin_permission = "administer crop types", |
|
30
|
|
|
* config_prefix = "type", |
|
31
|
|
|
* bundle_of = "crop", |
|
32
|
|
|
* entity_keys = { |
|
33
|
|
|
* "id" = "id", |
|
34
|
|
|
* "label" = "label", |
|
35
|
|
|
* }, |
|
36
|
|
|
* links = { |
|
37
|
|
|
* "edit-form" = "/admin/structure/crop/manage/{crop_type}", |
|
38
|
|
|
* "delete-form" = "/admin/structure/crop/manage/{crop_type}/delete", |
|
39
|
|
|
* }, |
|
40
|
|
|
* constraints = { |
|
41
|
|
|
* "CropTypeMachineNameValidation" = {}, |
|
42
|
|
|
* "CropTypeAspectRatioValidation" = {}, |
|
43
|
|
|
* } |
|
44
|
|
|
* ) |
|
45
|
|
|
*/ |
|
46
|
|
|
class CropType extends ConfigEntityBundleBase implements \IteratorAggregate, CropTypeInterface { |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* The machine name of this crop type. |
|
50
|
|
|
* |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
public $id; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* The human-readable name of the crop type. |
|
57
|
|
|
* |
|
58
|
|
|
* @var string |
|
59
|
|
|
*/ |
|
60
|
|
|
public $label; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* A brief description of this crop type. |
|
64
|
|
|
* |
|
65
|
|
|
* @var string |
|
66
|
|
|
*/ |
|
67
|
|
|
public $description; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* The ratio of the image of this crop type. |
|
71
|
|
|
* |
|
72
|
|
|
* @var string |
|
73
|
|
|
*/ |
|
74
|
|
|
public $aspect_ratio; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Soft limit width in px. |
|
78
|
|
|
* |
|
79
|
|
|
* @var int |
|
80
|
|
|
*/ |
|
81
|
|
|
public $soft_limit_width; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Soft limit height in px. |
|
85
|
|
|
* |
|
86
|
|
|
* @var int |
|
87
|
|
|
*/ |
|
88
|
|
|
public $soft_limit_height; |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Hard limit width in px. |
|
92
|
|
|
* |
|
93
|
|
|
* @var int |
|
94
|
|
|
*/ |
|
95
|
|
|
public $hard_limit_width; |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Hard limit height in px. |
|
99
|
|
|
* |
|
100
|
|
|
* @var int |
|
101
|
|
|
*/ |
|
102
|
|
|
public $hard_limit_height; |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* {@inheritdoc} |
|
106
|
|
|
*/ |
|
107
|
|
|
public function id() { |
|
108
|
|
|
return $this->id; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* {@inheritdoc} |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getAspectRatio() { |
|
115
|
|
|
return $this->aspect_ratio; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* {@inheritdoc} |
|
120
|
|
|
*/ |
|
121
|
|
|
public function validate() { |
|
122
|
|
|
$violations = $this->getTypedData()->validate(); |
|
123
|
|
|
return new ConstraintViolationList(iterator_to_array($violations)); |
|
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* {@inheritdoc} |
|
128
|
|
|
*/ |
|
129
|
|
|
public function getIterator() { |
|
130
|
|
|
return new \ArrayIterator(); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* {@inheritdoc} |
|
135
|
|
|
*/ |
|
136
|
|
|
public static function getCropTypeNames() { |
|
137
|
|
|
return array_map( |
|
138
|
|
|
function ($bundle_info) { return $bundle_info['label'];}, |
|
139
|
|
|
\Drupal::entityManager()->getBundleInfo('crop') |
|
140
|
|
|
); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* {@inheritdoc} |
|
145
|
|
|
*/ |
|
146
|
|
|
public function getSoftLimit() { |
|
147
|
|
|
return [ |
|
148
|
|
|
'width' => $this->soft_limit_width, |
|
149
|
|
|
'height' => $this->soft_limit_height, |
|
150
|
|
|
]; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* {@inheritdoc} |
|
155
|
|
|
*/ |
|
156
|
|
|
public function getHardLimit() { |
|
157
|
|
|
return [ |
|
158
|
|
|
'width' => $this->hard_limit_width, |
|
159
|
|
|
'height' => $this->hard_limit_height, |
|
160
|
|
|
]; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
} |
|
164
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.