Passed
Push — master ( d878f0...92c484 )
by Stefan
07:20
created

JsonLDEvent::setAddress()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 5
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\JsonLD;
5
6
/**
7
 * Adding structured data to any of your event pages.
8
 *
9
 * ### Avoid marking non-events as events:
10
 *  - Don’t promote non-event products or services such as
11
 *      "Trip package: San Diego/LA, 7 nights" as events.
12
 *  - Don’t add short-term discounts or purchase opportunities, such as:
13
 *      "Concert — buy your tickets now," or "Concert - 50% off until Saturday."
14
 *  - Don’t mark business hours as events, such as:
15
 *      "Adventure park open 8 AM to 5PM."
16
 *  - Don't mark coupons or vouchers as events, such as:
17
 *      "5% off your first order."
18
 *
19
 * ### Mark up multi-day events correctly:
20
 *  - If your event or ticket info is for an event that runs over several
21
 *    days, specify both the start and end dates of the event.
22
 *  - If there are several different performances across different days, each
23
 *    with individual tickets, add a separate Event element for each performance.
24
 *
25
 * ### required properties:
26
 *  - name
27
 *  - startdate
28
 *  - location
29
 *
30
 * ### recomended properties:
31
 *  - enddate
32
 *  - eventAttendanceMode (will be set automaticaly)
33
 *  - eventStatus
34
 *  - image
35
 *  - description
36
 *  - offers
37
 *  - organizer
38
 *  - performer
39
 *
40
 * @link https://developers.google.com/search/docs/data-types/event
41
 * @link https://schema.org/Event
42
 *
43
 * @package JsonLD
44
 * @author Stefanius <[email protected]>
45
 * @copyright MIT License - see the LICENSE file for details
46
 */
47
class JsonLDEvent extends JsonLD
48
{
49
    /** The event is scheduled (default value for the status) */
50
    public const EVENT_SCHEDULED     = 'EventScheduled';
51
    /** The event is cancelled */
52
    public const EVENT_CANCELLED     = 'EventCancelled';
53
    /** The event moved to online event */
54
    public const EVENT_MOVED_ONLINE  = 'EventMovedOnline';
55
    /** The event is postponed */
56
    public const EVENT_POSTPONED     = 'EventPostponed';
57
    /** The event is re-scheduled */
58
    public const EVENT_RESCHEDULED   = 'EventRescheduled';
59
60
    /** Tickets available in stock  */
61
    public const AVAILABLE_IN_STOCK  = 'InStock';
62
    /** No more tickets of this categorie available  */
63
    public const AVAILABLE_SOLD_OUT  = 'SoldOut';
64
    /** Tickets can be pre ordered  */
65
    public const AVAILABLE_PRE_ORDER = 'PreOrder';
66
67
    /** Event is performed/organized by person(s) */
68
    public const ORGANIZATION    = 'Organization';
69
    /** Event is performed/organized by group */
70
    public const GROUP           = 'PerformingGroup';
71
    /** Event is performed/organized by person(s) */
72
    public const PERSON          = 'Person';
73
74
    /**
75
     * Initializes a JsonLD object for article.
76
     */
77
    public function __construct()
78
    {
79
        parent::__construct(self::EVENT, 'Event');
80
        $this->aJsonLD["eventAttendanceMode"] = 'https://schema.org/OfflineEventAttendanceMode';
81
        $this->aJsonLD["eventStatus"] = 'https://schema.org/EventScheduled';
82
    }
83
84
    /**
85
     * Set the main infos for the event.
86
     * @param string $strName
87
     * @param mixed $start      can be string (format YYYY-MM-DD HH:ii:ss), int (unixtimestamp) or DateTime - object
88
     * @param mixed $end        can be string (format YYYY-MM-DD HH:ii:ss), int (unixtimestamp) or DateTime - object
89
     */
90
    public function setInfo(string $strName, $start, $end = null) : void
91
    {
92
        $strName = $this->validString($strName);
93
        if (strlen($strName) > 0) {
94
            $this->aJsonLD["name"] = $strName;
95
            if ($start != null) {
96
                $this->aJsonLD["startDate"] = $this->validDate($start);
97
            }
98
            if ($end != null) {
99
                $this->aJsonLD["endDate"] = $this->validDate($end);
100
            }
101
        }
102
    }
103
104
    /**     * Set postal adress of the business.
105
     * Here it makes sense to enter as many properties as possible. The more you
106
     * specify, the more informative the result will be for users.
107
     *
108
     * @param string $strStreet
109
     * @param string $strPostcode
110
     * @param string $strCity
111
     * @param string $strRegion     (default: '')
112
     * @param string $strCountry    (default: '')
113
     */
114
    public function setAddress(string $strStreet, string $strPostcode, string $strCity, string $strRegion = '', string $strCountry = '') : void
115
    {
116
        $aAddress = $this->buildAddress($strStreet, $strPostcode, $strCity, $strRegion, $strCountry);
117
        if (!isset($this->aJsonLD["location"]) || !is_array($this->aJsonLD["location"])) {
118
            $this->aJsonLD["location"] = array("@type" => "Place");
119
        }
120
        $this->aJsonLD["location"]["address"] = $aAddress;
121
    }
122
123
    /**
124
     * Set the virlual location of the event.
125
     * If the event is happening online, use this method instead of setLocation() / setAdress().
126
     * If an event has a mix of online and physical location components, FIRST call
127
     * setLocation() / setAdress() for the physical component and THEN call setVirtualLocation().
128
     * @param string $strURL
129
     */
130
    public function setVirtualLocation(string $strURL) : void
131
    {
132
        $strURL = $this->validURL($strURL);
133
        if (strlen($strURL) > 0) {
134
            $aVirtualLocation = array('@type' => 'VirtualLocation', 'url' => $strURL);
135
            if (isset($this->aJsonLD["location"]) && is_array($this->aJsonLD["location"])) {
136
                $aLocation = $this->aJsonLD["location"];
137
                $this->aJsonLD["location"] = array();
138
                $this->aJsonLD["location"][] = $aVirtualLocation;
139
                $this->aJsonLD["location"][] = $aLocation;
140
                $this->aJsonLD["eventAttendanceMode"] = 'https://schema.org/MixedEventAttendanceMode';
141
            } else {
142
                $this->aJsonLD["location"] = $aVirtualLocation;
143
                $this->aJsonLD["eventAttendanceMode"] = 'https://schema.org/OnlineEventAttendanceMode';
144
            }
145
        }
146
    }
147
148
    /**
149
     * Set the staus of the event.
150
     * Valid values are one of
151
     * - self::EVENT_SCHEDULED
152
     * - self::EVENT_CANCELLED
153
     * - self::EVENT_MOVED_ONLINE
154
     * - self::EVENT_POSTPONED
155
     * - self::EVENT_RESCHEDULED
156
     * If the event has been canceled or postponed, don't remove or change other
157
     * properties (for example, don't remove startDate or location) instead, keep all
158
     * values as the same as they were before the cancelation, and only update the
159
     * eventStatus.
160
     * If the event has been postponed to a later date, but the date isn't known yet,
161
     * Keep the original date in the startDate of the event until you know when the
162
     * event will take place. Once you know the new date information, change the
163
     * eventStatus to EventRescheduled and update the startDate and endDate with the
164
     * new date information. Optionally, you can also mark the eventStatus field as
165
     * rescheduled and add the previousStartDate.
166
     * @param string $strStatus
167
     * @param mixed $prevstart      can be string (format YYYY-MM-DD HH:ii:ss), int (unixtimestamp) or DateTime - object
168
     */
169
    public function setStatus(string $strStatus, $prevstart = null) : void
170
    {
171
        $aValid = array('EventScheduled', 'EventCancelled', 'EventMovedOnline', 'EventPostponed', 'EventRescheduled');
172
        if (in_array($strStatus, $aValid)) {
173
            $this->aJsonLD["eventStatus"] = 'https://schema.org/' . $strStatus;
174
        }
175
        if ($prevstart != null && $strStatus == 'EventRescheduled') {
176
            $this->aJsonLD["previousStartDate"] = $this->validDate($prevstart);
177
        }
178
    }
179
180
    /**
181
     * Add offer for tickets to the event.
182
     * Valid values for $strAvailable are one of
183
     * - self::AVAILABLE_IN_STOCK
184
     * - self::AVAILABLE_SOLD_OUT
185
     * - self::AVAILABLE_PRE_ORDER
186
     * @param string    $strName          Description or categorie of the ticket
187
     * @param float     $dftPrice         Price (should include service charges and fees)
188
     * @param string    $strCur           The 3-letter currency code.
189
     * @param string    $strAvailable     Availability
190
     * @param mixed     $validFrom        Offer valid from, may be string (format YYYY-MM-DD HH:ii:ss), int (unixtimestamp) or DateTime - object
191
     * @param string    $strURL           landing page that clearly and predominantly provides the opportunity to buy a ticket
192
     */
193
    public function addOffer(string $strName, float $dftPrice, string $strCur, $strAvailable = self::AVAILABLE_IN_STOCK, $validFrom = null, string $strURL = '') : void
194
    {
195
        $aValid = array('InStock', 'SoldOut', 'PreOrder');
196
        if (in_array($strAvailable, $aValid)) {
197
            $strName = $this->validString($strName);
198
            $strCur = strtoupper($this->validString($strCur));
199
            $strURL = $this->validURL($strURL);
200
            if (!isset($this->aJsonLD['offers'])) {
201
                $this->aJsonLD['offers'] = array();
202
            }
203
            $aOffer = array('@type' => 'Offer');
204
            if (strlen($strName) > 0) {
205
                $aOffer['name'] = $strName;
206
            }
207
            if ($dftPrice > 0.0) {
208
                $aOffer['price'] = $dftPrice;
209
            }
210
            if (strlen($strCur) > 0) {
211
                $aOffer['priceCurrency'] = $strCur;
212
            }
213
            if ($validFrom != null) {
214
                $aOffer['validFrom'] = $this->validDate($validFrom);
215
            }
216
            if (strlen($strAvailable) > 0) {
217
                $aOffer['availability'] = 'https://schema.org/' . $strAvailable;
218
            }
219
            if (strlen($strURL) > 0) {
220
                $aOffer['url'] = $strURL;
221
            }
222
            $this->aJsonLD['offers'][] = $aOffer;
223
        }
224
    }
225
226
    /**
227
     * Set organizer of the event.
228
     * Valid values for $strType are one of
229
     * - self::GROUP
230
     * - self::PERSON
231
     * - self::ORGANIZATION
232
     * @param string $strName
233
     * @param string $strURL
234
     * @param string $strType
235
     */
236
    public function setOrganizer(string $strName, string $strURL = '', string $strType = self::ORGANIZATION) : void
237
    {
238
        $aValid = array('PerformingGroup', 'Person', 'Organization');
239
        $strName = $this->validString($strName);
240
        $strURL = $this->validURL($strURL);
241
        if (strlen($strName) > 0 && in_array($strType, $aValid)) {
242
            $this->aJsonLD["organizer"] = array("@type" => $strType);
243
            $this->aJsonLD["organizer"]["name"] = $strName;
244
            if (strlen($strURL) > 0) {
245
                $this->aJsonLD["organizer"]["url"] = $strURL;
246
            }
247
        }
248
    }
249
250
    /**
251
     * Add performer of the event.
252
     * Valid values for $strType are one of
253
     * - self::GROUP
254
     * - self::PERSON
255
     * - self::ORGANIZATION
256
     * @param string $strName
257
     * @param string $strURL
258
     * @param string $strType
259
     */
260
    public function addPerformer(string $strName, string $strURL = '', string $strType = self::GROUP) : void
261
    {
262
        $aValid = array('PerformingGroup', 'Person', 'Organization');
263
        $strName = $this->validString($strName);
264
        $strURL = $this->validURL($strURL);
265
        if (strlen($strName) > 0 && in_array($strType, $aValid)) {
266
            if (!isset($this->aJsonLD["performer"])) {
267
                $this->aJsonLD["performer"] = array();
268
            }
269
            $aPerformer = array("@type" => $strType);
270
            $aPerformer["name"] = $strName;
271
            if (strlen($strURL) > 0) {
272
                $aPerformer["url"] = $strURL;
273
            }
274
            $this->aJsonLD["performer"][] = $aPerformer;
275
        }
276
    }
277
}
278