Skip to content

Commit

Permalink
Merge branch '1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
kkamkou committed May 1, 2015
2 parents a18c1c9 + 1a97b72 commit 1441777
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 38 deletions.
15 changes: 5 additions & 10 deletions CollectionJson/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@ class Collection implements ArrayConvertible
*/
public function __construct($href)
{
// extension validation
if (!extension_loaded('json')) {
throw new \RuntimeException('the json extension is required for this library');
}

$this->setHref($href);
}

Expand Down Expand Up @@ -184,15 +179,15 @@ public function addQuery(Property\Query $query)
/** @return string */
public function __toString()
{
return json_encode($this->__toArray());
return json_encode($this->toArray());
}

/**
* Converts the whole object to an array
*
* @return array
*/
public function __toArray()
public function toArray()
{
// defaults
$collection = array(
Expand All @@ -202,19 +197,19 @@ public function __toArray()

// we have an error object
if ($this->error) {
$collection['error'] = $this->error->__toArray();
$collection['error'] = $this->error->toArray();
}

// we have a template object
if ($this->template) {
$collection = array_merge($collection, $this->template->__toArray());
$collection = array_merge($collection, $this->template->toArray());
}

// we have some other objects
foreach (array('items', 'queries', 'links') as $type) {
if (count($this->{$type})) {
foreach ($this->{$type} as $item) {
$collection[$type][] = $item->__toArray();
$collection[$type][] = $item->toArray();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion CollectionJson/Collection/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function getTitle()
}

/** @return array */
public function __toArray()
public function toArray()
{
return array_filter(
array(
Expand Down
6 changes: 3 additions & 3 deletions CollectionJson/Collection/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,19 +125,19 @@ public function getLinks()
}

/** @return array */
public function __toArray()
public function toArray()
{
$return = array('href' => $this->getHref());

if (count($this->data)) {
foreach ($this->getData() as $data) {
$return['data'][] = $data->__toArray();
$return['data'][] = $data->toArray();
}
}

if (count($this->links)) {
foreach ($this->getLinks() as $link) {
$return['links'][] = $link->__toArray();
$return['links'][] = $link->toArray();
}
}

Expand Down
6 changes: 3 additions & 3 deletions CollectionJson/Collection/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Template implements ArrayConvertible

/**
* Constructor
*
*
* @param array $setWithData
*/
public function __construct(array $setWithData = array())
Expand Down Expand Up @@ -65,11 +65,11 @@ public function getData()
}

/** @return array */
public function __toArray()
public function toArray()
{
$return = array('template' => array('data' => array()));
foreach ($this->getData() as $data) {
$return['template']['data'][] = $data->__toArray();
$return['template']['data'][] = $data->toArray();
}
return $return;
}
Expand Down
2 changes: 1 addition & 1 deletion CollectionJson/Interfaces/ArrayConvertible.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@

interface ArrayConvertible
{
public function __toArray();
public function toArray();
}
2 changes: 1 addition & 1 deletion CollectionJson/Property/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function getValue()
}

/** @return array */
public function __toArray()
public function toArray()
{
$required = array('name' => $this->getName());
return $required + array_filter(
Expand Down
2 changes: 1 addition & 1 deletion CollectionJson/Property/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function getRender()
}

/** @return array */
public function __toArray()
public function toArray()
{
$required = array('href' => $this->getHref(), 'rel' => $this->getRel());
return $required + array_filter(
Expand Down
4 changes: 2 additions & 2 deletions CollectionJson/Property/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,14 @@ public function getData()
}

/** @return array */
public function __toArray()
public function toArray()
{
$required = array('href' => $this->getHref(), 'rel' => $this->getRel());

if (count($this->data)) {
$required['data'] = array();
foreach ($this->data as $data) {
$required['data'][] = $data->__toArray();
$required['data'][] = $data->toArray();
}
}

Expand Down
17 changes: 17 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM php:cli

RUN mkdir /opt/collection-json.php

VOLUME ["/opt/collection-json.php"]

WORKDIR /opt/collection-json.php

ENV PATH $PATH:/opt/vendor/bin

RUN cd .. \
&& php -r "readfile('https://getcomposer.org/installer');" | php \
&& apt-get update && apt-get install -y zlib1g-dev git \
&& docker-php-ext-install zip \
&& pecl install xdebug \
&& echo "zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20131226/xdebug.so" > /usr/local/etc/php/conf.d/xdebug.ini \
&& php composer.phar require --prefer-source phpunit/phpunit zerkalica/phpcs:dev-master
27 changes: 23 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ PHP implementation of the Collection+JSON [specification](http://amundsen.com/me
Examples of media type in use [can be found here](http://amundsen.com/media-types/collection/examples/).

## Example
More examples are located in the ```CollectioTest.php``` test file
More examples are located in the ```CollectionTest.php``` test file

```php
use \CollectionJson\Collection;
Expand All @@ -26,17 +26,36 @@ $collection->addItem($item);
echo $collection;
```

## Signatures
```php
\CollectionJson\Collection($href);

\CollectionJson\Collection\Item($href, array $setWithData = array(), array $setWithLinks = array());
\CollectionJson\Collection\Template(array $setWithData = array());
\CollectionJson\Collection\Error($title = null, $code = null, $message = null);

\CollectionJson\Property\Data($name, $value = null, $prompt = null);
\CollectionJson\Property\Link($href, $rel, $name = null, $render = null, $prompt = null);
\CollectionJson\Property\Query($href, $rel, $name = null, $prompt = null, array $data = array());
```

## Docker
```sh
[sudo] docker build .
[sudo] docker run -v "`pwd`":/opt/collection-json.php IMAGE_ID phpcs --standard=psr2 CollectionJson
[sudo] docker run -v "`pwd`":/opt/collection-json.php IMAGE_ID phpunit -c tests/phpunit.xml tests
```

## Tests
```sh
phpcs --standard=psr2 CollectionJson
cd tests
phpunit ./
phpunit -c tests/phpunit.xml tests
```

## License
The MIT License (MIT)

Copyright (c) 2013 Kanstantsin Kamkou
Copyright (c) 2013-2015 Kanstantsin Kamkou

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kkamkou/collection-json.php",
"version": "1.0.0",
"version": "1.1.0",
"description": "PHP implementation of the Collection+JSON",
"type": "library",
"homepage": "http://github.com/kkamkou/collection-json.php",
Expand All @@ -18,7 +18,8 @@
}
],
"require": {
"php": ">=5.3"
"php": ">=5.3",
"ext-json": "*"
},
"autoload": {
"psr-0": {
Expand Down
4 changes: 2 additions & 2 deletions tests/Collection/ErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function testConstructor()
public function testToArrayWithoutNulls()
{
$obj = new CollectionJson\Collection\Error('not found', 404, 'Example message');
$result = $obj->__toArray();
$result = $obj->toArray();
foreach (array('code', 'message', 'title') as $key) {
$this->assertArrayHasKey($key, $result);
$this->assertTrue(is_string($result[$key]));
Expand All @@ -25,7 +25,7 @@ public function testToArrayWithoutNulls()
public function testToArrayWithNulls()
{
$obj = new CollectionJson\Property\Data('fieldName');
$result = $obj->__toArray();
$result = $obj->toArray();
foreach (array('code', 'message') as $key) {
$this->assertFalse(array_key_exists($key, $result));
}
Expand Down
32 changes: 30 additions & 2 deletions tests/CollectioTest.php → tests/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,34 @@

class CollectionTest extends PHPUnit_Framework_TestCase
{
public function testChangeVersion()
{
$collection = new Collection('http://example.org/friends/');
$collection->setVersion('2.0');

$this->assertSame(
'{"collection":{"version":"2.0","href":"http:\/\/example.org\/friends\/"}}',
(string)$collection
);
}

public function testAddMultipleQueries()
{
$query = new Property\Query('http://example.org/friends/search', 'search', null, 'Search');
$query->addData(new Property\Data('search'));

$query2 = clone $query;

$collection = new Collection('http://example.org/friends/');
$collection->addQuerySet([$query, $query2]);

$result = json_decode($collection, true);
$this->assertEquals(
$result['collection']['queries'],
[$query->toArray(), $query2->toArray()]
);
}

public function testFixtureMinimal()
{
$collection = new Collection('http://example.org/friends/');
Expand Down Expand Up @@ -149,7 +177,7 @@ public function testError()
$collection = new Collection('http://example.com');
$collection->setError($error);
$output = json_decode($collection, true);
$this->assertEquals($output['collection']['error'], $error->__toArray());
$this->assertEquals($output['collection']['error'], $error->toArray());

$collection->setError(new Collection\Error('An error'));
$output = json_decode($collection, true);
Expand All @@ -160,6 +188,6 @@ protected function compareWithFixture($mixed, $name)
{
$json = file_get_contents(__DIR__ . "/_fixtures/{$name}.json");
$json = json_decode($json, true);
$this->assertEquals($json, $mixed->__toArray());
$this->assertEquals($json, $mixed->toArray());
}
}
4 changes: 2 additions & 2 deletions tests/Property/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testConstructor()
public function testToArrayWithoutNulls()
{
$obj = new CollectionJson\Property\Data('fieldName', 'value', 'Test value');
$result = $obj->__toArray();
$result = $obj->toArray();
foreach (array('name', 'value', 'prompt') as $key) {
$this->assertArrayHasKey($key, $result);
}
Expand All @@ -30,7 +30,7 @@ public function testToArrayWithoutNulls()
public function testToArrayWithNulls()
{
$obj = new CollectionJson\Property\Data('fieldName');
$result = $obj->__toArray();
$result = $obj->toArray();
foreach (array('prompt') as $key) {
$this->assertFalse(array_key_exists($key, $result));
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Property/LinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function testToArrayWithoutNulls()
$obj = new CollectionJson\Property\Link(
'http://example.com', 'homepage', 'My Homepage', 'link', 'Link to the homepage'
);
$result = $obj->__toArray();
$result = $obj->toArray();
foreach (array('href', 'rel', 'name', 'render', 'prompt') as $key) {
$this->assertArrayHasKey($key, $result);
}
Expand All @@ -51,7 +51,7 @@ public function testToArrayWithoutNulls()
public function testToArrayWithNulls()
{
$obj = new CollectionJson\Property\Link('http://example.com', 'homepage');
$result = $obj->__toArray();
$result = $obj->toArray();
foreach (array('name', 'render', 'prompt') as $key) {
$this->assertFalse(array_key_exists($key, $result));
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Property/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function testToArrayWithoutNulls()
'http://example.com/search', 'search', 'Search', 'Search page', array($data)
);

$result = $obj->__toArray();
$result = $obj->toArray();
foreach (array('href', 'rel', 'name', 'data', 'prompt') as $key) {
$this->assertArrayHasKey($key, $result);
}
Expand All @@ -39,7 +39,7 @@ public function testToArrayWithoutNulls()
public function testToArrayWithNulls()
{
$obj = new CollectionJson\Property\Query('http://example.com', 'homepage');
$result = $obj->__toArray();
$result = $obj->toArray();
foreach (array('name', 'data', 'prompt') as $key) {
$this->assertFalse(array_key_exists($key, $result));
}
Expand Down

0 comments on commit 1441777

Please sign in to comment.