Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Create new auto inherit structure for context attributes ($structure)…
…. Attributes in the $structure property are now automatically merged with all parent classes
  • Loading branch information
madman-81 committed Dec 8, 2021
commit bf5d9963ca16194bc0f7dfb80a0d685d776eab8f
21 changes: 6 additions & 15 deletions src/ContextTypes/AbstractContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,6 @@ abstract class AbstractContext implements ContextTypeInterface
'name' => '',
];

/**
* Property structure
*
* @var array
*/
protected $extendStructure = [];

/**
* Property structure, will be merged up for objects extending Thing
*
* @var array
*/
private $extendedStructure = [];

/**
* Create a new context type instance
*
Expand All @@ -56,6 +42,11 @@ public function __construct(array $attributes = [])
$path = explode('\\', get_class($this));
$this->type = end($path);

$class = get_called_class();
while ($class = get_parent_class($class)) {
$this->structure += get_class_vars($class)['structure'];
}

// Set attributes
$this->fill($attributes);
}
Expand Down Expand Up @@ -89,7 +80,7 @@ public function fill(array $attributes)
'@context' => 'http://schema.org',
'@type' => $this->type,
'sameAs' => null
], $this->structure, $this->extendStructure);
], $this->structure);

// Set properties from attributes
foreach ($properties as $key => $property) {
Expand Down
13 changes: 1 addition & 12 deletions src/ContextTypes/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Article extends CreativeWork
*
* @var array
*/
private $extendedStructure = [
protected $structure = [
'articleBody' => null,
'articleSection' => null,
'pageEnd' => null,
Expand All @@ -22,17 +22,6 @@ class Article extends CreativeWork
'mainEntityOfPage' => WebPage::class,
];

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

/**
* {@inheritDoc}
*/
Expand Down
12 changes: 1 addition & 11 deletions src/ContextTypes/Audiobook.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,11 @@ class Audiobook extends Book
*
* @var array
*/
protected $extendedStructure = [
protected $structure = [
'caption' => MediaObject::class,
'transcript' => null,
'duration' => Duration::class,
'readBy' => Person::class,
];

/**
* @param array $attributes
* @param array $extendedStructure
*/
public function __construct(array $attributes, array $extendedStructure = [])
{
parent::__construct(
$attributes, array_merge($this->structure, $this->extendedStructure, $extendedStructure)
);
}
}
2 changes: 1 addition & 1 deletion src/ContextTypes/BlogPosting.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class BlogPosting extends Article
*
* @var array
*/
protected $extendStructure = [
protected $structure = [
'sharedContent' => CreativeWork::class,
];
}
12 changes: 1 addition & 11 deletions src/ContextTypes/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Book extends CreativeWork
*
* @var array
*/
protected $extendedStructure = [
protected $structure = [
'abridged' => null,
'bookEdition' => null,
'bookFormat' => BookFormatType::class,
Expand All @@ -24,14 +24,4 @@ class Book extends CreativeWork
'numberOfPages' => null,
];

/**
* @param array $attributes
* @param array $extendedStructure
*/
public function __construct(array $attributes, array $extendedStructure = [])
{
parent::__construct(
$attributes, array_merge($this->structure, $this->extendedStructure, $extendedStructure)
);
}
}
13 changes: 1 addition & 12 deletions src/ContextTypes/BookFormatType.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,5 @@ class BookFormatType extends Enumeration
*
* @var array
*/
protected $extendedStructure = [];

/**
* @param array $attributes
* @param array $extendedStructure
*/
public function __construct(array $attributes, array $extendedStructure = [])
{
parent::__construct(
$attributes, array_merge($this->structure, $this->extendedStructure, $extendedStructure)
);
}
protected $structure = [];
}
12 changes: 1 addition & 11 deletions src/ContextTypes/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,9 @@ class Comment extends CreativeWork
*
* @var array
*/
private $extendedStructure = [
protected $structure = [
'downvoteCount' => null,
'upvoteCount' => null,
];

/**
* @param array $attributes
* @param array $extendedStructure
*/
public function __construct(array $attributes, array $extendedStructure = [])
{
parent::__construct(
$attributes, array_merge($this->structure, $this->extendedStructure, $extendedStructure)
);
}
}
14 changes: 1 addition & 13 deletions src/ContextTypes/Corporation.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,8 @@ class Corporation extends Organization
*
* @var array
*/
private $extendedStructure = [
protected $structure = [
'tickerSymbol' => null,
];

/**
* Corporation 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)
);
}
}
13 changes: 1 addition & 12 deletions src/ContextTypes/CreativeWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CreativeWork extends Thing
*
* @var array
*/
private $extendedStructure = [
protected $structure = [
'about' => Thing::class,
'aggregateRating' => AggregateRating::class,
'alternativeHeadline' => null,
Expand All @@ -35,17 +35,6 @@ class CreativeWork extends Thing
'video' => VideoObject::class,
];

/**
* @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 Down
12 changes: 1 addition & 11 deletions src/ContextTypes/Enumeration.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,8 @@ class Enumeration extends Thing
*
* @var array
*/
protected $extendedStructure = [
protected $structure = [
'supersededBy' => Enumeration::class,
];

/**
* @param array $attributes
* @param array $extendedStructure
*/
public function __construct(array $attributes, array $extendedStructure = [])
{
parent::__construct(
$attributes, array_merge($this->structure, $this->extendedStructure, $extendedStructure)
);
}
}
13 changes: 1 addition & 12 deletions src/ContextTypes/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Event extends Thing
*
* @var array
*/
protected $extendedStructure = [
protected $structure = [
'name' => null,
'startDate' => null,
'endDate' => null,
Expand All @@ -18,17 +18,6 @@ class Event extends Thing
'location' => Place::class,
];

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

/**
* Set offers attributes.
*
Expand Down
12 changes: 1 addition & 11 deletions src/ContextTypes/MediaObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class MediaObject extends CreativeWork
*
* @var array
*/
protected $extendedStructure = [
protected $structure = [
'associatedArticle' => NewsArticle::class,
'bitrate' => null,
'contentSize' => null,
Expand All @@ -40,14 +40,4 @@ class MediaObject extends CreativeWork
'width' => QuantitativeValue::class,
];

/**
* @param array $attributes
* @param array $extendedStructure
*/
public function __construct(array $attributes, array $extendedStructure = [])
{
parent::__construct(
$attributes, array_merge($this->structure, $this->extendedStructure, $extendedStructure)
);
}
}
13 changes: 1 addition & 12 deletions src/ContextTypes/Organization.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,14 @@ class Organization extends Thing
*
* @var array
*/
private $extendedStructure = [
protected $structure = [
'address' => PostalAddress::class,
'logo' => ImageObject::class,
'contactPoint' => ContactPoint::class,
'email' => null,
'hasPOS' => Place::class,
];

/**
* @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 contactPoints
*
Expand Down
11 changes: 1 addition & 10 deletions src/ContextTypes/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Person extends Thing
*
* @var array
*/
protected $extendedStructure = [
protected $structure = [
'additionalName' => null,
'address' => null, // PostalAddress or Text
'affiliation' => null,
Expand All @@ -39,15 +39,6 @@ class Person extends Thing
'workLocation' => Place::class,
];

/**
* @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
*
Expand Down
10 changes: 1 addition & 9 deletions src/ContextTypes/Place.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,10 @@ class Place extends Thing
*
* @var array
*/
protected $extendedStructure = [
protected $structure = [
'address' => PostalAddress::class,
'review' => Review::class,
'aggregateRating' => AggregateRating::class,
];

/**
* @param array $attributes
* @param array $extendedStructure
*/
public function __construct(array $attributes, array $extendedStructure = [])
{
parent::__construct($attributes, array_merge($this->structure, $this->extendedStructure, $extendedStructure));
}
}
14 changes: 1 addition & 13 deletions src/ContextTypes/Sculpture.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,6 @@ class Sculpture extends CreativeWork
*
* @var array
*/
private $extendedStructure = [];
protected $structure = [];

/**
* 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)
);
}
}
Loading