Skip to content

Commit

Permalink
Merge pull request #13 from spatie/if-function
Browse files Browse the repository at this point in the history
Add if function to conditionally modify the schema
  • Loading branch information
sebastiandedeyne authored Jan 3, 2017
2 parents 3deaf0f + 57ecb35 commit da16687
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

All Notable changes to `schema-org` will be documented in this file

# 1.1.0 - 2017-01-03
- Added: `if` function to conditionally modify the schema

# 1.0.1 - 2017-01-03
- Fix: Arrays of properties are now correctly serialized when converted to ld+json

Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ echo $localBusiness; // Same output as `toScript()`

There's no full API documentation for types and properties. You can refer to [the source](https://github.com/spatie/schema-org/tree/master/src) or to [the schema.org website](http://schema.org).

If you don't want to break the chain of a large schema object, you can use the `if` method to conditionally modify the schema.

```php
use Spatie\SchemaOrg\LocalBusiness;
use Spatie\SchemaOrg\Schema;

$business = ['name' => 'Spatie'];

$localBusiness = Schema::localBusiness()
->name($business['name'])
->if(isset($business['email']), function (LocalBusiness $schema) {
$schema->email($business['email']);
});
```

### Advanced usage

If you'd need to set a custom property, you can use the `setProperty` method.
Expand Down
9 changes: 9 additions & 0 deletions generator/templates/static/BaseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ public function setProperty(string $property, $value)
return $this;
}

public function if($condition, $callback)
{
if ($condition) {
$callback($this);
}

return $this;
}

public function getProperty(string $property, $default = null)
{
return $this->properties[$property] ?? $default;
Expand Down
9 changes: 9 additions & 0 deletions src/BaseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ public function setProperty(string $property, $value)
return $this;
}

public function if($condition, $callback)
{
if ($condition) {
$callback($this);
}

return $this;
}

public function getProperty(string $property, $default = null)
{
return $this->properties[$property] ?? $default;
Expand Down
16 changes: 16 additions & 0 deletions tests/BaseTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ public function it_can_set_and_retrieve_a_property()
$this->assertEquals(['foo' => 'bar'], $type->getProperties());
}

/** @test */
public function it_can_conditionally_set_and_retrieve_a_property()
{
$type = new DummyType();

$type->if(true, function (DummyType $type) {
$type->setProperty('foo', 'bar');
});

$type->if(false, function (DummyType $type) {
$type->setProperty('baz', 'qux');
});

$this->assertEquals(['foo' => 'bar'], $type->getProperties());
}

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

0 comments on commit da16687

Please sign in to comment.