melting-server/schema-dot-org-laravel

提供熔化服务器到Laravel中的schema-dot-org-tree。

dev-master 2019-11-10 20:26 UTC

This package is auto-updated.

Last update: 2024-09-11 14:33:15 UTC


README

此库抓取并解析Schema.org到Laravel 5+的有序数组,并按您的应用缓存偏好进行缓存。

https://github.com/spatie/schema-org 是在IDE中使用Schema.org的更友好工具。Spatie的schema工具包含IDE自动完成和其他许多帮助您处理Schema.org的实用功能。

安装

composer require melting-server\schema-dot-org-laravel

可选 | 您可以发布配置文件来调整缓存设置。

php artisan vendor:publish --provider="Schema\SchemaServiceProvider"

用法

初始化

use SchemaDotOrgTree\Tree;

$tree = app()->make(Tree::class);

默认情况下,应用程序第一次构建树时,它将从schema.org的GitHub存储库下载jsonld并将其解析为树。然后树将在应用程序中缓存(无论您如何设置默认的Laravel缓存)。整个过程大约需要2秒,但由于它是缓存的,所以仍然很方便。

//For the latest schema.org (default):
$tree = new Tree(); 

//For "all" of the latest schemas:
$tree = new Tree('5.0-all');

//For a specific version:
$tree = new Tree('3.7-core');

现在您可以访问结构化树

var_dump($tree->getTree()); //Structured tree 

实体

获取实体

$entity = $tree->getEntity("http://schema.org/Thing");

实体可以有父实体,并且可能有子实体

$parent = $entity->getParent(); // returns Entity or null
$children = $entity->getChildren(); // returns Entity[] (the array might be empty)

您可以通过测试查看实体是否在任何地方存在于树中

$tree->isLocatable("http://schema.org/Thing");

实体提供以下公共属性(不要与下面的实体Schema属性混淆)

// The Entity Property            The schema.org "field"
public $id;                    // from "@id"
public $type;                  // from "@type"
public $supersededBy;          // from "http://schema.org/supersededBy"
public $comment;               // from "rdfs:comment"
public $label;                 // from "rdfs:label"
public $subClassOf;            // from "rdfs:subClassOf"
public $purlSource;            // from "http://purl.org/dc/terms/source"
public $owlEquivalentProperty; // from "http://www.w3.org/2002/07/owl#equivalentClass"
public $category;              // from "http://schema.org/category"
public $closeMatch;            // from "http://www.w3.org/2004/02/skos/core#closeMatch"

/** @var string $version */
public $version;

/** @var Entity[] */
public $children = [];

/** @var Property[] */
public $properties = [];

属性

获取实体的属性

$properties = $entity->getProperties();      //with inherited properties
$properties = $entity->getProperties(false); //without inherited properties

属性提供以下公共属性

// The Entity Property            The schema.org "field"
public $id;                       // from @id
public $type;                     // from @type
public $domainIncludes = [];      // from http://schema.org/domainIncludes
public $rangeIncludes = [];       // from http://schema.org/rangeIncludes
public $comment = "";             // from rdfs:comment
public $label = "";               // from rdfs:label
public $purlSource;               // from http://purl.org/dc/terms/source
public $owlEquivalentProperty;    // from http://www.w3.org/2002/07/owl#equivalentProperty
public $subPropertyOf;            // from rdfs:subPropertyOf
public $category;                 // from http://schema.org/category
public $inverseOf;                // from http://schema.org/inverseOf
public $supersededBy;             // from http://schema.org/supersededBy

/** @var string */
public $version;

/** @var bool */
public $inherited; 

高级用法

大多数开发者可能不需要这些功能,但您有权决定。

读取数据

通过

$tree->reader->getJson();

多版本支持

使用配置文件指定您希望使用的版本,该配置文件将概述一个您可以使用的环境变量(.env)。

SCHEMA_DOT_ORG_VERSION=3.7-all

您可以使用以下命令获取可用版本的列表

$tree->reader::VERSIONS

当前支持的版本

您可以通过比较版本来查看不同版本之间包含/排除的内容。大多数实现应仅使用默认的“最新”版本。

new Tree('latest');
new Tree('4.0-core');
new Tree('3.7-core');
var_dump(Tree::$trees);

/** 
Outputs: [
    'latest' => {Tree}
    '4.0-core' => {Tree}
    '3.7-core' => {Tree}
]
*/

在有多个内存树的情况下,您可以检索特定版本的特定实体。

$entity = Tree::getEntityReference('latest', 'http://schema.org/Thing');

每个类/属性/数据类型都会加载有关其是从哪个版本加载的知识。

$entity->version;