Skip to content

Commit

Permalink
spatie#156 - render identifier instead of @id for typed identification
Browse files Browse the repository at this point in the history
spatie#156 - Update pull request number and add extra check


spatie#156 - update pull request
  • Loading branch information
JohJohan committed Dec 26, 2020
1 parent 3b22822 commit dfb51b5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ $localBusiness = Schema::localBusiness()

### Identifier

As of v2.6.0 the `identifier` key is replaced by `@id`. This is due to the definition for the `ld+json` syntax.
As of v2.6.0 the `identifier` key is replaced by `@id` for simple string identifiers. This is due to the definition for the `ld+json` syntax.

> All schema.org syntaxes already have built-in representation for URIs and URLs, e.g. in Microdata 'itemid', in RDFa 1.1, 'resource', **in JSON-LD, '@id'.**
>
> — [schema.org/docs](https://schema.org/docs/datamodel.html#identifierBg) // [PR#102](https://github.com/spatie/schema-org/pull/102)
> — [schema.org/docs](https://schema.org/docs/datamodel.html#identifierBg) // [PR#102](https://github.com/spatie/schema-org/pull/102) // [PR#157](https://github.com/spatie/schema-org/pull/157)
### Advanced Usage

Expand Down
2 changes: 1 addition & 1 deletion generator/templates/static/BaseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ protected function serializeProperty($property)

protected function serializeIdentifier()
{
if (isset($this['identifier'])) {
if (isset($this['identifier']) && !$this['identifier'] instanceof Type ) {
$this->setProperty('@id', $this['identifier']);
unset($this['identifier']);
}
Expand Down
2 changes: 1 addition & 1 deletion src/BaseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ protected function serializeProperty($property)

protected function serializeIdentifier()
{
if (isset($this['identifier'])) {
if (isset($this['identifier']) && !$this['identifier'] instanceof Type ) {
$this->setProperty('@id', $this['identifier']);
unset($this['identifier']);
}
Expand Down
22 changes: 22 additions & 0 deletions tests/BaseTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use PHPUnit\Framework\TestCase;
use Spatie\SchemaOrg\BaseType;
use Spatie\SchemaOrg\Exceptions\InvalidProperty;
use Spatie\SchemaOrg\Product;
use Spatie\SchemaOrg\PropertyValue;

class BaseTypeTest extends TestCase
{
Expand Down Expand Up @@ -325,6 +327,26 @@ public function it_replaces_identifier_with_at_id_property()
$this->assertEquals($expected, $type->toArray());
}

/** @test */
public function it_can_render_identifier_for_typed_identifiers()
{
$productType = new Product();
$propertyValue = new PropertyValue();
$propertyValue->identifier('#1');
$productType->setProperty('identifier', $propertyValue);

$expected = [
'@context' => 'https://schema.org',
'@type' => 'Product',
'identifier' => [
"@type" => "PropertyValue",
"@id" => "#1",
],
];

$this->assertEquals($expected, $productType->toArray());
}

/** @test */
public function it_can_reference_type_by_identifier()
{
Expand Down

0 comments on commit dfb51b5

Please sign in to comment.