maniaplanet/manialib-xml

此包已被废弃,不再维护。作者建议使用manialib/xml包。

用于编写XML的对象式PHP库。

v0.1.1 2014-05-22 08:29 UTC

This package is auto-updated.

Last update: 2022-02-01 12:35:29 UTC


README

Latest Stable Version Latest Unstable Version Total Downloads Build

ManiaLib\XML是一个用于编写XML的对象式PHP库。

安装

通过Composer安装:

{
	"require": {
        "maniaplanet/manialib-xml": "0.2.*@dev"
    }
}

特性

  • 简单灵活的对象式架构
  • 可配置的渲染驱动程序
  • Symfony\Component\EventDispatcher集成

架构

  • 您构建一个ManiaLib\XML\Node树。
  • 设置方法返回元素以便链式调用(例如:$node->setNodeName('foo')->setNodeValue('bar');)。
  • ManiaLib\XML\Node::create()实例化对象并返回它,以便轻松链式调用(例如:Node::create()->setNodeName('foo');)。
  • 如果您的PHP版本为5.4或更高,您可以在实例化时使用类成员访问,例如:(new Node)->setNodeName('foo');
  • 请参阅ManiaLib\XML\NodeInterface以获取参考。
  • 然后,您将根Node传递给ManiaLib\XML\Rendering\Renderer的一个实例。

示例

<?php

use ManiaLib\XML\Node;
use ManiaLib\XML\Rendering\Renderer;

require_once '../vendor/autoload.php';

// Let's build a Node tree. Here is the root element.
$root = Node::create()
	->setNodeName('rootElement')
	->setAttribute('rootAttrivute', '1.0');

// This is one way to append child, ie. "append this element to its parent"
Node::create()
	->setNodeName('someElement')
	->setAttribute('someAttribute', 'foo')
	->setAttribute('otherAttribute', 'bar')
	->setNodeValue('Hello world')
	->appendTo($root);

// This is another way, ie. "appends a child to this element"
$node = Node::create()->setNodeName('anotherOne');
$root->appendChild($node);

// Let's render the tree
$renderer = new Renderer();
$renderer->setRoot($root);
echo $renderer->getXML();

它将输出

<rootElement rootAttrivute="1.0">
    <someElement someAttribute="foo" otherAttribute="bar">
        Hello world
    </someElement>
    <anotherOne/>
</rootElement>

更多内容请参阅/examples目录

测试

一个简单的测试套件测试了/examples目录中的.php文件与其关联的.xml渲染文件。要运行测试,我们建议通过Composer安装PHPUnit系统:

待办事项

  • XML注释
  • PhpDoc
  • 原始节点值
  • 其他不支持的节点功能?