blackfyre / json-ld
0.0.11
2019-02-12 10:32 UTC
Requires
- php: >=5.5
Requires (Dev)
- mockery/mockery: ^0.9.4
- phpunit/phpunit: ~5.0
README
这是一个Torann的JSON-LD生成器的分支,我仅扩展了缺少的部分 😄
极为简单的JSON-LD生成器。
安装
在命令行中运行
$ composer require blackfyre/json-ld
方法
/JsonLd/Context.php
create($context, array $data = [])
getProperties()
generate()
上下文类型
- article
- beach
- blog_posting
- breadcrumb_list
- contact_point
- corporation
- creative_work
- duration
- event
- geo_coordinates
- image_object
- invoice
- job_posting
- list_item
- local_business
- music_album
- music_group
- music_playlist
- music_recording
- news_article
- offer
- order
- organization
- person
- place
- postal_address
- price_specification
- product
- rating
- review
- search_box
- thing
- video_object
- web_page
示例
快速示例
商业
$context = \JsonLd\Context::create('local_business', [ 'name' => 'Consectetur Adipiscing', 'description' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor', 'telephone' => '555-555-5555', 'openingHours' => 'mon,tue,fri', 'address' => [ 'streetAddress' => '112 Apple St.', 'addressLocality' => 'Hamden', 'addressRegion' => 'CT', 'postalCode' => '06514', ], 'geo' => [ 'latitude' => '41.3958333', 'longitude' => '-72.8972222', ], ]); echo $context; // Will output the script tag
新闻文章
$context = \JsonLd\Context::create('news_article', [ 'headline' => 'Article headline', 'description' => 'A most wonderful article', 'mainEntityOfPage' => [ 'url' => 'https://google.com/article', ], 'image' => [ 'url' => 'https://google.com/thumbnail1.jpg', 'height' => 800, 'width' => 800, ], 'datePublished' => '2015-02-05T08:00:00+08:00', 'dateModified' => '2015-02-05T09:20:00+08:00', 'author' => [ 'name' => 'John Doe', ], 'publisher' => [ 'name' => 'Google', 'logo' => [ 'url' => 'https://google.com/logo.jpg', 'width' => 600, 'height' => 60, ] ], ]); echo $context; // Will output the script tag
在Laracasts展示器中使用JSON-LD
尽管这个例子展示了如何在Laracasts\Presenter
展示器中使用JSON-LD,但此包并不需要Laravel。
/App/Presenters/BusinessPresenter.php
<?php namespace App\Presenters; use JsonLd\Context; use Laracasts\Presenter\Presenter; class BusinessPresenter extends Presenter { /** * Create JSON-LD object. * * @return \JsonLd\Context */ public function jsonLd() { return Context::create('local_business', [ 'name' => $this->entity->name, 'description' => $this->entity->description, 'telephone' => $this->entity->telephone, 'openingHours' => 'mon,tue,fri', 'address' => [ 'streetAddress' => $this->entity->address, 'addressLocality' => $this->entity->city, 'addressRegion' => $this->entity->state, 'postalCode' => $this->entity->postalCode, ], 'geo' => [ 'latitude' => $this->entity->location->lat, 'longitude' => $this->entity->location->lng, ], ]); } }
生成标签
生成器包含一个__toString
方法,当作为字符串显示时会自动生成正确的脚本标签。
在Laravel视图中
echo $business->present()->jsonLd();
在Laravel视图中
{!! $business->present()->jsonLd() !!}
自定义上下文类型
create($context, array $data = [])
方法的第一参数也接受类名。这对于自定义上下文类型很有帮助。
<?php namespace App\JsonLd; use JsonLd\ContextTypes\AbstractContext; class FooBar extends AbstractContext { /** * Property structure * * @var array */ protected $structure = [ 'name' => null, 'description' => null, 'image' => null, 'url' => null, ]; }
$context = \JsonLd\Context::create(\App\JsonLd\FooBar::class, [ 'name' => 'Foo Foo headline', 'description' => 'Bar bar article description', 'url' => 'http://google.com', ]); echo $context; // Will output the script tag