isometriks / json-ld-dumper
此包的最新版本(v0.1.1)没有可用的许可证信息。
v0.1.1
2019-01-23 21:05 UTC
Requires
- symfony/expression-language: ^2.8|^3.0|^4.0
- symfony/property-access: ^2.8|^3.0|^4.0
This package is auto-updated.
Last update: 2024-09-24 09:21:51 UTC
README
本项目旨在帮助您将对象或其他存储的静态数据序列化为JSON-LD微数据。
以下是一个展示大多数功能的相当大的示例
<?php use Isometriks\JsonLdDumper\Dumper; use Isometriks\JsonLdDumper\MappingConfiguration; use Isometriks\JsonLdDumper\Parser; use Isometriks\JsonLdDumper\Replacer\DateReplacer; use Isometriks\JsonLdDumper\Replacer\ExpressionReplacer; use Isometriks\JsonLdDumper\Replacer\ResourceReplacer; use Isometriks\JsonLdDumper\Replacer\StaticReplacer; use Isometriks\JsonLdDumper\Test\Model\AuthorInterface; use Isometriks\JsonLdDumper\Test\Model\Image; use Isometriks\JsonLdDumper\Test\Model\NewsArticle; use Symfony\Component\ExpressionLanguage\ExpressionLanguage; include __DIR__ . '/vendor/autoload.php'; $static = [ 'logo' => [ '@context' => 'http://schema.org/', '@type' => 'ImageObject', 'url' => 'https://riwebgurus.com/media/image/logo.png', ], 'company' => [ '@context' => 'http://schema.org/', '@type' => 'Organization', 'name' => 'RI Web Gurus', 'logo' => '$static.logo', 'url' => 'https://riwebgurus.com', ], ]; // Entities $entities = [ NewsArticle::class => [ '@context' => 'http://schema.org/', '@type' => 'NewsArticle', 'headline' => '$resource.headline', 'image' => '$resource.image', 'publisher' => '$static.company', 'datePublished' => '$resource.published', 'author' => '$resource.author', ], Image::class => [ '@context' => 'http://schema.org/', '@type' => 'ImageObject', 'url' => '$resource.url', 'width' => '$resource.width', 'height' => '$resource.height', ], AuthorInterface::class => [ '@context' => 'http://schema.org/', '@type' => 'Person', 'name' => 'expr:"Mr. " ~ context.getName()', ], ]; $mapping = new MappingConfiguration($static, $entities); $expressionLanguage = new ExpressionLanguage(); $parser = new Parser($mapping, [ new StaticReplacer($mapping), new ResourceReplacer(), new DateReplacer(), new ExpressionReplacer($expressionLanguage), ]); $dumper = new Dumper($parser); echo $dumper->dump([ new NewsArticle(), '$static.company', ]);
结果是
<script type="application/ld+json"> [ { "@context": "http://schema.org/", "@type": "NewsArticle", "headline": "Here is a headline", "image": { "@context": "http://schema.org/", "@type": "ImageObject", "url": "http://placehold.it/800x800", "width": 800, "height": 800 }, "publisher": { "@context": "http://schema.org/", "@type": "Organization", "name": "RI Web Gurus", "logo": { "@context": "http://schema.org/", "@type": "ImageObject", "url": "https://riwebgurus.com/media/image/logo.png" }, "url": "https://riwebgurus.com" }, "datePublished": "2016-11-30T03:53:00+00:00", "author": { "@context": "http://schema.org/", "@type": "Person", "name": "Mr. Craig Blanchette" } }, { "@context": "http://schema.org/", "@type": "Organization", "name": "RI Web Gurus", "logo": { "@context": "http://schema.org/", "@type": "ImageObject", "url": "https://riwebgurus.com/media/image/logo.png" }, "url": "https://riwebgurus.com" } ] </script>
安全值
在序列化任何实体/静态映射时,可能存在值替换。其中一些被认为是安全的(替换一个对象,然后也可以进行序列化),而另一些可能是危险的(替换对象中的字符串)。
如果从对象中的字符串替换包含我们替换的任何模式,则我们不想进一步解析该值。一个可能不太有害的例子
class FakeModel { public $var = '$static.company'; }
如果任何对象是用户创建的,那么可能可以获取不应返回的信息。由于您可以创建自己的解析器,因此在使用回调解析器或使用Symfony实现调用任意服务时,您可能需要小心。这些应该只允许来自映射本身,而不是来自对象的返回值。