melting-server/schema-dot-org-tree

读取schema.org的某个版本并构建出结构化树。可以访问任何条目的直接或继承属性,或使用$entity->parent()或$entity->children遍历树。支持多个schema版本。

dev-master 2019-11-12 22:07 UTC

This package is auto-updated.

Last update: 2024-09-13 08:48:14 UTC


README

这个库从Schema.org抓取并解析数据到一个结构化数组中。如果你在寻找一个可以帮助开发者使用php处理Schema的库,请查看https://github.com/spatie/schema-org,它包含了一些在IDE中处理schema的便利功能和快捷方式。此存储库不包含实际的schema.org json - 它将根据需要从https://github.com/schemaorg/schemaorg/tree/master/data/releases下载。

结果结构化树的缓存/存储应由你的应用程序处理。此库下载并解析json数据到一个结构化数组,进行一些对象归一化 - 仅此而已。

安装

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

用法

初始化

include('./vendor/autoload.php');
use \schemaDotOrgTree\Tree;

结构化Schema树

下载schema.org的某个版本并将其处理为结构化数据

//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; 

高级用法

大多数开发者可能不需要这些功能,但你有你的选择。

读取数据

通过以下方式访问schema.org的底层json数据:

$tree->reader->getJson();

多版本支持

你可以通过以下方式获取可用版本的列表:

$tree->reader::VERSIONS

当前支持版本


With multiple trees in memory, you can retrieve specfic entities from specific versions.
```php
$tree->getEntityReference('latest', 'http://schema.org/Thing');

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

$entity->version;

局限性

Schema设计为可扩展的,并且故意不是一个“通用schema”,因为它不是创建来涵盖所有存在的名词和形容词类型的。相反,它试图足够模糊,以至于只有当它有助于帮助系统对主题的深层理解或其与其他主题的关系时,才会创建单独的实体。这种灵活性使得schema成为一个强大的定义,但也允许它有些奇怪(例如,火山有传真号码)。在实现schema时,您需要决定在实施中会多么严格。我已经做出了一些决策,以帮助在处理速度与完整性之间取得平衡。

这个库最重要的局限性是单继承。例如,一个体育场馆既是公共建筑也是体育场所。这些实例在schema-core中不常见(在完整schema范围内使用时影响更大)但确实存在。这个库选择列出的第一个父类,并忽略其他类。这个库的目的是以树的形式在php数组中表示Schema,不支持多继承。如果这种偏好严重破坏了实体的意义,请给我发消息。从我看到的5.0-core中的实例来看,没有什么实质性的损失。