-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy paththing.php
More file actions
177 lines (161 loc) · 4.52 KB
/
thing.php
File metadata and controls
177 lines (161 loc) · 4.52 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
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/**
* @author Chris Zuber <chris@chriszuber.com>
* @package superuserdev/schemaserver
* @copyright 2017
* @license https://opensource.org/licenses/LGPL-3.0 GNU Lesser General Public License version 3
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
*/
namespace SuperUserDev\SchemaServer;
/**
* @see https://schema.org/Thing
*/
class Thing implements \JsonSerializable, \Serializable, \Iterator,
Interfaces\Base, Interfaces\Database
{
use Traits\Data;
use Traits\Database;
Use Traits\Magic;
use Traits\Serial;
use Traits\Iterator;
use Traits\Filters;
use Traits\Parse;
const CONTEXT = 'http://schema.org';
const CONTENT_TYPE = 'application/json';
const JSON = '/^\s*\{.*\}\s*$/';
/**
* [protected description]
* @var Array
*/
protected $_data = [];
/**
* [__construct description]
* @param array $data [description]
*/
final public function __construct(Array $data = [])
{
if (array_key_exists('@type', $data)) {
if ($data['@type'] !== static::getType() and false) {
throw new \Exception("Invalid type: '{$data['@type']}'");
}
unset($data['@type'], $data['@context']);
if (array_key_exists('@id', $data)) {
return $this->_set('identifier', $data['@id']);
}
foreach ($data as $key => $value) {
try {
if (is_array($value)) {
if (array_key_exists('@type', $value)) {
$this->{$key} = static::parseFromArray($value);
} else {
throw new \Error('Unable to create object of unknown @type');
}
} else {
if (is_string($value) and preg_match(self::JSON, $value)) {
$this->{$key} = static::parseFromJSON($value);
}
$this->{$key} = $value;
}
} catch (\Throwable $e) {
echo $e->getMessage() . PHP_EOL;
}
}
}
}
/**
* [setAdditionalType description]
* @param String $url [description]
*/
final public function setAdditionalType(String $url): self
{
if (static::_isURL($url)) {
return $this->_set('additionalType', $url);
}
}
/**
* [setAlternateName description]
* @param String $name [description]
*/
final public function setAlternateName(String $name): self
{
return $this->_set('alternamteName', $name);
}
/**
* [setDescription description]
* @param String $description [description]
*/
final public function setDescription(String $description): self
{
return $this->_set('description', $description);
}
final public function setDisambiguatingDescription(String $description): self
{
return $this->_set('disambiguatingDescription', $description);
}
/**
* [setImage description]
* @param ImageObject $image [description]
*/
final public function setImage(ImageObject $image): self
{
return $this->_set('image', $image);
}
final public function setMainEntityOfPage(CreativeWork $entity): self
{
return $this->_set('mainEntityOfPage', $entity);
}
/**
* [setName description]
* @param String $name [description]
*/
final public function setName(String $name): self
{
return $this->_set('name', $name);
}
final public function setPOtentialAction(Action $action): self
{
return $this->_set('potentialAction', $action);
}
/**
* [setSameAs description]
* @param String $url [description]
*/
final public function setSameAs(String $url): self
{
if (static::_isURL($url)) {
return $this->_set('sameAs', $url);
} else {
throw new \InvalidArgumentException("'{$url}' is not a valid url");
}
}
/**
* [setUrl description]
* @param String $url [description]
*/
final public function setUrl(String $url): self
{
if (static::_isURL($url)) {
return $this->_set('url', $url);
} else {
throw new \InvalidArgumentException("'{$url}' is not a valid url");
}
}
final public function setIdentifier(String $id): self
{
return $this->_set('identifier', $id);
$scheme = array_key_exists('HTTPS', $_SERVER) and $_SERVER['HTTPS'] ? 'http' : 'https';
$domain = $_SERVER['HTTP_HOST'] ?? 'localhost';
return $this->_set('@id', "{$scheme}://{$domain}/{$this::getType()}/{$id}");
}
}