Skip to content

Commit

Permalink
add getter method auto resolve option #1645
Browse files Browse the repository at this point in the history
  • Loading branch information
nadar committed Nov 15, 2017
1 parent 7ea5efb commit 473b160
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 40 deletions.
35 changes: 35 additions & 0 deletions core/web/jsonld/BaseThing.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,43 @@
use yii\base\Object;
use yii\base\Arrayable;
use yii\base\ArrayableTrait;
use luya\helpers\StringHelper;

/**
* Base Thing.
*
* Every JsonLD object must implement BaseThing. Therfore BaseThing auto resolves the fields, in
* order to provide the Arrayable::fields() methods.
*
* @author Basil Suter <basil@nadar.io>
*/
abstract class BaseThing extends Object implements Arrayable
{
use ArrayableTrait;

public function resolveGetterMethods()
{
$resolved = [];
$methods = get_class_methods($this);

if (!$methods) {
return [];
}

foreach ($methods as $method) {
if (StringHelper::startsWith($method, 'get', true)) {
$resolved[] = lcfirst(StringHelper::replaceFirst('get', '', $method));
}
}

return $resolved;
}

/**
* @inheritdoc
*/
public function fields()
{
return $this->resolveGetterMethods();
}
}
8 changes: 0 additions & 8 deletions core/web/jsonld/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,4 @@ public function getAddresses()
{
return $this->_addresses;
}

public function fields()
{
return [
'name',
'addresses',
];
}
}
43 changes: 12 additions & 31 deletions tests/core/web/JsonLdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ public function testAssignView()
ob_end_clean();

$this->assertContains('<script type="application/ld+json">{"@graph":[{"foo":"bar"}]}</script>', $out);
JsonLd::reset();
}



public function testBaseThingGetters()
{
$thing = (new Location());
$fields = $thing->resolveGetterMethods();

$this->assertSame(['name', 'addresses'], $fields);
}

public function testJsonLdElementGraphNesting()
Expand Down Expand Up @@ -74,16 +85,6 @@ public function testThing()
'subjectOf' => null,
'url' => null
], $thing->toArray());

JsonLd::reset();
$staticThing = JsonLd::thing()->setName('The Thing');

ob_start();
$this->app->view->beginBody();
$out = ob_get_contents();
ob_end_clean();

$this->assertContains('{"@graph":[{"additionalType":null,"alternateName":null,"description":null,"disambiguatingDescription":null,"identifier":null,"image":null,"mainEntityOfPage":null,"name":"The Thing","potentialAction":null,"sameAs":null,"subjectOf":null,"url":null}]}', $out);
}

public function testOrganization()
Expand Down Expand Up @@ -150,17 +151,7 @@ public function testOrganization()
'subjectOf' => null,
'url' => null
], $thing->toArray());

JsonLd::reset();
$staticThing = JsonLd::organization()->setName('The Organization');

ob_start();
$this->app->view->beginBody();
$out = ob_get_contents();
ob_end_clean();

$this->assertContains('{"@graph":[{"actionableFeedbackPolicy":null,"address":null,"aggregateRating":null,"alumni":null,"areaServed":null,"award":null,"brand":null,"contactPoint":null,"correctionsPolicy":null,"department":null,"dissolutionDate":null,"diversityPolicy":null,"duns":null,"email":null,"employee":null,"ethicsPolicy":null,"event":null,"faxNumber":null,"founder":null,"foundingDate":null,"foundingLocation":null,"funder":null,"globalLocationNumber":null,"hasOfferCatalog":null,"hasPOS":null,"isicV4":null,"legalName":null,"leiCode":null,"location":null,"logo":null,"makesOffer":null,"member":null,"memberOf":null,"naics":null,"numberOfEmployees":null,"owns":null,"parentOrganization":null,"publishingPrinciples":null,"review":null,"seeks":null,"sponsor":null,"subOrganization":null,"taxID":null,"telephone":null,"unnamedSourcesPolicy":null,"vatID":null,"additionalType":null,"alternateName":null,"description":null,"disambiguatingDescription":null,"identifier":null,"image":null,"mainEntityOfPage":null,"name":"The Organization","potentialAction":null,"sameAs":null,"subjectOf":null,"url":null}]}', $out);
}
}

public function testPerson()
{
Expand Down Expand Up @@ -232,16 +223,6 @@ public function testPerson()
'subjectOf' => null,
'url' => null
], $thing->toArray());

JsonLd::reset();
$staticThing = JsonLd::person()->setName('The Person');

ob_start();
$this->app->view->beginBody();
$out = ob_get_contents();
ob_end_clean();

$this->assertContains('{"@graph":[{"additionalName":null,"address":null,"affiliation":null,"alumniOf":null,"award":null,"birthDate":null,"birthPlace":null,"brand":null,"children":null,"colleague":null,"contactPoint":null,"deathDate":null,"deathPlace":null,"duns":null,"email":null,"familyName":null,"faxNumber":null,"follows":null,"funder":null,"gender":null,"givenName":null,"globalLocationNumber":null,"hasOccupation":null,"hasOfferCatalog":null,"hasPOS":null,"height":null,"homeLocation":null,"honorificPrefix":null,"honorificSuffix":null,"isicV4":null,"jobTitle":null,"knows":null,"makesOffer":null,"memberOf":null,"naics":null,"nationality":null,"netWorth":null,"owns":null,"parent":null,"performerIn":null,"publishingPrinciples":null,"relatedTo":null,"seeks":null,"sibling":null,"sponsor":null,"spouse":null,"taxID":null,"telephone":null,"vatID":null,"weight":null,"workLocation":null,"worksFor":null,"additionalType":null,"alternateName":null,"description":null,"disambiguatingDescription":null,"identifier":null,"image":null,"mainEntityOfPage":null,"name":"The Person","potentialAction":null,"sameAs":null,"subjectOf":null,"url":null}]}', $out);
}

/*
Expand Down
1 change: 0 additions & 1 deletion tests/data/configs/web.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

return [
'id' => 'testenv',
'siteTitle' => 'Luya Tests',
Expand Down

0 comments on commit 473b160

Please sign in to comment.