Skip to content

Commit

Permalink
Merge pull request #15 from mickaelvieira/2.0.1
Browse files Browse the repository at this point in the history
2.0.1
  • Loading branch information
mickaelvieira committed Oct 17, 2017
2 parents cde664a + c318728 commit 00bc1de
Show file tree
Hide file tree
Showing 44 changed files with 670 additions and 492 deletions.
11 changes: 7 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
sudo: false # use container-based-infrastructure

env:
global:
- MAKEFLAGS="-j 2"

language: php

php: [7.0, 7.1]
Expand All @@ -10,14 +14,13 @@ install:

script:
- mkdir -p build/logs
- php bin/phpcs --standard=PSR2 ./src/ --report=full
- php bin/phpspec run --format=pretty -v
- php bin/test-examples
- make lint
- make test
- make examples

after_success:
- travis_retry php bin/coveralls -v

cache:
directories:
- $HOME/.composer/cache

14 changes: 10 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,33 @@
```sh
$ git clone git@github.com:mickaelvieira/CollectionJson.git
$ cd CollectionJson
$ composer install
$ make
```

## Run the test

The test suite has been written with [PHPSpec](http://phpspec.net/)

```sh
$ ./bin/phpspec run --format=pretty
$ make test
```

## PHP Code Sniffer

This project follows the coding style guide [PSR1](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-1-basic-coding-standard.md) and [PSR2](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)

```sh
$ ./bin/phpcs --standard=PSR2 ./src/
$ make lint
```

To fix PHP Code Sniffer issues

```sh
$ make fmt
```

## Run the examples (see `./examples`)

```sh
$ ./bin/test-examples
$ make examples
```
32 changes: 32 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
OS := $(shell uname -s)
PATH := bin:$(PATH)
SHELL := /bin/bash
SOURCE_DIR := src

.PHONY: all clean install lint fmt test examples branch release

all: clean install

clean:
rm -rf vendor/*

install: composer.json composer.lock
composer install

lint:
phpcs --standard=PSR2 --report=full $(SOURCE_DIR)

fmt:
phpcbf --standard=PSR2 $(SOURCE_DIR)

test:
phpspec run --format=pretty -vvv

examples:
test-examples

branch:
create-branch

release:
create-release
60 changes: 46 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# Collection Json

[![Software License](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/mickaelvieira/CollectionJson/blob/master/LICENSE.md)
[![Latest Stable Version](https://img.shields.io/packagist/v/mvieira/collection-json.svg)](https://packagist.org/packages/mvieira/collection-json)
[![Build Status](https://travis-ci.org/mickaelvieira/CollectionJson.svg?branch=master)](https://travis-ci.org/mickaelvieira/CollectionJson)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](https://github.com/mickaelvieira/CollectionJson/blob/master/LICENSE)
[![Coverage Status](https://coveralls.io/repos/github/mickaelvieira/CollectionJson/badge.svg?branch=master)](https://coveralls.io/github/mickaelvieira/CollectionJson?branch=master)

PHP implementation of the Collection+JSON Media Type

Specification:
Specification:
- [http://amundsen.com/media-types/collection/](http://amundsen.com/media-types/collection/)

## Installation
Expand Down Expand Up @@ -34,16 +35,15 @@ Please see [CONTRIBUTING](https://github.com/mickaelvieira/CollectionJson/tree/m

## License

The MIT License (MIT). Please see [License File](https://github.com/mickaelvieira/CollectionJson/tree/master/LICENSE) for more information.
The MIT License (MIT). Please see [License File](https://github.com/mickaelvieira/CollectionJson/tree/master/LICENSE.md) for more information.

## Documentation

### Creating a collection

```php
$collection = (new Collection())
->withItem((new Item())
->withHref('https://example.co/item/1')
->withItem((new Item('https://example.co/item/1'))
->withDataSet([
new Data('data 1'),
new Data('data 2', 'value 2')
Expand Down Expand Up @@ -100,8 +100,7 @@ $data = Data::fromArray([
...or by using the accessors (Note that entities are immutable)

```php
$data = (new Data())
->withName('email')
$data = (new Data('email'))
->withValue('hello@example.co');
```

Expand Down Expand Up @@ -216,7 +215,7 @@ echo json_encode($template);
Examples are available in the directory ```./examples/```, you can execute them on the command line by running:

```sh
$ ./bin/test-example
$ make examples
```

Or separately
Expand All @@ -237,29 +236,29 @@ They allows you to add the corresponding entities to objects that implement them

```php
// this...
$item = (new Item())
$item = (new Item('https://example.co/item/1'))
->withData([
'name' => 'email',
'value' => 'email value'
]);

// ...is similar to
// ...is similar to
$data = Data::fromArray([
'name' => 'email',
'value' => 'email value'
]);

$item = (new Item())
$item = (new Item('https://example.co/item/1'))
->withData($data);

// and that...
$item = (new Item())
$item = (new Item('https://example.co/item/1'))
->withDataSet([
new Data('email', 'hello@example.co'),
new Data('tel', '0000000000')
]);

// ...is similar to
// ...is similar to
$data1 = Data::fromArray([
'name' => 'email',
'value' => 'hello@example.co'
Expand All @@ -268,9 +267,42 @@ $data2 = Data::fromArray([
'name' => 'tel',
'value' => '0000000000'
]);
$item = (new Item())
$item = (new Item('https://example.co/item/1'))
->withDataSet([
$data1,
$data2
]);
```

### Validation

It is now possible to validate the data entering your API by using the [Symfony validator](https://symfony.com/doc/current/components/validator.html).

```php
use CollectionJson\Validator\Dataset as DatasetValidator;
use Symfony\Component\Validator\Constraints;

$constraints = [
'id' => [
new Constraints\NotBlank(),
],
'url' => [
new Constraints\NotBlank(),
new Constraints\Url(),
],
'email' => [
new Constraints\NotBlank(),
new Constraints\Email(),
],
];

$template = (new Template())
->withData(new Data('id', '123'))
->withData(new Data('url', 'http://example.co'))
->withData(new Data('email', 'test@example.co'));

$errors = (new DatasetValidator())
->validate($template->getDataSet(), $constraints);
```

It will return the list of errors.
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@
"phpspec/phpspec": "^3.0",
"squizlabs/php_codesniffer": "^3.0",
"satooshi/php-coveralls": "^1.0",
"leanphp/phpspec-code-coverage": "^3.1"
"leanphp/phpspec-code-coverage": "^3.1",
"symfony/validator": "^3.3"
},
"suggest": {
"symfony/validator": "Adds DataSet validation support"
},
"config": {
"bin-dir": "bin"
Expand All @@ -40,7 +44,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
"dev-master": "2.x-dev"
}
}
}
3 changes: 1 addition & 2 deletions examples/create-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

use CollectionJson\Entity\Data;

$data = (new Data())
->withName('data name')
$data = (new Data('data name'))
->withPrompt('data prompt')
->withValue('data value');

Expand Down
3 changes: 1 addition & 2 deletions examples/create-item.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
use CollectionJson\Entity\Link;
use CollectionJson\Type\Relation;

$item = (new Item())
->withHref('https://example.co/item/1')
$item = (new Item('https://example.co/item/1'))
->withDataSet([
new Data('data 1'),
new Data('data 2', 'value 2')
Expand Down
4 changes: 1 addition & 3 deletions examples/create-link.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
use CollectionJson\Entity\Link;
use CollectionJson\Type;

$link = (new Link())
$link = (new Link('https://example.co', Type\Relation::ITEM))
->withName('link name')
->withHref('https://example.co')
->withPrompt('prompt value')
->withRel(Type\Relation::ITEM)
->withRender(Type\Render::IMAGE); // default Render::LINK

echo json_encode($link, JSON_PRETTY_PRINT);
4 changes: 1 addition & 3 deletions examples/create-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
use CollectionJson\Entity\Data;
use CollectionJson\Type\Relation;

$query = (new Query())
->withHref('https://example.co')
->withRel(Relation::SEARCH)
$query = (new Query('https://example.co', Relation::SEARCH))
->withName('value')
->withPrompt('value')
->withData(new Data('data 1', true))
Expand Down
34 changes: 17 additions & 17 deletions spec/CollectionJson/BagSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ function it_should_empty_by_default()

function it_should_add_an_item_to_the_bag_link_set()
{
$bag = $this->with(new Item());
$bag = $this->with(new Item('http://example.com'));
$this->shouldHaveCount(0);
$bag->shouldHaveCount(1);
}

function it_should_remove_an_item_from_the_bag_link_set()
{
$item = new Item();
$item = new Item('http://example.com');
$bag = $this->with($item);
$this->shouldHaveCount(0);
$bag->shouldHaveCount(1);
Expand All @@ -52,8 +52,8 @@ function it_should_remove_an_item_from_the_bag_link_set()

function it_should_not_blow_up_when_removing_an_unexisting_item_from_the_bag_link_set()
{
$item1 = new Item();
$item2 = new Item();
$item1 = new Item('http://example.com');
$item2 = new Item('http://example.com');
$bag = $this->with($item1);
$this->shouldHaveCount(0);
$bag->shouldHaveCount(1);
Expand All @@ -67,8 +67,8 @@ function it_should_not_blow_up_when_removing_an_unexisting_item_from_the_bag_lin

function it_is_clonable()
{
$item1 = new Item();
$item2 = new Item();
$item1 = new Item('http://example.com');
$item2 = new Item('http://example.com');

$copy = $this->with($item1)->with($item2);

Expand All @@ -83,8 +83,8 @@ function it_is_clonable()
function it_should_throw_an_exception_when_item_is_of_the_wrong_type()
{
$this->shouldThrow(
new \BadMethodCallException('Property [item] must be of type [CollectionJson\Entity\Item]')
)->during('with', [new Query()]);
new \DomainException('Property [item] must be of type [CollectionJson\Entity\Item]')
)->during('with', [new Query('http://example.com', 'item')]);
}

function it_should_build_an_item_from_an_array()
Expand All @@ -98,15 +98,15 @@ function it_should_build_an_item_from_an_array()

function it_should_add_multiple_items_to_the_bag()
{
$bag = $this->withSet([new Item(), ['href' => 'http://example.com']]);
$bag = $this->withSet([new Item('http://example.com'), ['href' => 'http://example.com']]);
$this->shouldHaveCount(0);
$bag->shouldHaveCount(2);
}

function it_should_return_the_set()
{
$item1 = new Item();
$item2 = new Item();
$item1 = new Item('http://example.com');
$item2 = new Item('http://example.com');

$bag = $this->with($item1);
$bag = $bag->with($item2);
Expand All @@ -115,9 +115,9 @@ function it_should_return_the_set()

function it_should_return_the_first_element_in_the_set()
{
$item1 = new Item();
$item2 = new Item();
$item3 = new Item();
$item1 = new Item('http://example.com');
$item2 = new Item('http://example.com');
$item3 = new Item('http://example.com');

$bag = $this->withSet([$item1, $item2, $item3]);

Expand All @@ -132,9 +132,9 @@ function it_should_return_null_when_the_first_element_in_not_the_set()

function it_should_return_the_last_element_in_the_set()
{
$item1 = new Item();
$item2 = new Item();
$item3 = new Item();
$item1 = new Item('http://example.com');
$item2 = new Item('http://example.com');
$item3 = new Item('http://example.com');

$bag = $this->withSet([$item1, $item2, $item3]);

Expand Down
Loading

0 comments on commit 00bc1de

Please sign in to comment.