承接 porkchopsandwiches/preserialiser 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

porkchopsandwiches/preserialiser

Composer 安装命令:

composer require porkchopsandwiches/preserialiser

包简介

A generic pre-serialiser for converting objects to generic types.

README 文档

README

A simple PHP Preserialiser, use before serialising data into JSON, XML, etc. Recursively iterates through values where applicable.

Install via Composer

Add repo and require to composer.json:

{
	"require": {
		"porkchopsandwiches/preserialiser": "dev-master"
	}
}

Basic usage

use PorkChopSandwiches\Preserialiser\Preserialiser;

$p = new Preserialiser();
$p -> preserialise(1); # => 1
$p -> preserialise("string"); # => "string"
$p -> preserialise(array(1, true, "three")); # => array(1, true, "three")

$obj = new stdClass;
$obj -> prop = "value";
$p -> preserialise($obj); # => array("prop" => "value")

class ExampleA {
	private $a = "foo";
	public $b = "bar";
}
$p -> preserialise(new ExampleA()); # => array("a" => "bar")

Preserialisable usage

use PorkChopSandwiches\Preserialiser\Preserialiser;
use PorkChopSandwiches\Preserialiser\Preserialisable;

class ExampleB implements Preserialisable {
	private $a = "foo";
	private $b = "bar";
	
	public function preserialise (array $args = array()) {
		$data = array(
			"a" => $this -> a
		);
			
		if (array_key_exists("include_b", $args) && !!$args["include_b"]) {
			$data["b"] = $this -> b;
		}
			
		return $data;
	}
}
	
$p = new Preserialiser();
$ex = new ExampleB();
	
$p -> preserialise($ex);
# => array("a" => "foo")
$p -> preserialise($ex, array("include_b" => true));
# => array("a" => "foo", "b" => "bar")

Recursive usage

use PorkChopSandwiches\Preserialiser\Preserialiser;
use PorkChopSandwiches\Preserialiser\Preserialisable;

class ExampleParent implements Preserialisable {
	private $children = array();
	
	public function addChild(ExampleChild $child) {
		$child -> setParent($this);
		$this -> children[] = $child;
	}
	
	public function preserialise (array $args = array()) {
		$data = array(
			"type" => "parent"
		);
		
		if (array_key_exists("include_children", $args) && !!$args["include_children"]) {
			$data["children"] = $this -> children;
		}
		
		return $data;
	}
}

class ExampleChild implements Preserialisable {
	private $parent = null;
	
	public function setParent(ExampleParent $parent) {
		$this -> parent = $parent;
	}
	
	public function preserialise (array $args = array()) {
		$data = array(
			"type" => "child"
		);
		
		if (array_key_exists("include_parent", $args) && !!$args["include_parent"]) {
			$data["parent"] = $this -> parent;
		}
		
		return $data;
	}
}

$p = new Preserialiser();
$parent = new ExampleParent();
$child = new ExampleChild();
$parent -> addChild($child);

$p -> preserialise($parent);
# => array("type" => "parent")
$p -> preserialise($parent, array("include_children" => true));
# => array("type" => "parent", "children" => array(array("type" => "child")))
$p -> preserialise($child, array("include_parent" => true));
# => array("type" => "child", "parent" => array("type" => "parent"))
$p -> preserialise($parent, array("include_children" => true, "include_parent" => true));
# => throws PreserialiserMaxDepthException

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-04-21

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固