-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
JsonLdProvider.php
162 lines (147 loc) · 4.81 KB
/
JsonLdProvider.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
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
<?php
/**
* YAWIK
*
* @filesource
* @license MIT
* @copyright 2013 - 2017 Cross Solution <http://cross-solution.de>
*/
/** */
namespace Jobs\Entity\Decorator;
use Doctrine\Common\Collections\Collection;
use Jobs\Entity\JobInterface;
use Jobs\Entity\JsonLdProviderInterface;
use Jobs\Entity\TemplateValuesInterface;
use Zend\Json\Json;
/**
* Decorates a job with implementing a toJsonLd method.
*
* This decorator *does not* delegate other methods.
*
* @author Mathias Gelhausen <gelhausen@cross-solution.de>
* @author Carsten Bleek <bleek@cross-solution.de>
* @todo write test
*/
class JsonLdProvider implements JsonLdProviderInterface
{
/**
* the decorated job entity.
*
* @var \Jobs\Entity\JobInterface|\Jobs\Entity\Job
*/
private $job;
/**
* @param JobInterface $job
*/
public function __construct(JobInterface $job)
{
$this->job = $job;
}
public function toJsonLd()
{
$organization = $this->job->getOrganization();
$organizationName = $organization ? $organization->getOrganizationName()->getName() : $this->job->getCompany();
$dateStart = $this->job->getDatePublishStart();
$dateStart = $dateStart ? $dateStart->format('Y-m-d H:i:s') : null;
$dateEnd = $this->job->getDatePublishEnd();
$dateEnd = $dateEnd ? $dateEnd->format('Y-m-d H:i:s') : null;
if (!$dateEnd) {
$dateEnd = new \DateTime($dateStart);
$dateEnd->add(new \DateInterval("P180D"));
$dateEnd = $dateEnd->format('Y-m-d H:i:s');
}
$array=[
'@context'=>'http://schema.org/',
'@type' => 'JobPosting',
'title' => $this->job->getTitle(),
'description' => $this->getDescription($this->job->getTemplateValues()),
'datePosted' => $dateStart,
'identifier' => [
'@type' => 'PropertyValue',
'value' => $this->job->getApplyId(),
'name' => $organizationName,
],
'hiringOrganization' => [
'@type' => 'Organization',
'name' => $organizationName,
// @TODO add the link to the Logo of the company
// https://developers.google.com/search/docs/data-types/logo
// 'logo' => $this->job->getOrganization()->getImage()->getUri(),
],
'jobLocation' => $this->getLocations($this->job->getLocations()),
'employmentType' => $this->job->getClassifications()->getEmploymentTypes()->getValues(),
'validThrough' => $dateEnd
];
$array += $this->generateSalary();
return Json::encode($array);
}
/**
* Generates a location array
*
* @param Collection $locations,
*
* @return array
*/
private function getLocations($locations)
{
$array=[];
foreach ($locations as $location) { /* @var \Core\Entity\LocationInterface $location */
array_push(
$array,
[
'@type' => 'Place',
'address' => [
'@type' => 'PostalAddress',
'streetAddress' => $location->getStreetname() .' '.$location->getStreetnumber(),
'postalCode' => $location->getPostalCode(),
'addressLocality' => $location->getCity(),
'addressCountry' => $location->getCountry(),
'addressRegion' => $location->getRegion(),
]
]
);
}
return $array;
}
/**
* Generates a description from template values
*
* @param TemplateValuesInterface $values
*
* @return string
*/
private function getDescription(TemplateValuesInterface $values)
{
$description=sprintf(
"<p>%s</p>".
"<h1>%s</h1>".
"<h3>Requirements</h3><p>%s</p>".
"<h3>Qualifications</h3><p>%s</p>".
"<h3>Benefits</h3><p>%s</p>",
$values->getDescription(),
$values->getTitle(),
$values->getRequirements(),
$values->getQualifications(),
$values->getBenefits()
);
return $description;
}
private function generateSalary()
{
$salary = $this->job->getSalary();
if (!$salary || null === $salary->getValue()) {
return [];
}
return [
'baseSalary' => [
'@type' => 'MonetaryAmount',
'currency' => $salary->getCurrency(),
'value' => [
'@type' => 'QuantitiveValue',
'value' => $salary->getValue(),
'unitText' => $salary->getUnit()
],
],
];
}
}