SeoComponent.php
5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
namespace artbox\core\components;
use artbox\core\exceptions\AliasOverwriteException;
use artbox\core\models\interfaces\AliasInterface;
use yii\base\Object;
use yii\db\ActiveRecord;
use yii\helpers\Json;
/**
* Class SeoComponent
*
*@package artbox\core\components
* @property AliasInterface $alias
* @property string $title
* @property string $desc
* @property string $h1
* @property string $text
* @property string $robots
* @property integer $aliasId
* @property boolean $loaded
*/
class SeoComponent extends Object
{
/**
* Property to show wether Alias was loaded or not
*
* @var bool
*/
protected $loaded = false;
/**
* Data that will be replaced in seo fields
*
* @var array
*/
protected $fields = [];
/**
* Seo title
*
* @var string
*/
protected $title;
/**
* meta description
*
* @var string
*/
protected $desc;
/**
* h1 tag on pages
*
* @var string
*/
protected $h1;
/**
* Seo text
*
* @var string
*/
protected $text;
/**
* The alias model that contains seo data
* sets in Seo url manager
*
* @var AliasInterface
*/
protected $alias;
/**
* ActiveRecord instance that contains actual data for replacing
*
* @var ActiveRecord
*/
protected $model;
/**
* Getter for protected property
*
* @return bool
*/
public function getLoaded(): bool
{
return $this->loaded;
}
/**
* Setter for protected property
*
* @return void
*/
public function setLoaded(bool $status)
{
$this->loaded = $status;
}
/**
* Setting alias model
*
* @param \artbox\core\models\interfaces\AliasInterface $alias
*
* @throws \artbox\core\exceptions\AliasOverwriteException
*/
public function setAlias(AliasInterface $alias)
{
if ($this->loaded) {
throw new AliasOverwriteException();
}
$this->alias = $alias;
$this->title = $alias->getTitle();
$this->desc = $alias->getDesc();
$this->h1 = $alias->getH1();
$this->text = $alias->getText();
$this->loaded = true;
}
/**
* @return int
*/
public function getAliasId()
{
return $this->alias->getId();
}
/**
* @return AliasInterface
*/
public function getAlias()
{
return $this->alias;
}
/**
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* @return string
*/
public function getDesc()
{
return $this->desc;
}
/**
* @return string
*/
public function getH1()
{
return $this->h1;
}
/**
* @return string
*/
public function getText()
{
return $this->text;
}
/**
* @return string
*/
public function getRobots()
{
if (!empty($this->alias) && !empty($this->alias->getRobots())) {
return $this->alias->getRobots();
}
return 'all';
}
/**
* Setting up ActiveRecord model and starting to generate data
*
* @param \yii\db\ActiveRecord $model
*/
public function setModel(ActiveRecord $model)
{
$this->model = $model;
$this->generateData();
}
/**
* Trying to get field values with all dependecies
* if it fails fill it with empty string
*
* @return void
*/
protected function populateFields()
{
$fields = array_keys(Json::decode($this->alias->getFields()) ? : []);
foreach ($fields as $field) {
$params = explode('.', $field);
$chain = $this->model;
foreach ($params as $param) {
if (!empty($chain->$param)) {
$chain = $chain->$param;
} else {
$chain = '';
break;
}
}
$this->fields[ '[[' . $field . ']]' ] = $chain;
}
}
/**
* Populating self data with modified information from Alias model
*
* @return void
*/
protected function generateData()
{
$this->populateFields();
$title = $this->alias->getTitle();
$desc = $this->alias->getDesc();
$h1 = $this->alias->getH1();
$text = $this->alias->getSeoText();
foreach ($this->fields as $search => $replace) {
$title = str_replace($search, $replace, $title);
$desc = str_replace($search, $replace, $desc);
$h1 = str_replace($search, $replace, $h1);
$text = str_replace($search, $replace, $text);
}
$this->title = $title;
$this->desc = $desc;
$this->h1 = $h1;
$this->text = $text;
}
}