Skip to content

Commit a22ad80

Browse files
committed
Merge branch 'master' of github.com:jjware/PhpCollectionJson
2 parents 2af92ba + f5bcb4b commit a22ad80

File tree

1 file changed

+68
-1
lines changed

1 file changed

+68
-1
lines changed

README.md

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,71 @@
11
# PhpCollectionJson
22
PHP classes for building a Collection+JSON response.
33

4-
Soon to come - A simple method for deserializing Collection+JSON responses into these classes.
4+
## Installation
5+
```
6+
composer require jjware/phpcollectionjson
7+
```
8+
9+
## Usage
10+
### Creating a response
11+
```php
12+
use PhpCollectionJson\Document;
13+
use PhpCollectionJson\Collection;
14+
use PhpCollectionJson\Item;
15+
use PhpCollectionJson\Data;
16+
17+
$document = new Document();
18+
$collection = new Collection('http://www.somesite.com/users');
19+
$document->setCollection($collection);
20+
21+
$item = new Item('http://www.somesite.com/users/123');
22+
$item->getData()
23+
->add(new Data('firstName', 'John'))
24+
->add(new Data('lastName', 'Smith'))
25+
->add(new Data('username', 'jsmith'));
26+
27+
$collection->getItems()->add($item);
28+
29+
echo json_encode($document);
30+
```
31+
### Building from a response
32+
#### The JSON
33+
```json
34+
{
35+
"collection": {
36+
"version": "1.0",
37+
"href": "http://www.somesite.com/users",
38+
"items": [
39+
{
40+
"href": "http://www.somesite.com/users/123",
41+
"data": [
42+
{
43+
"name": "firstName",
44+
"value": "John"
45+
},
46+
{
47+
"name": "lastName",
48+
"value": "Smith"
49+
},
50+
{
51+
"name": "username",
52+
"value": "jsmith"
53+
}
54+
]
55+
}
56+
]
57+
}
58+
}
59+
```
60+
#### The PHP
61+
```php
62+
use PhpCollectionJson\Document;
63+
64+
$json = file_get_contents('http://www.somesite.com/users'); // don't do this at home kids
65+
66+
$document = Document::fromJSON($json);
67+
68+
$firstName = $document->getCollection()->getItems()->elementAt(0)->getData()->elementAt(0)->getValue();
69+
70+
// $firstName === 'John'
71+
```

0 commit comments

Comments
 (0)