anton-am/json-ld

一个非常简单的 JSON-LD 标记生成器。

1.0.0 2023-03-13 14:05 UTC

This package is auto-updated.

Last update: 2024-09-13 17:35:38 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

一个非常简单的 JSON-LD 生成器。

依赖项

  • PHP 8.0 或更高版本
  • JSON 扩展

安装

从命令行运行

$ composer require anton-am/json-ld

或添加到您的 composer.json 中

"anton-am/json-ld": "^1.0.0"

方法

/JsonLd/Context.php

  • create($context, array $data = [])
  • getProperties()
  • generate()

/JsonLd/MultiContext.php

  • create(array $contextList, int $type = self::TYPE_GRAPH)
  • generateScripts()
  • generateArray()
  • generateGraph()
  • generate()

上下文类型类

所有当前可用的上下文类型都可以在 ContextTypes 目录 中找到

示例

快速示例

企业

use AntonAm\JsonLD\Context;
use AntonAm\JsonLD\ContextTypes\LocalBusiness;

$context = Context::create(LocalBusiness::class, [
    '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

新闻文章

use AntonAm\JsonLD\Context;
use AntonAm\JsonLD\ContextTypes\NewsArticle;

$context = Context::create(NewsArticle::class, [
    '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 Presenter 中使用 JSON-LD

尽管这个示例展示了在 Laracasts\Presenter 展示者中使用 JSON-LD,但不需要 Laravel 来使用此包。

/App/Presenters/BusinessPresenter.php

<?php

namespace App\Presenters;

use AntonAm\JsonLD\Context;
use AntonAm\JsonLD\ContextTypes\LocalBusiness;
use Laracasts\Presenter\Presenter;

class BusinessPresenter extends Presenter
{
    /**
     * Create JSON-LD object.
     *
     * @return \AntonAm\JsonLD\Context
     */
    public function jsonLd()
    {
        return Context::create(LocalBusiness::class, [
            '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\AntonAm\JsonLD;

use AntonAm\JsonLD\ContextTypes\AbstractContext;

class FooBar extends AbstractContext
{
    /**
     * Property structure
     *
     * @var array
     */
    protected $structure = [
        'name' => null,
        'description' => null,
        'image' => null,
        'url' => null,
    ];
}
use AntonAm\JsonLD\Context;
use App\JsonLD\FooBar;

$context = Context::create(FooBar::class, [
    'name' => 'Foo Foo headline',
    'description' => 'Bar bar article description',
    'url' => 'https://google.com',
]);

echo $context; // Will output the script tag

多个上下文

use AntonAm\JsonLD\Context;
use AntonAm\JsonLD\ContextTypes\WebSite;
use AntonAm\JsonLD\ContextTypes\BreadcrumbList;

$context =  MultiContext::create([
    [
        'context' => WebSite::class,
        'data'    => [
            'name'            => 'Google',
            'url'             => 'https://google.com/',
            'potentialAction' => [
                'target'      => 'https://google.com/results?q={search_term_string}',
                'query'       => 'required name=search_term_string',
                'query-input' => 'required name=search_term_string'
            ]
        ]
    ],
    [
        'context' => BreadcrumbList::class,
        'data'    => [
            'itemListElement' => [
                [
                    'name' => 'Main',
                    'url'  => 'https://google.com'
                ],
                [
                    'name' => 'Section',
                    'url'  => 'https://google.com/section'
                ],
                [
                    'name' => 'Article title',
                    'url'  => 'https://google.com/section/article'
                ]
            ]
        ]
    ]
]);

echo $context; // Will output the script tag