-
Notifications
You must be signed in to change notification settings - Fork 4
/
JsonLDHelper.php
118 lines (109 loc) · 3.7 KB
/
JsonLDHelper.php
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
<?php
/**
* @link https://github.com/nirvana-msu/yii2-jsonld-helper
* @link https://github.com/simialbi/yii2-schema-org
* @copyright Copyright (c) 2016 Alexander Stepanov
* @copyright Copyright (c) 2017 Simon Karlen
* @license MIT
*/
namespace simialbi\yii2\schemaorg\helpers;
use simialbi\yii2\schemaorg\models\Model;
use Yii;
use yii\base\InvalidArgumentException;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\helpers\Url;
/**
* Helper class for registering structured data markup in JSON-LD format
*
* @author Alexander Stepanov <student_vmk@mail.ru>
* @author Simon Karlen <simi.albi@outlook.com>
* @author Mehdi Achour <machour@gmail.com>
*/
class JsonLDHelper
{
/**
* @var Model[] Keeps schema org models wich will be displayed on page
*/
private static $_models = [];
/**
* Adds BreadcrumbList schema.org markup based on the application view `breadcrumbs` parameter
* @throws \yii\base\InvalidConfigException
*/
public static function addBreadCrumbList()
{
if (!class_exists('\simialbi\yii2\schemaorg\models\BreadcrumbList')) {
return;
}
$view = Yii::$app->view;
/* @var $breadcrumbList Model */
$breadcrumbs = ArrayHelper::getValue($view->params, 'breadcrumbs', []);
$breadcrumbList = Yii::createObject([
'class' => 'simialbi\yii2\schemaorg\models\BreadcrumbList'
]);
if (!empty($breadcrumbs)) {
$position = 1;
foreach ($breadcrumbs as $breadcrumb) {
$listItem = Yii::createObject([
'class' => 'simialbi\yii2\schemaorg\models\ListItem',
'position' => $position++
]);
if (is_array($breadcrumb)) {
$listItem->item = [
'@id' => Url::to(ArrayHelper::getValue($breadcrumb, 'url', ''), true),
'name' => ArrayHelper::getValue($breadcrumb, 'label', '')
];
} else {
$listItem->item = [
'@id' => Yii::$app->request->absoluteUrl,
'name' => $breadcrumb
];
}
$breadcrumbList->itemListElement[] = $listItem;
}
}
self::add($breadcrumbList);
}
/**
* Add model to json+ld rendering queue
*
* @param Model $model
*/
public static function add(Model $model)
{
self::$_models[] = $model;
}
/**
* Renders ld+json data
*
* @param boolean $merge
*/
public static function render(bool $merge = false)
{
if (!empty(self::$_models)) {
if ($merge) {
$models = array_map(function ($item) {
return $item->toJsonLDArray();
}, self::$_models);
try {
echo Html::script(Json::encode($models), ['type' => 'application/ld+json']) . "\n";
} catch (InvalidArgumentException $e) {
$logger = Yii::$app->log->logger;
$logger->log($e->getMessage(), $logger::LEVEL_ERROR);
}
self::$_models = [];
return;
}
foreach (self::$_models as $model) {
try {
echo Html::script(Json::encode($model->toJsonLDArray()), ['type' => 'application/ld+json']) . "\n";
} catch (InvalidArgumentException $e) {
$logger = Yii::$app->log->logger;
$logger->log($e->getMessage(), $logger::LEVEL_ERROR);
}
}
self::$_models = [];
}
}
}