ProductMicrodata.php
4.37 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
<?php
namespace frontend\microdata;
use yii\base\BaseObject;
class ProductMicrodata extends Microdata
{
public $type=null;
public $name=null;
public $image=null;
public $brand=null;
public $aggregateRating=null;
public $offers=null;
public $description=null;
public $npm=null;
# массив, который составляет из себя реально существующие пары свойство/значение
# !!! так же тут нету пар с пустым значением
public $constructArray=[];
public function __construct(array $config = [])
{
# обрезаю входящие свойства, работаем только с реально существующими
foreach ($config as $key=>$value){
if(!property_exists(self::class,$key))
{
unset($config[$key]);
}
elseif($value==null || $value===false || $value=='')
{
unset($config[$key]);
}
}
$this->constructArray=$config;
parent::__construct($config);
}
public function createJson()
{
}
public function toJson(): string
{
$resultJson=" <script type=\"application/ld+json\">";
$test=[];
foreach ($this->constructArray as $key=>$value)
{
if($key=='type')$key='@'.$key;
$test[$key]=$value;
}
$resultJson1=json_encode($test,JSON_UNESCAPED_UNICODE);
$resultJson1=str_replace('\/','/',$resultJson1);
$resultJson.=$resultJson1;
$resultJson.='</script>';
return $resultJson;
}
function add($key,$value)
{
if ($value!==null)
$this->constructArray[$key]=$value;
}
public function test()
{
#Для страниц услуг где представлена только одна цена
$productArray=[
'type'=>'Product',
'name'=> 'Executive Anvil',
'image'=> [
'https://example.com/photos/1x1/photo.jpg',
'https://example.com/photos/4x3/photo.jpg',
'https://example.com/photos/16x9/photo.jpg'
],
'description'=>'Sleeker than ACME\'s Classic Anvil, the Executive Anvil is perfect for the business traveler looking for something to drop from a height.',
'mpn'=>'925872',
'brand'=>[
'type'=> 'Thing',
'name'=> 'ACME'
],
'aggregateRating'=>
[
'type'=> 'AggregateRating',
'ratingValue'=> '4.4',
'reviewCount'=> '89'
],
'offers'=> [
'@type'=> 'Offer',
'priceCurrency'=> 'USD',
'Price'=> '199.99',
'priceValidUntil'=> '2020-11-05',
'itemCondition'=> 'http://schema.org/UsedCondition',
'availability'=> 'http://schema.org/InStock',
'seller'=> [
'type'=> 'Organization',
'name'=>'Executive Objects']
]
];
#Для страниц услуг где представлены 2 и больше цен
$layout2=
[
'@context'=> 'http://schema.org/',
'@type'=> 'Product',
'name'=> 'Executive Anvil',
'image'=>
[
'https://example.com/photos/1x1/photo.jpg',
'https://example.com/photos/4x3/photo.jpg',
'https://example.com/photos/16x9/photo.jpg'
],
'brand'=>
[
'@type'=> 'Thing',
'name'=> 'ACME'
],
'aggregateRating'=>
[
'@type'=> 'AggregateRating',
'ratingValue'=> '4.4',
'ratingCount'=> '89'
],
'offers' =>
[
'@type'=> 'AggregateOffer',
'lowPrice'=> '119.99',
'highPrice'=> '199.99',
'priceCurrency'=> 'USD'
]
];
}
}