hungcrush / schema-org
Schema.org类型和ld+json生成器的流畅构建器
Requires
- php: ^7.3 || ^8.0
- ext-json: *
Requires (Dev)
- graham-campbell/analyzer: ^3.0.4
- illuminate/collections: ^8.16.1
- league/flysystem: ^1.1.3
- phpunit/phpunit: ^9.4.3
- symfony/console: ^3.4.47
- twig/twig: ^1.44.1
- dev-master
- 3.5.1
- 3.5.0
- 3.4.0
- 3.3.0
- 3.2.1
- 3.2.0
- 3.1.1
- 3.1.0
- 3.0.0
- 2.16.0
- 2.15.0
- 2.14.1
- 2.14.0
- 2.13.0
- 2.12.1
- 2.12.0
- 2.11.1
- 2.11.0
- 2.10.0
- 2.9.0
- 2.8.0
- 2.7.0
- 2.6.0
- 2.5.0
- 2.4.0
- 2.3.0
- 2.2.1
- 2.2.0
- 2.1.0
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.7.0
- 1.6.0
- 1.5.0
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.1
- 1.3.0
- 1.2.1
- 1.2.0
- 1.1.0
- 1.0.1
- 1.0.0
- dev-ft-auto-release
- dev-extensible-generator
- dev-fix-phpunit
This package is auto-updated.
Last update: 2024-09-09 23:55:15 UTC
README
spatie/schema-org
提供了一个用于所有Schema.org类型及其属性的流畅构建器。在src
中的代码是从Schema.org的JSON-LD标准文件生成的,因此它提供了整个核心词汇表的对象和方法。类和方法也被完全文档化,作为快速参考。
use Spatie\SchemaOrg\Schema; $localBusiness = Schema::localBusiness() ->name('Spatie') ->email('info@spatie.be') ->contactPoint(Schema::contactPoint()->areaServed('Worldwide')); echo $localBusiness->toScript();
<script type="application/ld+json"> { "@context": "https:\/\/schema.org", "@type": "LocalBusiness", "name": "Spatie", "email": "info@spatie.be", "contactPoint": { "@type": "ContactPoint", "areaServed": "Worldwide" } } </script>
支持我们
我们投入了大量资源来创建最优质的开放源代码包。您可以通过购买我们的付费产品之一来支持我们。
我们非常感谢您从您的家乡寄来明信片,说明您正在使用我们哪个包。您可以在我们的联系页面上找到我们的地址。我们将所有收到的明信片发布在我们的虚拟明信片墙上。
安装
您可以通过composer安装此包
composer require spatie/schema-org
使用
所有类型都可以通过Spatie\SchemaOrg\Schema
工厂类或使用new
关键字进行实例化。
$localBusiness = Schema::localBusiness()->name('Spatie'); // Is equivalent to: $localBusiness = new LocalBusiness(); $localBusiness->name('Spatie');
所有类型也接受预期数据类型的数组,例如
sameAs
接受字符串或字符串数组。
所有类型也实现了SPL的ArrayAccess
,可以通过数组表示法访问属性
$anotherLocalBusiness = new LocalBusiness(); var_dump(isset($anotherLocalBusiness['name'])); // => false $anotherLocalBusiness['name'] = 'Spatie'; var_dump(isset($anotherLocalBusiness['name'])); // => true var_dump($anotherLocalBusiness['name']); // => 'Spatie' unset($anotherLocalBusiness['name']); var_dump(isset($anotherLocalBusiness['name'])); // => false
类型可以被转换为数组或渲染为脚本。
$localBusiness->toArray(); echo $localBusiness->toScript(); echo $localBusiness; // Same output as `toScript()`
此外,所有类型都可以通过调用json_encode()
将对象转换为普通JSON字符串
echo json_encode($localBusiness);
我建议您使用Google的structured data测试工具检查您的结构化数据。
枚举
从v1.6.0版本开始,所有枚举子类型都作为具有常量的类提供。
Schema::book()->bookFormat(Spatie\SchemaOrg\BookFormatType::Hardcover);
没有类型和属性的完整API文档。您可以参考源代码或schema.org网站。
如果您不想打断大型方案对象链,可以使用if
方法有条件地修改方案。
use Spatie\SchemaOrg\LocalBusiness; use Spatie\SchemaOrg\Schema; $business = ['name' => 'Spatie']; $localBusiness = Schema::localBusiness() ->name($business['name']) ->if(isset($business['email']), function (LocalBusiness $schema) use ($business) { $schema->email($business['email']); });
标识符
从v2.6.0版本开始,将identifier
键替换为@id
用于简单的字符串标识符。这是由于ld+json
语法的定义。
所有schema.org语法已经内置了对URI和URL的表示,例如在Microdata中为'itemid',在RDFa 1.1中为'resource',在JSON-LD中为'@id'。
— schema.org/docs // PR#102 // PR#157
高级使用
如果您需要设置自定义属性,可以使用setProperty
方法。
$localBusiness->setProperty('foo', 'bar');
如果您需要检索属性,可以使用 getProperty
方法。您可以可选地传递第二个参数来提供默认值。
$localBusiness->getProperty('name'); // 'Spatie' $localBusiness->getProperty('bar'); // null $localBusiness->getProperty('bar', 'baz'); // 'baz'
所有属性都可以使用 getProperties
方法以数组形式检索。
$localBusiness->getProperties(); // ['name' => 'Spatie', ...]
可以使用 addProperties
方法一次性设置多个属性。
$localBusiness->addProperties(['name' => 'value', 'foo' => 'bar']);
可以使用 getContext
和 getType
方法检索上下文和类型。
$localBusiness->getContext(); // 'https://schema.org' $localBusiness->getType(); // 'LocalBusiness'
图 - 多个项目
图有很多方法和工具 - 最安全、最简单的方法是使用 Spatie\SchemaOrg\Schema
类的重载方法。这些方法将获取请求的架构的已创建或新实例。
$graph = new Graph(); // Create a product and prelink organization $graph ->product() ->name('My cool Product') ->brand($graph->organization()); // Hide the organization from the created script tag $graph->hide(\Spatie\SchemaOrg\Organization::class); // Somewhere else fill out the organization $graph ->organization() ->name('My awesome Company'); // Render graph to script tag echo $graph;
使用这些工具,图是所有可用架构的集合,可以相互链接这些架构,并防止辅助架构在脚本标签中渲染。
图节点标识符
有时您必须跟踪多个相同类型的图节点 - 例如,在您的组织中跟踪多个 Person
节点代表不同的人。为此,您可以在图实例上使用节点标识符。如果您不提供标识符,则将使用保留关键字 default
标识符。
use Spatie\SchemaOrg\Graph; use Spatie\SchemaOrg\Person; $graph = new Graph(); // add a Person using chaining $graph->person('freekmurze') ->givenName('Freek') ->familyName('Van der Herten') ->alternateName('freekmurze'); // add a Person using closure $graph->person('sebastiandedeyne', function(Person $sebastian, Graph $graph): void { $sebastian ->givenName('Sebastian') ->familyName('De Deyne') ->alternateName('sebastiandedeyne'); }); // add a person using closure and second call with same identifier $graph->person( 'gummibeer', fn(Person $gummibeer) => $gummibeer->alternateName('gummibeer') ); $graph->person('gummibeer') ->givenName('Tom') ->familyName('Witkowski'); $graph->person('random')->name('Random Person'); // hide the random person from Graph $graph->hide(Person::class, 'random'); echo json_encode($graph);
{ "@context":"https:\/\/schema.org", "@graph":[ { "@type":"Person", "givenName":"Freek", "familyName":"Van der Herten", "alternateName":"freekmurze" }, { "@type":"Person", "givenName":"Sebastian", "familyName":"De Deyne", "alternateName":"sebastiandedeyne" }, { "@type":"Person", "alternateName":"gummibeer", "givenName":"Tom", "familyName":"Witkowski" } ] }
多类型实体
Schema.org 允许 多类型实体 - 要使用此包,您可以使用 MultiTypedEntity
类 - 它的工作方式与图类似。
$mte = new MultiTypedEntity(); $mte->hotelRoom()->name('The Presidential Suite'); $mte->product()->offers( Schema::offer() ->name('One Night') ->price(100000.00) ->priceCurrency('USD') ); $mte->product(function (Product $product) { $product->aggregateRating( Schema::aggregateRating() ->bestRating(5) ->worstRating(4) ); }); echo json_encode($mte);
{ "@context":"https:\/\/schema.org", "@type":[ "HotelRoom", "Product" ], "name":"The Presidential Suite", "offers":{ "@type":"Offer", "name":"One Night", "price":100000, "priceCurrency":"USD" }, "aggregateRating":{ "@type":"AggregateRating", "bestRating":5, "worstRating":4 } }
合并属性没有实际规则。它仅在后台使用 array_merge()
。因此,您应避免在 MTE 中在 不同类型上定义相同的属性,或者确保所有属性具有相同的值,这样最后使用哪个属性就不再重要了。
已知问题
- 由于它是 PHP 中的保留关键字,因此没有
Float
类型。 - 由于它扩展了
health
扩展规范中的类型,因此没有Physician
类型。
变更日志
有关最近更改的更多信息,请参阅 变更日志。
测试
$ composer test
贡献
有关详细信息,请参阅 贡献。
安全
如果您发现任何与安全相关的问题,请通过电子邮件 freek@spatie.be 而不是使用问题跟踪器。
明信片软件
您可以自由使用此包,但如果它进入您的生产环境,我们非常感谢您从您家乡给我们寄一张明信片,并说明您正在使用我们的哪些包。
我们的地址是:Spatie,Kruikstraat 22,2018 安特卫普,比利时。
我们将发布所有收到的明信片 在我们的公司网站上。
鸣谢
许可
MIT 许可证(MIT)。有关更多信息,请参阅 许可文件。