{"id":22640738,"url":"https://github.com/beastbytes/schema-dot-org-helper","last_synced_at":"2025-03-29T05:26:30.878Z","repository":{"id":65691616,"uuid":"594718136","full_name":"beastbytes/schema-dot-org-helper","owner":"beastbytes","description":"Generate Schema.org JSON-LD","archived":false,"fork":false,"pushed_at":"2023-06-28T14:56:07.000Z","size":95,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-03T15:55:04.129Z","etag":null,"topics":["json-ld","schema-org","yii3"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/beastbytes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-01-29T12:38:20.000Z","updated_at":"2023-01-29T12:40:34.000Z","dependencies_parsed_at":"2025-02-03T15:51:31.038Z","dependency_job_id":"fd5eda93-5a79-43a3-97f7-580a50b5a0c8","html_url":"https://github.com/beastbytes/schema-dot-org-helper","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beastbytes%2Fschema-dot-org-helper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beastbytes%2Fschema-dot-org-helper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beastbytes%2Fschema-dot-org-helper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beastbytes%2Fschema-dot-org-helper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/beastbytes","download_url":"https://codeload.github.com/beastbytes/schema-dot-org-helper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246143397,"owners_count":20730256,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["json-ld","schema-org","yii3"],"created_at":"2024-12-09T04:13:39.726Z","updated_at":"2025-03-29T05:26:30.863Z","avatar_url":"https://github.com/beastbytes.png","language":"PHP","readme":"# Schema.org Helper (schema-dot-org-helper)\nA Helper for generating Schema.org schemas in JSON-LD.\n\n## Installation\n\nThe preferred way to install this extension is through [composer](http://getcomposer.org/download/).\n\nEither run\n\n```\nphp composer.phar require --prefer-dist beastbytes/schemadotorg\n```\n\nor add\n\n```json\n\"beastbytes/schema-dot-org\": \"^2.0\"\n```\n\nto the require section of your composer.json.\n\n## Usage\nTo generate a schema:\n```php\n// In the view\nuse BeastBytes\\SchemaDotOrg\\SchemaDotOrg;\n\n$mapping = [\n    // define mapping\n];\n$model = [\n    // model can be an array or an object\n];\n$schema = SchemaDotOrg::generate($model, $mapping);\n// Multiple schemas can be generated\n```\n\nThe generated schema can be rendered directly\n```php\necho SchemaDotOrg::generate($model, $mapping);\n```\nor registered with the view:\n```php\n$this-\u003eregisterJs(SchemaDotOrg::generate($model, $mapping));\n```\n\n## Defining a Schema Mapping\nA schema mapping is an array that defines the mapping of model properties to Schema.org properties; it is of the form:\n```php\n$mapping = [\n    'Type' =\u003e [\n        'schemaDotOrgProperty' =\u003e 'model.property', // or\n        'model.property' // if the Schema.org and property names are the same\n    ]\n];\n```\n\nWhere a Schema.org property is defined as a Schema.org type, the type is a nested array:\n```php\n[\n    'Type' =\u003e [\n        'schemaDotOrgProperty' =\u003e [\n            'NestedType' =\u003e [\n                // ...\n            ]\n        ]\n    ]\n]\n```\n\nIf a Schema.org property is to be a string literal, prepend with SchemaDotOrg::STRING_LITERAL :\n```php\n[\n    'Type' =\u003e [\n        'schemaDotOrgProperty' =\u003e SchemaDotOrg::STRING_LITERAL . 'Literal value'\n    ]\n]\n```\n\nIf a Schema.org property is a SchemaDotOrg Enumeration value, prepend with SchemaDotOrg::ENUMERATION :\n```php\n[\n    'Type' =\u003e [\n        'schemaDotOrgProperty' =\u003e SchemaDotOrg::ENUMERATION . 'EnumerationName'\n    ]\n]\n```\n\nIf a Schema.org property is an array of values - usually nested types - specify the mapping as an array.\nThe key must be or start with SchemaDotOrg::ARRAY. If it is just SchemaDotOrg::ARRAY the mapping parent key is the\nmodel property, else the model property is the remainder of the key; both forms are shown below:\n```php\n[\n    'EducationalOrganization' =\u003e [\n        'name',\n        'alumni' =\u003e [\n            SchemaDotOrg::ARRAY =\u003e [ // the model property is 'alumni'\n                'Person' =\u003e [\n                    'familyName',\n                    'givenName'\n                ]\n            ]\n        ]\n    ]\n]\n```\n```php\n[\n    'EducationalOrganization' =\u003e [\n        'name',\n        'alumni' =\u003e [\n            SchemaDotOrg::ARRAY . 'pastPupils' =\u003e [ // the model property is 'pastPupils'\n                'Person' =\u003e [\n                    'familyName',\n                    'givenName'\n                ]\n            ]\n        ]\n    ]\n]\n```\n\nExample schema mapping definition:\n```php\n[\n    'LocalBusiness' =\u003e [ // @type always begins with an uppercase letter\n        'name' =\u003e 'org', // maps the 'org' property of the model to the Schema.org 'name' property\n        'address' =\u003e [ // the Schema.org 'address' property is a PostalAddress type\n            'PostalAddress' =\u003e [ // @type\n                'adr.streetAddress', // no need for mapping if the Schema.org and model property names are the same\n                'addressLocality' =\u003e 'adr.locality', // define the mapping if different property names \n                'addressRegion' =\u003e 'adr.region',\n                'adr.postalCode'\n            ]\n        ],\n        'location' =\u003e [\n            'Place' =\u003e [\n                'additionalProperty' =\u003e [\n                    'PropertyValue' =\u003e [\n                        'propertyID' =\u003e SchemaDotOrg::STRING_LITERAL . 'what3words',\n                        'value' =\u003e 'adr.what3words',\n                    ],           \n                ],\n                'latitude',\n                'longitude',\n            ],\n        ],\n        'email',\n        'telephone' =\u003e 'tel.cell.value',\n        'currenciesAccepted' =\u003e SchemaDotOrg::STRING_LITERAL . 'GBP',\n        'image' =\u003e SchemaDotOrg::STRING_LITERAL . 'https://example.com/images/logo.svg',\n        'makesOffer' =\u003e [\n            'Offer' =\u003e [\n                'name',\n                'description',\n                'price',\n                'priceCurrency' =\u003e SchemaDotOrg::STRING_LITERAL . 'GBP',\n                'availability' =\u003e SchemaDotOrg::ENUMERATION . 'InStock'\n            ]\n        ]\n    ]\n]\n```\n\nExample JSON-LD generated using the above schema mapping:\n```html\n\u003cscript type=\"application/ld+json\"\u003e\n{\n  \"@context\": \"https://schema.org\",\n  \"@type\": \"LocalBusiness\",\n  \"name\": \"Business Name\",\n  \"address\": {\n    \"@type\": \"PostalAddress\",\n    \"streetAddress\": \"99 Fencott Road\",\n    addressLocality: \"Fencott\",\n    addressRegion: \"Oxon\",\n    postalCode: \"OX5 2RD\"    \n  }\n  \"location\": {\n    \"@type\": \"Place\",\n    \"additionalProperty\": {\n      \"@type\": \"PropertyValue\",\n      \"propertyID\": \"what3words\",\n      \"value\": \"tangent.migrate.commander\"\n    },\n    \"latitude\": 51.84095049377005,\n    \"longitude\": -1.1709238113995422,\n  },\n  \"email\": \"getintouch@example.com\",\n  \"telephone\": \"01865 369248\",\n  \"currenciesAccepted\": \"GBP\",\n  \"image\": \"https://example.com/images/logo.svg\",\n  \"makesOffer\": {\n    \"@type\": \"Offer\",\n    \"name\": \"Awesome Product\",\n    \"description\": \"The ony product you will ever need\",\n    \"price\": 999.99,\n    \"priceCurrency\": \"GBP\",\n    \"availability\": \"https://schema.org/InStock\"\n  }  \n}\n\u003c/script\u003e\n```\n\n## Twig Templates\n\nTo use the helper in a Twig templates either include it in CommonViewInjection (in the examples it is assigned to the \nschemaDotOrg variable) or in the template\n\n```twig\n{% set schemaDotOrg = get('BeastBytes\\\\SchemaDotOrg\\\\SchemaDotOrg') %}\n```\nThen in the template either:\n\nto echo the schema immediately:\n```twig\n{{ schemaDotOrg.generate(model, mapping) }}\n```\nor to register the schema with the view:\n```twig\n{% do this.registerJs(schemaDotOrg.generate(model, mapping)) %}\n```\n### Defining the Mapping\n\n+ In Twig templates the mapping must define both the SchemaDotOrg property and the model property, even if they have the same name.\n+ To use the SchemaDotOrg class constants use Twig's constant() function and concatenate the string\n\nFor example:\n\n```twig\n{\n    Offer: {\n        name: 'name',\n        description: 'description',\n        price: 'price',\n        priceCurrency: constant('STRING_LITERAL', schemaDotOrg) ~ 'GBP',\n        availability: constant('ENUMERATION', schemaDotOrg) ~ 'InStock'\n    }\n}\n```\n# Testing\nAll testing is carried out from the root directory.\n\n## Unit testing\nThe package is tested with [PHPUnit](https://phpunit.de/). To run tests:\ncomposer test\n\n## Mutation testing\nThe package tests are checked with [Infection mutation framework](https://infection.github.io/) with [Infection Static Analysis Plugin](https://github.com/Roave/infection-static-analysis-plugin). To run it:\ncomposer infection\n\n## Static analysis\nThe code is statically analyzed with [Psalm](https://psalm.dev/). To run static analysis:\ncomposer psalm\n\n# License\nThe Schema.org Helper is free software. It is released under the terms of the BSD License. Please see [LICENSE](./license.md) for more information.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeastbytes%2Fschema-dot-org-helper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbeastbytes%2Fschema-dot-org-helper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeastbytes%2Fschema-dot-org-helper/lists"}