定制 harm-less/php-encoder 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

harm-less/php-encoder

Composer 安装命令:

composer require harm-less/php-encoder

包简介

Encoder to intelligently convert an entire PHP object to Json or XML e.g.

README 文档

README

Build Status

php-encoder is a fast & flexible encoder for PHP 5.3+

About

This library will allow you to save a snapshot of your PHP object in various formats like XML or JSON (encoding). When your project has been setup properly you can reuse the snapshot by decoding it. The decoding process will return the same PHP object you started with.

This library is useful for you if you want to quickly find a way to save a state of your PHP object. Think about a configurator that allows you to customize a certain product in various ways for example.

Getting started

  1. PHP 5.3.x is required
  2. Install php-encoder using Composer (recommended) or manually
  3. Set up the nodes you need for your PHP objects

Composer Installation

  1. Get Composer
  2. Require the encoder with composer require harm-less/php-encoder
  3. Add the following to your application's main PHP file: require 'vendor/autoload.php';

Included encoders

There are currently 2 encoding types available: XML and JSON. Both can be used interchangeably as long as you have set up your nodes correctly.

Example

Hello World - Obligatory hello world example

<?php
require('vendor/autoload.php');

use PE\Encoders\XmlEncoder;

use PE\Nodes\EncoderNode;
use PE\Nodes\EncoderNodeVariable;

// create a simple class with 1 variable and a setter/getter
class HelloWorld {

	private $foo;

	public function setFoo($value) {
		$this->foo = $value;
	}
	public function getFoo() {
		return $this->foo;
	}
}

// create a corresponding node and add the variable
class HelloWorldNode extends EncoderNode {

	function __construct() {
		parent::__construct('hello-worlds', 'hello-world', null);

		$this->addVariable(new EncoderNodeVariable('foo'));
	}
}

// register the node so it becomes known to the encoder
EncoderNode::addNode(new HelloWorldNode());

// create a HelloWorld object
$helloWorld = new HelloWorld();
$helloWorld->setFoo('hello world');

// make an instance of an encoder type and encode the object
$encoder = new XmlEncoder();
$encodedResultXml = $encoder->encode($helloWorld);

// will output:
/* <?xml version="1.0" encoding="UTF-8"?>
 * <encoded>
 *   <hello-world foo="hello world"/>
 * </encoded>
 */
echo htmlentities($encodedResultXml->saveXML());

// decode the XML again
$decoded = $encoder->decode($encodedResultXml->saveXML());

// will output:
/*
 * HelloWorld Object
 * (
 *   [foo:HelloWorld:private] => hello world
 * )
 */
print_r($decoded['hello-world']);
?>

Unit Testing

This project uses PHPUnit as its unit testing framework.

The tests all live in /tests and each test extends an abstract class AbstractPETest

To test the project, simply run composer install --dev to download a common version of PHPUnit with composer and run the tests from the main directory with phpunit or ./vendor/bin/phpunit

Contributors

统计信息

  • 总下载量: 38
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-02-07

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固