From 4c7fd21f375d64697b3b8bc64c42f2cc00d89b50 Mon Sep 17 00:00:00 2001 From: Sebastian De Deyne Date: Tue, 3 Jan 2017 16:11:48 +0100 Subject: [PATCH 1/4] Add if function to conditionally modify the schema --- generator/templates/static/BaseType.php | 9 +++++++++ src/BaseType.php | 9 +++++++++ tests/BaseTypeTest.php | 16 ++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/generator/templates/static/BaseType.php b/generator/templates/static/BaseType.php index 06685bac5..373d3712a 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() { From 46efd13a7b26d26e8ffc2b7df2e370e9761690be Mon Sep 17 00:00:00 2001 From: Sebastian De Deyne Date: Tue, 3 Jan 2017 16:18:08 +0100 Subject: [PATCH 2/4] Readme & changelog --- CHANGELOG.md | 3 +++ README.md | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+) 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..5fd177293 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. @@ -87,6 +102,15 @@ $localBusiness->setProperty('foo', 'bar'); If you'd need to retrieve a property, you can use the `getProperty` method. You can optionally pass in a second parameter to provide a default value. +``` php +$localBusiness = Schema::localBusiness()->name('Spatie'); + +// Is equivalent to: + +$localBusiness = new LocalBusiness(); +$localBusiness->name('Spatie'); +``` + ```php $localBusiness->getProperty('name'); // 'Spatie' $localBusiness->getProperty('bar'); // null From bebf8319cc019811cb23140ffa56054fbfd2051e Mon Sep 17 00:00:00 2001 From: Sebastian De Deyne Date: Tue, 3 Jan 2017 16:18:50 +0100 Subject: [PATCH 3/4] Readme --- README.md | 9 --------- 1 file changed, 9 deletions(-) diff --git a/README.md b/README.md index 5fd177293..e7dd2b387 100644 --- a/README.md +++ b/README.md @@ -102,15 +102,6 @@ $localBusiness->setProperty('foo', 'bar'); If you'd need to retrieve a property, you can use the `getProperty` method. You can optionally pass in a second parameter to provide a default value. -``` php -$localBusiness = Schema::localBusiness()->name('Spatie'); - -// Is equivalent to: - -$localBusiness = new LocalBusiness(); -$localBusiness->name('Spatie'); -``` - ```php $localBusiness->getProperty('name'); // 'Spatie' $localBusiness->getProperty('bar'); // null From 93b12dd78458e12347d302c713fbd8700dbcf230 Mon Sep 17 00:00:00 2001 From: Freek Van der Herten Date: Tue, 3 Jan 2017 15:18:57 +0000 Subject: [PATCH 4/4] Apply fixes from StyleCI --- generator/templates/static/BaseType.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generator/templates/static/BaseType.php b/generator/templates/static/BaseType.php index 373d3712a..6a4b43b4c 100644 --- a/generator/templates/static/BaseType.php +++ b/generator/templates/static/BaseType.php @@ -32,7 +32,7 @@ public function if($condition, $callback) if ($condition) { $callback($this); } - + return $this; }