Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restructure context types and add AggregateOffer #71

Open
wants to merge 6 commits into
base: 1.0.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
}
],
"require": {
"php": "^7.2"
"php": "^7.2",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^8.0",
Expand Down
5 changes: 5 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@
<directory>./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
90 changes: 60 additions & 30 deletions src/ContextTypes/AbstractContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,9 @@ abstract class AbstractContext implements ContextTypeInterface
* @var array
*/
protected $structure = [
'name' => '',
'name' => null,
];

/**
* 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,12 +80,12 @@ 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) {
$this->setProperty(
$key, $property, $this->getArrValue($attributes, $key, '')
$this->properties[$key] = $this->makeProperty(
$key, $property, $this->getArrValue($attributes, $key,'')
);
}

Expand Down Expand Up @@ -148,41 +139,50 @@ public function getProperty(string $key, $default = null)
*
* @return mixed
*/
protected function setProperty(string $key, $property, $value = null)
protected function makeProperty(string $key, $property, $value = null)
{
// Can't be changed
if ($key[0] === '@') {
return $this->properties[$key] = $property;
return $property;
}

// If the attribute has a get mutator, we will call that
// then return what it returns as the value.
if ($this->hasGetMutator($key)) {
return $this->properties[$key] = $this->mutateAttribute($key, $value);
return $this->mutateAttribute($key, $value);
}

// Format date and time to UTC
if ($value instanceof DateTime) {
return $this->properties[$key] = $value->format('Y-m-d\TH:i:s');
return $value->format('Y-m-d\TH:i:s');
}

// Set nested context
if ($value instanceof Context) {
return $this->properties[$key] = $this->filterNestedContext($value->getProperties());
return $this->filterNestedContext($value->getProperties());
}

// Set nested context from class
if ($property && class_exists($property)) {
return $this->properties[$key] = $this->getNestedContext($property, $value);
if (is_array($value) && $this->hasValidContext($value, $property)) {
return $this->makeContext($value);
}

// Map properties to object
if ($property !== null && is_array($property) && is_array($value)) {
return $this->properties[$key] = $this->mapProperty($property, $value);
// Set nested context from class
if (is_array($value) && is_string($property) && class_exists($property)) {
// Check if it is an array with one dimension
if (is_array(reset($value)) === false) {
$nested_context = $this->getNestedContext($property, $value);
} else {
// Process multi dimensional array
$nested_context = array_map(function ($item) use ($property) {
return $this->getNestedContext($property, $item);
}, $value);
}

return $nested_context;
}

// Set value
return $this->properties[$key] = $value;
return $value;
}

/**
Expand Down Expand Up @@ -343,4 +343,34 @@ protected function getArrValue(array $array, string $key, $default = null)
? $array[$key]
: $default;
}

/**
* Check if the values array has a key '@type' and if that contains an existing context
*
* @param array $value
* @param string|array $property
*
* @retrun bool
*/
protected function hasValidContext(array $value, $property)
{
if (array_key_exists('@type', $value)) {
$class_name = __NAMESPACE__ .'\\'. $value['@type'];

if (!is_array($property)) {
$property = [$property];
}

return (in_array($class_name, $property) && class_exists($class_name));
}

return false;
}

protected function makeContext(array $value)
{
$property = __NAMESPACE__ .'\\'. $value['@type'];

return $this->getNestedContext($property, $value);
}
}
18 changes: 18 additions & 0 deletions src/ContextTypes/AggregateOffer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace JsonLd\ContextTypes;

class AggregateOffer extends Offer
{
/**
* Property structure
*
* @var array
*/
protected $structure = [
'highPrice' => null,
'lowPrice' => null,
'offerCount' => null,
];

}
8 changes: 4 additions & 4 deletions src/ContextTypes/AggregateRating.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace JsonLd\ContextTypes;

class AggregateRating extends AbstractContext
/**
* https://schema.org/AggregateRating
*/
class AggregateRating extends Rating
{
/**
* Property structure
Expand All @@ -11,9 +14,6 @@ class AggregateRating extends AbstractContext
*/
protected $structure = [
'reviewCount' => null,
'ratingValue' => null,
'bestRating' => null,
'worstRating' => null,
'ratingCount' => null,
'itemReviewed' => Thing::class,
];
Expand Down
14 changes: 1 addition & 13 deletions src/ContextTypes/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,15 @@ class Article extends CreativeWork
*
* @var array
*/
private $extendedStructure = [
protected $structure = [
'articleBody' => null,
'articleSection' => null,
'pageEnd' => null,
'pageStart' => null,
'pagination' => null,
'wordCount' => null,
'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
20 changes: 20 additions & 0 deletions src/ContextTypes/AudioObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace JsonLd\ContextTypes;

/**
* https://schema.org/AudioObject
*/
class AudioObject extends MediaObject
{
/**
* Property structure
*
* @var array
*/
protected $structure = [
'caption' => MediaObject::class,
'transcript' => null,
];

}
18 changes: 3 additions & 15 deletions src/ContextTypes/Audiobook.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,18 @@
namespace JsonLd\ContextTypes;

/**
* https://schema.org/BookFormatType
* https://schema.org/Audiobook
*/
class Audiobook extends Book
class Audiobook extends AudioObject
{
/**
* Property structure
*
* @var array
*/
protected $extendedStructure = [
'caption' => MediaObject::class,
'transcript' => null,
protected $structure = [
'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)
);
}
}
17 changes: 5 additions & 12 deletions src/ContextTypes/Beach.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,15 @@

namespace JsonLd\ContextTypes;

class Beach extends AbstractContext
/**
* https://schema.org/Beach
*/
class Beach extends CivicStructure
{
/**
* Property structure
*
* @var array
*/
protected $structure = [
'name' => null,
'openingHours' => null,
'description' => null,
'image' => null,
'url' => null,
'address' => PostalAddress::class,
'geo' => GeoCoordinates::class,
'review' => Review::class,
'aggregateRating' => AggregateRating::class,
];
protected $structure = [];
}
9 changes: 5 additions & 4 deletions src/ContextTypes/BlogPosting.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

namespace JsonLd\ContextTypes;

class BlogPosting extends Article
/**
* https://schema.org/BlogPosting
*/
class BlogPosting extends SocialMediaPosting
{
/**
* Property structure
*
* @var array
*/
protected $extendStructure = [
'sharedContent' => CreativeWork::class,
];
protected $structure = [];
}
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 = [];
}
Loading