Skip to content

Commit fb248f2

Browse files
committedDec 7, 2016
Optional properties fix
1 parent 5001371 commit fb248f2

7 files changed

+42
-114
lines changed
 

‎generate.php

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env php
12
<?php
23

34
if (file_exists(__DIR__.'/vendor/autoload.php')) {

‎generator/Parser/RdfaParser.php

+5
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ protected function parseProperties()
6262
$property = new Property();
6363

6464
$property->name = $this->getText($node, '[property="rdfs:label"]');
65+
66+
if (empty($property->name)) {
67+
return;
68+
}
69+
6570
$property->description = $this->getText($node, '[property="rdfs:comment"]');
6671
$property->resource = $this->getAttribute($node, 'resource');
6772

‎src/BaseType.php

+13-30
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Spatie\SchemaOrg;
44

5-
use DateTimeInterface;
65
use ReflectionClass;
76
use Spatie\SchemaOrg\Exceptions\InvalidProperty;
87

@@ -40,43 +39,27 @@ public function getProperties(): array
4039

4140
public function toArray(): array
4241
{
43-
$properties = $this->serialize($this->getProperties());
42+
$properties = array_map(function ($property) {
43+
if ($property instanceof Type) {
44+
$property = $property->toArray();
45+
unset($property['@context']);
46+
}
47+
48+
if (is_object($property)) {
49+
throw new InvalidProperty();
50+
}
51+
52+
return $property;
53+
}, $this->getProperties());
4454

4555
return [
4656
'@context' => $this->getContext(),
4757
'@type' => $this->getType(),
4858
] + $properties;
4959
}
5060

51-
protected function serialize($property)
52-
{
53-
if (is_array($property)) {
54-
return array_map(function ($property) {
55-
return $this->serialize($property);
56-
}, $property);
57-
}
58-
59-
if ($property instanceof Type) {
60-
$property = $property->toArray();
61-
unset($property['@context']);
62-
return $property;
63-
}
64-
65-
if ($property instanceof DateTimeInterface) {
66-
return $property->format('c');
67-
}
68-
69-
if (is_object($property)) {
70-
throw new InvalidProperty();
71-
}
72-
73-
return $property;
74-
}
75-
7661
public function toScript(): string
7762
{
78-
return '<script type="application/ld+json">'.
79-
json_encode($this->toArray()).
80-
'</script>';
63+
return '<script type="application/ld+json">'.json_encode($this->toArray()).'</script>';
8164
}
8265
}

‎src/DatedMoneySpecification.php

-28
Original file line numberDiff line numberDiff line change
@@ -41,32 +41,4 @@ public function currency($currency)
4141
return $this->setProperty('currency', $currency);
4242
}
4343

44-
/**
45-
*
46-
*
47-
* @param \DateTimeInterface $
48-
*
49-
* @return static
50-
*
51-
* @see http://schema.org/startDate
52-
*/
53-
public function ($)
54-
{
55-
return $this->setProperty('', $);
56-
}
57-
58-
/**
59-
*
60-
*
61-
* @param \DateTimeInterface $
62-
*
63-
* @return static
64-
*
65-
* @see http://schema.org/endDate
66-
*/
67-
public function ($)
68-
{
69-
return $this->setProperty('', $);
70-
}
71-
7244
}

‎src/MusicRelease.php

-14
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,6 @@ public function creditedTo($creditedTo)
3939
return $this->setProperty('creditedTo', $creditedTo);
4040
}
4141

42-
/**
43-
*
44-
*
45-
* @param $
46-
*
47-
* @return static
48-
*
49-
* @see http://schema.org/duration
50-
*/
51-
public function ($)
52-
{
53-
return $this->setProperty('', $);
54-
}
55-
5642
/**
5743
* Format of this release (the type of recording media used, ie. compact
5844
* disc, digital media, LP, etc.).

‎src/VisualArtwork.php

-42
Original file line numberDiff line numberDiff line change
@@ -70,48 +70,6 @@ public function artworkSurface($artworkSurface)
7070
return $this->setProperty('artworkSurface', $artworkSurface);
7171
}
7272

73-
/**
74-
*
75-
*
76-
* @param $
77-
*
78-
* @return static
79-
*
80-
* @see http://schema.org/width
81-
*/
82-
public function ($)
83-
{
84-
return $this->setProperty('', $);
85-
}
86-
87-
/**
88-
*
89-
*
90-
* @param $
91-
*
92-
* @return static
93-
*
94-
* @see http://schema.org/height
95-
*/
96-
public function ($)
97-
{
98-
return $this->setProperty('', $);
99-
}
100-
101-
/**
102-
*
103-
*
104-
* @param $
105-
*
106-
* @return static
107-
*
108-
* @see http://schema.org/depth
109-
*/
110-
public function ($)
111-
{
112-
return $this->setProperty('', $);
113-
}
114-
11573
/**
11674
* The number of copies when multiple copies of a piece of artwork are
11775
* produced - e.g. for a limited edition of 20 prints, 'artEdition' refers

‎tests/data/schema-excerpt.rdfa

+23
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@
5959
<span>Subclass of: <a property="rdfs:subClassOf" href="http://schema.org/WebPage">WebPage</a></span>
6060
</div>
6161

62+
<div typeof="rdfs:Class" resource="http://schema.org/MusicPlaylist">
63+
<span class="h" property="rdfs:label">MusicPlaylist</span>
64+
<span property="rdfs:comment">A collection of music tracks in playlist form.</span>
65+
<span>Subclass of: <a property="rdfs:subClassOf" href="http://schema.org/CreativeWork">CreativeWork</a></span>
66+
</div>
67+
68+
<div typeof="rdfs:Class" resource="http://schema.org/MusicRelease">
69+
<span class="h" property="rdfs:label">MusicRelease</span>
70+
<span property="rdfs:comment">A MusicRelease is a specific release of a music album.</span>
71+
<link property="dc:source" href="http://www.w3.org/wiki/WebSchemas/SchemaDotOrgSources#MBZ"/>
72+
<span>Subclass of: <a property="rdfs:subClassOf" href="http://schema.org/MusicPlaylist">MusicPlaylist</a></span>
73+
</div>
74+
6275
<div typeof="rdf:Property" resource="http://schema.org/schemaVersion">
6376
<span class="h" property="rdfs:label">schemaVersion</span>
6477
<span property="rdfs:comment">Indicates (by URL or string) a particular version of a schema used in some CreativeWork. For example, a document could declare a schemaVersion using an URL such as http://schema.org/version/2.0/ if precise indication of schema version was required by some application. </span>
@@ -73,5 +86,15 @@
7386
<span>Domain: <a property="http://schema.org/domainIncludes" href="http://schema.org/CreativeWork">CreativeWork</a></span>
7487
<span>Range: <a property="http://schema.org/rangeIncludes" href="http://schema.org/Thing">Thing</a></span>
7588
</div>
89+
90+
<div typeof="rdf:Property" resource="http://schema.org/duration">
91+
<span class="h" property="rdfs:label">duration</span>
92+
<span property="rdfs:comment">The duration of the item (movie, audio recording, event, etc.) in [ISO 8601 date format](http://en.wikipedia.org/wiki/ISO_8601).</span>
93+
<span>Domain: <a property="http://schema.org/domainIncludes" href="http://schema.org/MediaObject">MediaObject</a></span>
94+
<span>Domain: <a property="http://schema.org/domainIncludes" href="http://schema.org/Event">Event</a></span>
95+
<span>Domain: <a property="http://schema.org/domainIncludes" href="http://schema.org/Movie">Movie</a></span>
96+
<span>Domain: <a property="http://schema.org/domainIncludes" href="http://schema.org/MusicRecording">MusicRecording</a></span>
97+
<span>Range: <a property="http://schema.org/rangeIncludes" href="http://schema.org/Duration">Duration</a></span>
98+
</div>
7699
</body>
77100
</html>

0 commit comments

Comments
 (0)
Please sign in to comment.