首页 > 解决方案 > TypeMapper 类对象属性为空

问题描述

我是 rdf 和 easyrdf 框架的新手。我们目前正在创建一个小型 poc,看看它是否符合我们的需求。

我们希望将给定的 RDF 文件或数据(例如以 rdf/xml 格式化)转换为我们的类对象。

因此,我已经通过 TypeMapper 注册了该类。

TypeMapper::set('foaf:Product', RdfProduct::class);
TypeMapper::set('foaf:Price', RdfProductPrice::class);

我的课:

class RdfProduct extends Resource
{
    /** @var string */
    public $productID;
    /** @var string */
    public $name;
    /** @var string */
    public $description;

    //Do they have to be public oder private? with getters/setters?!
}

我的数据文件:

<?xml version="1.0" encoding="utf-8" ?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:foaf="http://xmlns.com/foaf/0.1/">

    <foaf:Product rdf:about="http://test.com/product">
        <foaf:productID xml:lang="de_DE">e16d61860c644d4088de12b67e22f3b2</foaf:productID>
        <foaf:name xml:lang="de_DE">Demo Produkt</foaf:name>
        <foaf:name xml:lang="en_GB">Demo Product</foaf:name>
        <foaf:description xml:lang="de_DE">Test Beschreibung Lorem Ipsum</foaf:description>
        <foaf:description xml:lang="en_GB">Test description Lorem Ipsum</foaf:description>
        <foaf:brand xml:lang="de_DE">Test</foaf:brand>
        <foaf:height xml:lang="de_DE">120</foaf:height>
        <foaf:depth xml:lang="de_DE">210</foaf:depth>
        <foaf:weight xml:lang="de_DE">2.5</foaf:weight>
        <foaf:width xml:lang="de_DE">100</foaf:width>
        <foaf:manufacturer xml:lang="de_DE">Test GmbH</foaf:manufacturer>
        <foaf:sku xml:lang="de_DE">DemoTest1</foaf:sku>
        <foaf:hasVariant xml:lang="de_DE"></foaf:hasVariant>
        <foaf:price>
            <foaf:Price rdf:about="http://test.com/product/price">
                <foaf:net rdf:datatype="http://www.w3.org/2001/XMLSchema#double">17.31</foaf:net>
                <foaf:gross rdf:datatype="http://www.w3.org/2001/XMLSchema#double">20.59</foaf:gross>
            </foaf:Price>
        </foaf:price>

        <foaf:category>
            <foaf:Category rdf:about="http://test.com/product/category">
                <foaf:name>Main</foaf:name>
                <foaf:name>Submain</foaf:name>
            </foaf:Category>
        </foaf:category>

        <foaf:media>
            <foaf:Media rdf:about="http://test.com/product/media">
                <foaf:MediaItem>
                    <foaf:MediaItem rdf:about="http://test.com/product/media-item">
                        <foaf:url>https://test.com/test.png</foaf:url>
                        <foaf:url>https://test.com/test.jpg
                        </foaf:url>
                    </foaf:MediaItem>
                </foaf:MediaItem>

            </foaf:Media>
        </foaf:media>

    </foaf:Product>

</rdf:RDF>

我的实现:

TypeMapper::set('foaf:Product', RdfProduct::class);
TypeMapper::set('foaf:Price', RdfProductPrice::class);

$rdfXml = file_get_contents(__DIR__ . '/product-demo.xml');

$graph = new Graph(
  'http://test.com/product',
  $rdfXml,
  'rdfxml'
);
        
$rdfProduct = current($graph->resources());
dd($rdfProduct);

但是类的属性是空的..

有任何想法吗?我也可以有一个 RdfProduct 对象实例并将其转换回 rdf 格式吗?

标签: rdfrdfseasyrdf

解决方案


推荐阅读