PHPackages                             cuevae/collection-plus-json - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. cuevae/collection-plus-json

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

cuevae/collection-plus-json
===========================

Collection+Json PHP library. This is a read/write hypermedia-type designed to support management and querying of simple collections.

v1.0.1(11y ago)39931[3 issues](https://github.com/cuevae/collection-json-php/issues)MITPHPPHP &gt;=5.4

Since Jul 14Pushed 7y ago2 watchersCompare

[ Source](https://github.com/cuevae/collection-json-php)[ Packagist](https://packagist.org/packages/cuevae/collection-plus-json)[ Docs](https://github.com/cuevae/collectionPlusJson-php)[ RSS](/packages/cuevae-collection-plus-json/feed)WikiDiscussions master Synced 4d ago

READMEChangelogDependencies (1)Versions (12)Used By (0)

[![Build Status](https://camo.githubusercontent.com/b42d05396ad16a69ee4c5178b3d2a97243a2cdd96d5ed23a0c4322149d729809/68747470733a2f2f7472617669732d63692e6f72672f6375657661652f636f6c6c656374696f6e2d6a736f6e2d7068702e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/cuevae/collection-json-php)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/28d98e7013967d6a6dc767e60fd3118b00f4e9efbc0750ec1379bf6d6ae9f5bf/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6375657661652f636f6c6c656374696f6e2d6a736f6e2d7068702f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/cuevae/collection-json-php/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/8ac03c46282bd41653aa7b36a40d46ed0c7402c4d126b1fb5ffac910bb1a9078/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6375657661652f636f6c6c656374696f6e2d6a736f6e2d7068702f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/cuevae/collection-json-php/?branch=master)

collectionPlusJson-php
======================

[](#collectionplusjson-php)

version: 1.0.1

Introduction
------------

[](#introduction)

> **Collection+JSON** is a JSON-based read/write hypermedia-type designed to support management and querying of simple collections.
>
> The **Collection+JSON** hypermedia type is designed to support full read/write capability for simple lists (contacts, &gt;tasks, blog entries, etc.). The standard application semantics supported by this media type include Create, Read, Update, &gt;and Delete (CRUD) along w/ support for predefined queries including query templates (similar to HTML "GET" forms). Write &gt;operations are defined using a template object supplied by the server as part of the response representation."
>
> *From Collection+JSON [site](http://amundsen.com/media-types/collection/format/)*

\##Installation

1. Install composer.phar (if you don't already have it)

    - [\*nix installation guide](https://getcomposer.org/doc/00-intro.md#installation-nix)
    - [Windows installation guide](https://getcomposer.org/doc/00-intro.md#installation-windows)
2. Edit your `composer.json` file to include the library

    ```
       "require": {
              "other/libraries" : "..."
              "cuevae/collection-plus-json": "*"
       }
    ```
3. Check you are good to go

    ```
       require '../vendor/autoload.php';

       $test = new \CollectionPlusJson\Collection('http://api.test.io');
       echo json_encode($test->output());
    ```

    That code should output:

    ```
       {"collection":
          {"version":"1.0.1",
           "href":"http:\/\/api.test.io",
           "links":[],
           "items":[],
           "queries":[],
           "template":null,
           "error":null
           }
       }
    ```

Example
-------

[](#example)

```
//Build the collection object
$colors = new Collection('http://api.colors.io/');

//Add a Link to the collection object
$colors->addLink(new Link($colors->getHref()->extend('rss'), 'feed', 'rss-feed', '', 'Subscribe to our RSS feed!'));

//Build an item
$color1 = new Item($colors->getHref()->extend('color1'));
$color1->addData('id', '1', 'This is the color id.')
       ->addData('hex_value', '#9932CC', 'This is the color in hex format.');

// or add data with dynamic setter
$color1->setHuman_value('DarkOrchid', 'This is the color in human readable format.');

$color1->addLink(new Link('http://www.w3schools.com/tags/ref_colornames.asp', 'source'));
$color1->addLink(new Link('http://www.w3schools.com/tags/ref_color_tryit.asp?hex=9932CC', 'color-test'));

//Build a second item
$color2 = new Item($colors->getHref()->extend('color2'));
$color2->addData('id', '2', 'This is the color id.')
       ->addData('hex_value', '#FFFFF0', 'This is the color in hex format.');

// or add data with dynamic setter
$color2->setHuman_value('DarkOrchid', 'This is the color in human readable format.');

$color2->addLink(new Link('http://www.w3schools.com/tags/ref_colornames.asp', 'source'));
$color2->addLink(new Link('http://www.w3schools.com/tags/ref_color_tryit.asp?hex=FFFFF0', 'color-test'));

//Add both items
$colors->addItems([$color1, $color2]);

//Build a collection query
$query = new Query($colors->getHref()->extend('search'), 'search');
$query->addData('search');
$colors->addQuery($query);

//Set the collection template
$template = new Template();
$template->addData('id', 'This is the color id.')
         ->addData('hex_value', 'This is the color in hex format.')
         ->addData('human_value', 'This is the color in human readable format.')
         ->addData('color-test', 'Link to test how your color looks with other colors.');

// or add data with dynamic setter
$template->setSource('Link to colors source');

$colors->setTemplate($template);

//Set an error
$error = new Error('error-test', 'ABC123', 'This is a test error. Server has encountered a problem and could not process your request, please try later.');
$colors->setError($error);

//Send response
$app->response->headers->set('Content-Type', 'application/vnd.collection+json');
echo json_encode($colors->output());

/*
Output would be:

{
    "collection": {
        "version": "1.0.1",
        "href": "http://api.colors.io/",
        "links": [
            {
                "href": "http://api.colors.io/rss",
                "rel": "feed",
                "prompt": "Subscribe to our RSS feed!",
                "name": "rss-feed",
                "render": ""
            }
        ],
        "items": [
            {
                "href": "http://api.colors.io/color1",
                "data": [
                    {
                        "name": "id",
                        "value": "1",
                        "prompt": "This is the color id."
                    },
                    {
                        "name": "hex_value",
                        "value": "#9932CC",
                        "prompt": "This is the color in hex format."
                    },
                    {
                        "name": "human_value",
                        "value": "DarkOrchid",
                        "prompt": "This is the color in human readable format."
                    }
                ],
                "links": [
                    {
                        "href": "http://www.w3schools.com/tags/ref_colornames.asp",
                        "rel": "source",
                        "prompt": "",
                        "name": "",
                        "render": ""
                    },
                    {
                        "href": "http://www.w3schools.com/tags/ref_color_tryit.asp?hex=9932CC",
                        "rel": "color-test",
                        "prompt": "",
                        "name": "",
                        "render": ""
                    }
                ]
            },
            {
                "href": "http://api.colors.io/color2",
                "data": [
                    {
                        "name": "id",
                        "value": "2",
                        "prompt": "This is the color id."
                    },
                    {
                        "name": "hex_value",
                        "value": "#FFFFF0",
                        "prompt": "This is the color in hex format."
                    },
                    {
                        "name": "human_value",
                        "value": "Ivory",
                        "prompt": "This is the color in human readable format."
                    }
                ],
                "links": [
                    {
                        "href": "http://www.w3schools.com/tags/ref_colornames.asp",
                        "rel": "source",
                        "prompt": "",
                        "name": "",
                        "render": ""
                    },
                    {
                        "href": "http://www.w3schools.com/tags/ref_color_tryit.asp?hex=FFFFF0",
                        "rel": "color-test",
                        "prompt": "",
                        "name": "",
                        "render": ""
                    }
                ]
            }
        ],
        "queries": [
            {
                "href": "http://api.colors.io/search",
                "rel": "search",
                "prompt": "",
                "data": [
                    {
                        "name": "search",
                        "value": null,
                        "prompt": ""
                    }
                ]
            }
        ],
        "template": [
            {
                "name": "id",
                "value": "",
                "prompt": "This is the color id."
            },
            {
                "name": "hex_value",
                "value": "",
                "prompt": "This is the color in hex format."
            },
            {
                "name": "human_value",
                "value": "",
                "prompt": "This is the color in human readable format."
            },
            {
                "name": "source",
                "value": "",
                "prompt": "Link to colors source."
            },
            {
                "name": "color-test",
                "value": "",
                "prompt": "Link to test how your color looks with other colors."
            }
        ],
        "error": {
            "title": "error-test",
            "code": "ABC-123",
            "message": "This is a test error. Server has encountered a problem and could not process your request, please try later."
        }
    }
}

*/
```

Consume a Collection+JSON object
================================

[](#consume-a-collectionjson-object)

This functionality enables a plain json string in Collection+JSON format to be consumed and translated into a Collection object for easy manipulation.

Example with transfer object:

```
// init Collection object with json string to parse
$collection = new Collection(json_decode($collectionJson, true));

// get the first item
$item = $collection->getFirstItem();

// get a fake transfer object
$entity = new ExampleEntity();

// add the data with dynamic getters from item object
$entity->setFoo($item->getFoo());
$entity->setBar($item->getBar());

// save to example database
$repo->persist($entity);
$repo->flush();
```

Example with template object from POST/PUT request. See [Collection+JSON Documentation](http://amundsen.com/media-types/collection/examples/#ex-write) for details

```
// init Template object with json string to parse
$template = new Template(json_decode($collectionJsonTemplate, true));

// get a fake transfer object
$entity = new ExampleEntity();

// add the data with dynamic getters from template object
$entity->setFoo($template->getFoo());
$entity->setBar($template->getBar());

// save to example database
$repo->persist($entity);
$repo->flush();
```

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 63.5% 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

Every ~4 days

Total

11

Last Release

4284d ago

Major Versions

v0.7.0 → v1.0.02014-08-22

### Community

Maintainers

![](https://www.gravatar.com/avatar/34169136795c5e2c58c607354de2f1fe9b5c343976514fb1e7402d3c67ce6b4f?d=identicon)[cuevae](/maintainers/cuevae)

---

Top Contributors

[![cuevae](https://avatars.githubusercontent.com/u/6576409?v=4)](https://github.com/cuevae "cuevae (54 commits)")[![nuxnik](https://avatars.githubusercontent.com/u/17590223?v=4)](https://github.com/nuxnik "nuxnik (31 commits)")

---

Tags

jsoncollectioncollection-plus-json

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/cuevae-collection-plus-json/health.svg)

```
[![Health](https://phpackages.com/badges/cuevae-collection-plus-json/health.svg)](https://phpackages.com/packages/cuevae-collection-plus-json)
```

###  Alternatives

[nette/utils

🛠 Nette Utils: lightweight utilities for string &amp; array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.

2.1k394.3M1.5k](/packages/nette-utils)[phpcollection/phpcollection

General-Purpose Collection Library for PHP

1.0k64.0M34](/packages/phpcollection-phpcollection)[cuyz/valinor

Dependency free PHP library that helps to map any input into a strongly-typed structure.

1.5k9.2M108](/packages/cuyz-valinor)[cerbero/lazy-json

Framework-agnostic package to load JSONs of any dimension and from any source into Laravel lazy collections.

254309.8k1](/packages/cerbero-lazy-json)[lorisleiva/lody

Load files and classes as lazy collections in Laravel.

956.6M9](/packages/lorisleiva-lody)[beste/json

A simple JSON helper to decode and encode JSON

4222.7M3](/packages/beste-json)

PHPackages © 2026

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