PHPackages                             porkchopsandwiches/preserialiser - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. porkchopsandwiches/preserialiser

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

porkchopsandwiches/preserialiser
================================

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

1.0.0(12y ago)01153MITPHP

Since Apr 21Pushed 11y ago1 watchersCompare

[ Source](https://github.com/porkchopsandwiches/preserialiser)[ Packagist](https://packagist.org/packages/porkchopsandwiches/preserialiser)[ RSS](/packages/porkchopsandwiches-preserialiser/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (2)Used By (3)

porkchopsandwiches/preserialiser
================================

[](#porkchopsandwichespreserialiser)

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

Install via Composer
--------------------

[](#install-via-composer)

Add repo and require to composer.json:

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

Basic usage
-----------

[](#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
-----------------------

[](#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
---------------

[](#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
```

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

4404d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7344435?v=4)[Cam Morrow](/maintainers/PorkChopSandwiches)[@porkchopsandwiches](https://github.com/porkchopsandwiches)

---

Top Contributors

[![porkchopsandwiches](https://avatars.githubusercontent.com/u/7344435?v=4)](https://github.com/porkchopsandwiches "porkchopsandwiches (13 commits)")

### Embed Badge

![Health badge](/badges/porkchopsandwiches-preserialiser/health.svg)

```
[![Health](https://phpackages.com/badges/porkchopsandwiches-preserialiser/health.svg)](https://phpackages.com/packages/porkchopsandwiches-preserialiser)
```

###  Alternatives

[mtdowling/jmespath.php

Declaratively specify how to extract elements from a JSON document

2.0k472.8M135](/packages/mtdowling-jmespathphp)[opis/closure

A library that can be used to serialize closures (anonymous functions) and arbitrary data.

2.6k230.0M284](/packages/opis-closure)[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[sabberworm/php-css-parser

Parser for CSS Files written in PHP

1.8k191.2M65](/packages/sabberworm-php-css-parser)[michelf/php-markdown

PHP Markdown

3.5k52.4M345](/packages/michelf-php-markdown)[jms/metadata

Class/method/property metadata management in PHP

1.8k152.8M88](/packages/jms-metadata)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
