Skip to content

Commit

Permalink
Merge pull request #42 from boly38/feature/repeatingAttributes
Browse files Browse the repository at this point in the history
Fixes #41 an Article with N Comments
  • Loading branch information
Torann authored Oct 12, 2018
2 parents eb7456d + 25f5953 commit a85d7f6
Show file tree
Hide file tree
Showing 14 changed files with 626 additions and 65 deletions.
2 changes: 1 addition & 1 deletion src/ContextTypes/AbstractContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ protected function setProperty($key, $property, $value = null)
}

// Map properties to object
if (is_array($value)) {
if ($property !== null && is_array($property) && is_array($value)) {
return $this->properties[$key] = $this->mapProperty($property, $value);
}

Expand Down
36 changes: 15 additions & 21 deletions src/ContextTypes/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,35 @@

namespace JsonLd\ContextTypes;

class Article extends AbstractContext
class Article extends CreativeWork
{
/**
* Property structure
* reference: https://schema.org/Article (alphabetical order)
*
* @var array
*/
protected $structure = [
'name' => null,
'url' => null,
'description' => null,

'image' => ImageObject::class,
'video' => VideoObject::class,
'thumbnailUrl' => null,
'text' => null,
'review' => Review::class,
'publisher' => Organization::class,
'keywords' => null,
'inLanguage' => null,
'dateCreated' => null,
'dateModified' => null,
'datePublished' => null,
'author' => Person::class,
'aggregateRating' => AggregateRating::class,

private $extendedStructure = [
'articleBody' => null,
'articleSection' => null,
'pageEnd' => null,
'pageStart' => null,
'pagination' => null,
'wordCount' => null,
'mainEntityOfPage' => WebPage::class,
'headline' => null,
];

/**
* Constructor. Merges extendedStructure up
*
* @param array $attributes
* @param array $extendedStructure
*/
public function __construct(array $attributes, array $extendedStructure = [])
{
parent::__construct($attributes, array_merge($this->structure, $this->extendedStructure, $extendedStructure));
}

/**
* Set the description attribute.
*
Expand Down
30 changes: 30 additions & 0 deletions src/ContextTypes/Comment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace JsonLd\ContextTypes;

class Comment extends CreativeWork
{
/**
* Property structure.
* reference: https://schema.org/Comment (alphabetical order)
*
* @var array
*/
private $extendedStructure = [
'downvoteCount' => null,
'upvoteCount' => null,
];

/**
* Constructor. Merges extendedStructure up
*
* @param array $attributes
* @param array $extendedStructure
*/
public function __construct(array $attributes, array $extendedStructure = [])
{
parent::__construct(
$attributes, array_merge($this->structure, $this->extendedStructure, $extendedStructure)
);
}
}
46 changes: 40 additions & 6 deletions src/ContextTypes/CreativeWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,49 @@

namespace JsonLd\ContextTypes;

class CreativeWork extends AbstractContext
class CreativeWork extends Thing
{
/**
* Property structure
* reference: https://schema.org/CreativeWork (alphabetical order)
*
* @var array
*/
protected $structure = [
'name' => null,
'url' => null,
private $extendedStructure = [
'about' => Thing::class,
'aggregateRating' => AggregateRating::class,
'alternativeHeadline' => null,
'author' => Person::class,
'comment' => Comment::class,
'commentCount' => null,
'creator' => Person::class,
'dateCreated' => null,
'dateModified' => null,
'datePublished' => null,
'headline' => null,
'image' => ImageObject::class,
'inLanguage' => null,
'keywords' => null,
'mainEntity' => Thing::class,
'publisher' => Organization::class,
'review' => Review::class,
'text' => null,
'thumbnailUrl' => null,
'video' => VideoObject::class,
];

/**
* Constructor. Merges extendedStructure up
*
* @param array $attributes
* @param array $extendedStructure
*/
public function __construct(array $attributes, array $extendedStructure = [])
{
parent::__construct(
$attributes, array_merge($this->structure, $this->extendedStructure, $extendedStructure)
);
}

/**
* Set the article body attribute.
*
Expand All @@ -38,4 +55,21 @@ protected function setTextAttribute($txt)
{
return $this->truncate($txt, 260);
}
}

/**
* Set the comments
*
* @param array $items
* @return array
*/
protected function setCommentAttribute($items)
{
if (is_array($items) === false) {
return $items;
}

return array_map(function ($item) {
return $this->getNestedContext(Comment::class, $item);
}, $items);
}
}
58 changes: 54 additions & 4 deletions src/ContextTypes/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,64 @@

namespace JsonLd\ContextTypes;

class Person extends AbstractContext
class Person extends Thing
{
/**
* Property structure
* reference: https://schema.org/Person (alphabetical order)
*
* @var array
*/
protected $structure = [
'name' => null,
protected $extendedStructure = [
'additionalName' => null,
'address' => null, // PostalAddress or Text
'affiliation' => null,
'alumniOf' => null,
'award' => null,
'birthDate' => null,
'birthPlace' => Place::class,
'brand' => null,
'children' => Person::class,
'colleague' => null,
'contactPoint' => null,
'deathDate' => null,
'deathPlace' => Place::class,
'duns' => null,
'email' => null,
'familyName' => null,
'faxNumber' => null,
'follows' => Person::class,
'homeLocation' => Place::class,
'givenName' => null,
'jobTitle' => null,
'parent' => Person::class,
'telephone' => null,
'workLocation' => Place::class,
];
}

/**
* Constructor. Merges extendedStructure up
*
* @param array $attributes
* @param array $extendedStructure
*/
public function __construct(array $attributes, array $extendedStructure = [])
{
parent::__construct($attributes, array_merge($this->structure, $this->extendedStructure, $extendedStructure));
}

/**
* Set the address
*
* @param array $items
* @return array
*/
protected function setAddressAttribute($items)
{
if (is_array($items) === false) {
return $items;
}

return $this->getNestedContext(PostalAddress::class, $items);
}
}
18 changes: 15 additions & 3 deletions src/ContextTypes/Place.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,29 @@

namespace JsonLd\ContextTypes;

class Place extends AbstractContext
class Place extends Thing
{
/**
* Property structure
*
* @var array
*/
protected $structure = [
'name' => null,
protected $extendedStructure = [
'address' => PostalAddress::class,
'review' => Review::class,
'aggregateRating' => AggregateRating::class,
];


/**
* Constructor. Merges extendedStructure up
*
* @param array $attributes
* @param array $extendedStructure
*/
public function __construct(array $attributes, array $extendedStructure = [])
{
parent::__construct($attributes, array_merge($this->structure, $this->extendedStructure, $extendedStructure));
}

}
26 changes: 26 additions & 0 deletions src/ContextTypes/Sculpture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace JsonLd\ContextTypes;

class Sculpture extends CreativeWork
{
/**
* Property structure.
*
* @var array
*/
private $extendedStructure = [];

/**
* Constructor. Merges extendedStructure up
*
* @param array $attributes
* @param array $extendedStructure
*/
public function __construct(array $attributes, array $extendedStructure = [])
{
parent::__construct(
$attributes, array_merge($this->structure, $this->extendedStructure, $extendedStructure)
);
}
}
9 changes: 6 additions & 3 deletions src/ContextTypes/Thing.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ class Thing extends AbstractContext
{
/**
* Property structure
* reference: https://schema.org/Thing (alphabetical order)
*
* @var array
*/
protected $structure = [
'name' => null,
'alternateName' => null,
'description' => null,
'image' => null,
'image' => ImageObject::class,
'mainEntityOfPage' => WebPage::class,
'name' => null,
'sameAs' => null,
'url' => null,
];

Expand All @@ -40,4 +43,4 @@ protected function setTypeAttribute($type)
// TODO: Add type validation
return $type;
}
}
}
Loading

0 comments on commit a85d7f6

Please sign in to comment.