| @@ 9-73 (lines=65) @@ | ||
| 6 | use Spatie\SchemaOrg\BaseType; |
|
| 7 | use Spatie\SchemaOrg\Exceptions\InvalidType; |
|
| 8 | ||
| 9 | class LdJson |
|
| 10 | { |
|
| 11 | public static function fromJson(string $json): BaseType |
|
| 12 | { |
|
| 13 | $data = json_decode($json, true); |
|
| 14 | ||
| 15 | return static::fromArray($data); |
|
| 16 | } |
|
| 17 | ||
| 18 | public static function fromArray(array $data): BaseType |
|
| 19 | { |
|
| 20 | if (! isset($data['@context'])) { |
|
| 21 | throw new InvalidArgumentException('No context given in schema.'); |
|
| 22 | } |
|
| 23 | ||
| 24 | if (strpos($data['@context'], 'schema.org') === false) { |
|
| 25 | throw new InvalidArgumentException('The given @context is not valid.'); |
|
| 26 | } |
|
| 27 | ||
| 28 | unset($data['@context']); |
|
| 29 | ||
| 30 | return static::generateType($data); |
|
| 31 | } |
|
| 32 | ||
| 33 | protected static function generateType(array $data): BaseType |
|
| 34 | { |
|
| 35 | if (! isset($data['@type'])) { |
|
| 36 | throw new InvalidType('No type given in schema.'); |
|
| 37 | } |
|
| 38 | ||
| 39 | $fqcn = '\\Spatie\\SchemaOrg\\'.$data['@type']; |
|
| 40 | unset($data['@type']); |
|
| 41 | if (! static::isValidType($fqcn)) { |
|
| 42 | throw new InvalidType(sprintf('The given type "%s" is not an instance of "%s".', $fqcn, BaseType::class)); |
|
| 43 | } |
|
| 44 | /** @var BaseType $type */ |
|
| 45 | $type = new $fqcn(); |
|
| 46 | ||
| 47 | foreach ($data as $property => $value) { |
|
| 48 | if (is_array($value)) { |
|
| 49 | if (isset($value['@type'])) { |
|
| 50 | $type->setProperty($property, static::generateType($value)); |
|
| 51 | continue; |
|
| 52 | } |
|
| 53 | ||
| 54 | $type->setProperty($property, array_map(function ($value) { |
|
| 55 | if (is_array($value) && isset($value['@type'])) { |
|
| 56 | return static::generateType($value); |
|
| 57 | } |
|
| 58 | ||
| 59 | return $value; |
|
| 60 | }, $value)); |
|
| 61 | continue; |
|
| 62 | } |
|
| 63 | ||
| 64 | $type->setProperty($property, $value); |
|
| 65 | } |
|
| 66 | ||
| 67 | return $type; |
|
| 68 | } |
|
| 69 | ||
| 70 | protected static function isValidType(string $fqcn): bool |
|
| 71 | { |
|
| 72 | return class_exists($fqcn) && is_a($fqcn, BaseType::class); |
|
| 73 | } |
|
| 74 | } |
|
| 75 | ||
| @@ 9-73 (lines=65) @@ | ||
| 6 | use Spatie\SchemaOrg\BaseType; |
|
| 7 | use Spatie\SchemaOrg\Exceptions\InvalidType; |
|
| 8 | ||
| 9 | class LdJson |
|
| 10 | { |
|
| 11 | public static function fromJson(string $json): BaseType |
|
| 12 | { |
|
| 13 | $data = json_decode($json, true); |
|
| 14 | ||
| 15 | return static::fromArray($data); |
|
| 16 | } |
|
| 17 | ||
| 18 | public static function fromArray(array $data): BaseType |
|
| 19 | { |
|
| 20 | if (!isset($data['@context'])) { |
|
| 21 | throw new InvalidArgumentException('No context given in schema.'); |
|
| 22 | } |
|
| 23 | ||
| 24 | if (strpos($data['@context'], 'schema.org') === false) { |
|
| 25 | throw new InvalidArgumentException('The given @context is not valid.'); |
|
| 26 | } |
|
| 27 | ||
| 28 | unset($data['@context']); |
|
| 29 | return static::generateType($data); |
|
| 30 | } |
|
| 31 | ||
| 32 | protected static function generateType(array $data): BaseType |
|
| 33 | { |
|
| 34 | if (!isset($data['@type'])) { |
|
| 35 | throw new InvalidType('No type given in schema.'); |
|
| 36 | } |
|
| 37 | ||
| 38 | $fqcn = '\\Spatie\\SchemaOrg\\' . $data['@type']; |
|
| 39 | unset($data['@type']); |
|
| 40 | if (!static::isValidType($fqcn)) { |
|
| 41 | throw new InvalidType(sprintf('The given type "%s" is not an instance of "%s".', $fqcn, BaseType::class)); |
|
| 42 | } |
|
| 43 | /** @var BaseType $type */ |
|
| 44 | $type = new $fqcn(); |
|
| 45 | ||
| 46 | foreach ($data as $property => $value) { |
|
| 47 | if(is_array($value)) { |
|
| 48 | if(isset($value['@type'])) { |
|
| 49 | $type->setProperty($property, static::generateType($value)); |
|
| 50 | continue; |
|
| 51 | } |
|
| 52 | ||
| 53 | $type->setProperty($property, array_map(function ($value) { |
|
| 54 | if(is_array($value) && isset($value['@type'])) { |
|
| 55 | return static::generateType($value); |
|
| 56 | } |
|
| 57 | ||
| 58 | return $value; |
|
| 59 | }, $value)); |
|
| 60 | continue; |
|
| 61 | } |
|
| 62 | ||
| 63 | $type->setProperty($property, $value); |
|
| 64 | } |
|
| 65 | ||
| 66 | return $type; |
|
| 67 | } |
|
| 68 | ||
| 69 | protected static function isValidType(string $fqcn): bool |
|
| 70 | { |
|
| 71 | return class_exists($fqcn) && is_a($fqcn, BaseType::class); |
|
| 72 | } |
|
| 73 | } |
|
| 74 | ||