diff --git a/CHANGELOG.md b/CHANGELOG.md index 41a4423cb..5c9aff008 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 4e6487ae1..e7dd2b387 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/generator/templates/static/BaseType.php b/generator/templates/static/BaseType.php index 06685bac5..6a4b43b4c 100644 --- a/generator/templates/static/BaseType.php +++ b/generator/templates/static/BaseType.php @@ -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; diff --git a/src/BaseType.php b/src/BaseType.php index 040152d3c..dcebe193a 100644 --- a/src/BaseType.php +++ b/src/BaseType.php @@ -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; diff --git a/tests/BaseTypeTest.php b/tests/BaseTypeTest.php index c3310c4d3..7b0da2dd8 100644 --- a/tests/BaseTypeTest.php +++ b/tests/BaseTypeTest.php @@ -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() {