JsonLDEvent::setOrganizer()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 3
nop 3
dl 0
loc 10
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
    /** constants for Event status */
50
    public const __STATUS = '';
51
    /** The event is scheduled (default value for the status) */
52
    public const EVENT_SCHEDULED     = 'EventScheduled';
53
    /** The event is cancelled */
54
    public const EVENT_CANCELLED     = 'EventCancelled';
55
    /** The event moved to online event */
56
    public const EVENT_MOVED_ONLINE  = 'EventMovedOnline';
57
    /** The event is postponed */
58
    public const EVENT_POSTPONED     = 'EventPostponed';
59
    /** The event is re-scheduled */
60
    public const EVENT_RESCHEDULED   = 'EventRescheduled';
61
62
    /** constants for availability */
63
    public const __AVAILABILITY = '';
64
    /** Tickets available in stock  */
65
    public const AVAILABLE_IN_STOCK  = 'InStock';
66
    /** No more tickets of this categorie available  */
67
    public const AVAILABLE_SOLD_OUT  = 'SoldOut';
68
    /** Tickets can be pre ordered  */
69
    public const AVAILABLE_PRE_ORDER = 'PreOrder';
70
71
    /** constants for performer/organizer */
72
    public const __PERFORMER = '';
73
    /** Event is performed/organized by organization */
74
    public const ORGANIZATION    = 'Organization';
75
    /** Event is performed/organized by group */
76
    public const GROUP           = 'PerformingGroup';
77
    /** Event is performed/organized by person(s) */
78
    public const PERSON          = 'Person';
79
80
    /**
81
     * Initializes a JsonLD object for article.
82
     */
83
    public function __construct()
84
    {
85
        parent::__construct(self::EVENT, 'Event');
86
        $this->aJsonLD["eventAttendanceMode"] = 'https://schema.org/OfflineEventAttendanceMode';
87
        $this->aJsonLD["eventStatus"] = 'https://schema.org/EventScheduled';
88
    }
89
90
    /**
91
     * Set the main infos for the event.
92
     * @param string $strName
93
     * @param mixed $start      can be string (format YYYY-MM-DD HH:ii:ss), int (unixtimestamp) or DateTime - object
94
     * @param mixed $end        can be string (format YYYY-MM-DD HH:ii:ss), int (unixtimestamp) or DateTime - object
95
     */
96
    public function setInfo(string $strName, $start, $end = null) : void
97
    {
98
        $strName = $this->validString($strName);
99
        if (strlen($strName) > 0) {
100
            $this->aJsonLD["name"] = $strName;
101
            if ($start != null) {
102
                $this->aJsonLD["startDate"] = $this->validDate($start);
103
            }
104
            if ($end != null) {
105
                $this->aJsonLD["endDate"] = $this->validDate($end);
106
            }
107
        }
108
    }
109
110
    /**     * Set postal adress of the business.
111
     * Here it makes sense to enter as many properties as possible. The more you
112
     * specify, the more informative the result will be for users.
113
     *
114
     * @param string $strStreet
115
     * @param string $strPostcode
116
     * @param string $strCity
117
     * @param string $strRegion     (default: '')
118
     * @param string $strCountry    (default: '')
119
     */
120
    public function setAddress(string $strStreet, string $strPostcode, string $strCity, string $strRegion = '', string $strCountry = '') : void
121
    {
122
        $aAddress = $this->buildAddress($strStreet, $strPostcode, $strCity, $strRegion, $strCountry);
123
        if (!isset($this->aJsonLD["location"]) || !is_array($this->aJsonLD["location"])) {
124
            $this->aJsonLD["location"] = array("@type" => "Place");
125
        }
126
        $this->aJsonLD["location"]["address"] = $aAddress;
127
    }
128
129
    /**
130
     * Set the virlual location of the event.
131
     * If the event is happening online, use this method instead of setLocation() / setAdress().
132
     * If an event has a mix of online and physical location components, FIRST call
133
     * setLocation() / setAdress() for the physical component and THEN call setVirtualLocation().
134
     * @param string $strURL
135
     */
136
    public function setVirtualLocation(string $strURL) : void
137
    {
138
        $strURL = $this->validURL($strURL);
139
        if (strlen($strURL) > 0) {
140
            $aVirtualLocation = array('@type' => 'VirtualLocation', 'url' => $strURL);
141
            if (isset($this->aJsonLD["location"]) && is_array($this->aJsonLD["location"])) {
142
                $aLocation = $this->aJsonLD["location"];
143
                $this->aJsonLD["location"] = array();
144
                $this->aJsonLD["location"][] = $aVirtualLocation;
145
                $this->aJsonLD["location"][] = $aLocation;
146
                $this->aJsonLD["eventAttendanceMode"] = 'https://schema.org/MixedEventAttendanceMode';
147
            } else {
148
                $this->aJsonLD["location"] = $aVirtualLocation;
149
                $this->aJsonLD["eventAttendanceMode"] = 'https://schema.org/OnlineEventAttendanceMode';
150
            }
151
        }
152
    }
153
154
    /**
155
     * Set the staus of the event.
156
     * Valid values are one of
157
     * - self::EVENT_SCHEDULED
158
     * - self::EVENT_CANCELLED
159
     * - self::EVENT_MOVED_ONLINE
160
     * - self::EVENT_POSTPONED
161
     * - self::EVENT_RESCHEDULED
162
     *
163
     * If the event has been canceled or postponed, don't remove or change other
164
     * properties (for example, don't remove startDate or location) instead, keep all
165
     * values as the same as they were before the cancelation, and only update the
166
     * eventStatus.
167
     * If the event has been postponed to a later date, but the date isn't known yet,
168
     * Keep the original date in the startDate of the event until you know when the
169
     * event will take place. Once you know the new date information, change the
170
     * eventStatus to EventRescheduled and update the startDate and endDate with the
171
     * new date information. Optionally, you can also mark the eventStatus field as
172
     * rescheduled and add the previousStartDate.
173
     * @param string $strStatus
174
     * @param mixed $prevstart      can be string (format YYYY-MM-DD HH:ii:ss), int (unixtimestamp) or DateTime - object
175
     */
176
    public function setStatus(string $strStatus, $prevstart = null) : void
177
    {
178
        $aValid = array('EventScheduled', 'EventCancelled', 'EventMovedOnline', 'EventPostponed', 'EventRescheduled');
179
        if (in_array($strStatus, $aValid)) {
180
            $this->aJsonLD["eventStatus"] = 'https://schema.org/' . $strStatus;
181
        }
182
        if ($prevstart != null && $strStatus == 'EventRescheduled') {
183
            $this->aJsonLD["previousStartDate"] = $this->validDate($prevstart);
184
        }
185
    }
186
187
    /**
188
     * Add offer for tickets to the event.
189
     * Valid values for $strAvailable are one of
190
     * - self::AVAILABLE_IN_STOCK
191
     * - self::AVAILABLE_SOLD_OUT
192
     * - self::AVAILABLE_PRE_ORDER
193
     * @param string    $strName          Description or categorie of the ticket
194
     * @param float     $dftPrice         Price (should include service charges and fees)
195
     * @param string    $strCur           The 3-letter currency code.
196
     * @param string    $strAvailable     Availability
197
     * @param mixed     $validFrom        Offer valid from, may be string (format YYYY-MM-DD HH:ii:ss), int (unixtimestamp) or DateTime - object
198
     * @param string    $strURL           landing page that clearly and predominantly provides the opportunity to buy a ticket
199
     */
200
    public function addOffer(string $strName, float $dftPrice, string $strCur, $strAvailable = self::AVAILABLE_IN_STOCK, $validFrom = null, string $strURL = '') : void
201
    {
202
        $aValid = array('InStock', 'SoldOut', 'PreOrder');
203
        if (in_array($strAvailable, $aValid)) {
204
            $strName = $this->validString($strName);
205
            $strCur = strtoupper($this->validString($strCur));
206
            $strURL = $this->validURL($strURL);
207
            if (!isset($this->aJsonLD['offers'])) {
208
                $this->aJsonLD['offers'] = array();
209
            }
210
            $aOffer = array('@type' => 'Offer');
211
            if (strlen($strName) > 0) {
212
                $aOffer['name'] = $strName;
213
            }
214
            if ($dftPrice > 0.0) {
215
                $aOffer['price'] = $dftPrice;
216
            }
217
            if (strlen($strCur) > 0) {
218
                $aOffer['priceCurrency'] = $strCur;
219
            }
220
            if ($validFrom != null) {
221
                $aOffer['validFrom'] = $this->validDate($validFrom);
222
            }
223
            if (strlen($strAvailable) > 0) {
224
                $aOffer['availability'] = 'https://schema.org/' . $strAvailable;
225
            }
226
            if (strlen($strURL) > 0) {
227
                $aOffer['url'] = $strURL;
228
            }
229
            $this->aJsonLD['offers'][] = $aOffer;
230
        }
231
    }
232
233
    /**
234
     * Set organizer of the event.
235
     * Valid values for $strType are one of
236
     * - self::GROUP
237
     * - self::PERSON
238
     * - self::ORGANIZATION
239
     * @param string $strName
240
     * @param string $strURL
241
     * @param string $strType
242
     */
243
    public function setOrganizer(string $strName, string $strURL = '', string $strType = self::ORGANIZATION) : void
244
    {
245
        $aValid = array('PerformingGroup', 'Person', 'Organization');
246
        $strName = $this->validString($strName);
247
        $strURL = $this->validURL($strURL);
248
        if (strlen($strName) > 0 && in_array($strType, $aValid)) {
249
            $this->aJsonLD["organizer"] = array("@type" => $strType);
250
            $this->aJsonLD["organizer"]["name"] = $strName;
251
            if (strlen($strURL) > 0) {
252
                $this->aJsonLD["organizer"]["url"] = $strURL;
253
            }
254
        }
255
    }
256
257
    /**
258
     * Add performer of the event.
259
     * Valid values for $strType are one of
260
     * - self::GROUP
261
     * - self::PERSON
262
     * - self::ORGANIZATION
263
     * @param string $strName
264
     * @param string $strURL
265
     * @param string $strType
266
     */
267
    public function addPerformer(string $strName, string $strURL = '', string $strType = self::GROUP) : void
268
    {
269
        $aValid = array('PerformingGroup', 'Person', 'Organization');
270
        $strName = $this->validString($strName);
271
        $strURL = $this->validURL($strURL);
272
        if (strlen($strName) > 0 && in_array($strType, $aValid)) {
273
            if (!isset($this->aJsonLD["performer"])) {
274
                $this->aJsonLD["performer"] = array();
275
            }
276
            $aPerformer = array("@type" => $strType);
277
            $aPerformer["name"] = $strName;
278
            if (strlen($strURL) > 0) {
279
                $aPerformer["url"] = $strURL;
280
            }
281
            $this->aJsonLD["performer"][] = $aPerformer;
282
        }
283
    }
284
}
285