diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d54f32..094312b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -## [0.2.0] - 2022-05-17 +## [0.2.1] - 2023-05-25 +### Fixed +* If a schema.org object has a non string @type, it is ignored and a warning is logged, if the class has a logger. + +## [0.2.0] - 2023-05-17 ### Added * You can now optionally pass a PSR-3 LoggerInterface to the `SchemaOrg` class, so it'll log decoding errors. diff --git a/src/SchemaOrg.php b/src/SchemaOrg.php index fc0a171..d28e3ac 100644 --- a/src/SchemaOrg.php +++ b/src/SchemaOrg.php @@ -83,6 +83,12 @@ private function convertJsonDataToSchemaOrgObject(array $json, bool $isChild = f return null; } + if (!is_string($json['@type'])) { + $this->logger?->warning('Can\'t convert schema.org object with non-string type.'); + + return null; + } + $className = $this->types->getClassName($json['@type']); if (!$className) { diff --git a/tests/SchemaOrgTest.php b/tests/SchemaOrgTest.php index ce7a188..923fc44 100644 --- a/tests/SchemaOrgTest.php +++ b/tests/SchemaOrgTest.php @@ -185,6 +185,31 @@ expect($schemaOrgObjects)->toBeEmpty(); }); +it('returns null if the schema.org object doesn\'t have a distinct type', function () { + $html = << + + + Hello world + + + + + + HTML; + + $schemaOrgObjects = SchemaOrg::fromHtml($html); + + expect($schemaOrgObjects)->toBeEmpty(); +}); + test('you can pass it a PSR-3 LoggerInterface and it will log an error message for invalid JSON string', function () { $scriptBlockContent = <<