| Total Complexity | 7 |
| Total Lines | 49 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 9 | trait SchemaOrg |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Automatically called to add a schema to the Page |
||
| 13 | */ |
||
| 14 | protected function initSchemaOrg(): void |
||
| 15 | { |
||
| 16 | if (method_exists($this, 'schemaOrg')) { |
||
| 17 | if ($this instanceof Page) { |
||
| 18 | $page = $this; |
||
| 19 | } else { |
||
| 20 | $page = $this->page(); |
||
|
|
|||
| 21 | } |
||
| 22 | |||
| 23 | if ($page) { |
||
| 24 | $this->add($page); |
||
| 25 | } |
||
| 26 | } |
||
| 27 | |||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Returns the JavaScript JSON-LD string |
||
| 32 | * |
||
| 33 | * @return string |
||
| 34 | */ |
||
| 35 | public function schemaOrgScript(): string |
||
| 36 | { |
||
| 37 | $schemaJson = ''; |
||
| 38 | |||
| 39 | foreach ($this->meta()['schema_org'] as $schema) { |
||
| 40 | $schemaJson .= $schema->toScript(); |
||
| 41 | } |
||
| 42 | |||
| 43 | return $schemaJson; |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Adds the schema to the meta of the current page |
||
| 48 | * |
||
| 49 | * @param $page |
||
| 50 | */ |
||
| 51 | private function add($page): void |
||
| 58 | } |
||
| 59 | } |