Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mhndev/hal
Php Hal Object creation and manipulation from array data
https://github.com/mhndev/hal
hal http php
Last synced: 16 days ago
JSON representation
Php Hal Object creation and manipulation from array data
- Host: GitHub
- URL: https://github.com/mhndev/hal
- Owner: mhndev
- Created: 2017-11-12T11:53:03.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2017-11-20T08:00:31.000Z (about 7 years ago)
- Last Synced: 2024-12-02T15:55:19.863Z (about 1 month ago)
- Topics: hal, http, php
- Language: PHP
- Homepage:
- Size: 35.2 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[![Build Status](https://travis-ci.org/mhndev/hal.svg?branch=master)](https://travis-ci.org/mhndev/hal)
[![Latest Stable Version](https://poser.pugx.org/mhndev/hal/v/stable)](https://packagist.org/packages/mhndev/hal)
[![Total Downloads](https://poser.pugx.org/mhndev/hal/downloads)](https://packagist.org/packages/mhndev/hal)
[![Latest Unstable Version](https://poser.pugx.org/mhndev/hal/v/unstable)](https://packagist.org/packages/mhndev/hal)
[![License](https://poser.pugx.org/mhndev/hal/license)](https://packagist.org/packages/mhndev/hal)
[![composer.lock](https://poser.pugx.org/mhndev/hal/composerlock)](https://packagist.org/packages/mhndev/hal)### Php Hal Object
generating php hal object from array data
this package currently just supports json and not xml
#### Sample usage:
```php
$post = [
'title' => 'sample post title',
'text' => 'post body goes here ...',
];$user = [
'username' => 'mhndev',
'mobile' => '09124917706',
'email' => '[email protected]'
];$comments = [
[
'text' => 'Hi',
'uid' => 12
],
[
'text' => 'OK',
'uid' => 14
],
[
'text' => 'NOK',
'uid' => 10
]
];$tags = [
'tag1', 'tag2', 'tag3'
];$profile = [
'avatar' => 'http://google.com/inja.jpeg',
'name' => 'majid',
'username' => 'mhndev',
'bio' => 'user biography goes here ...'
];$self_link = new \mhndev\hal\Link('self', 'http://google.com');
$next_link = new \mhndev\hal\Link('next', 'http://google.com');$postResource = new \mhndev\hal\Resource($post);
$profileResource = new \mhndev\hal\Resource($profile);
$profileResource->addLink($self_link);$userResource = new \mhndev\hal\Resource($user);
$userResource->addEmbeddedResource($profileResource, 'profile');
$tagsResource = new \mhndev\hal\Resource($tags);
$request = \Slim\Http\Request::createFromGlobals([]);
$commentsResource = new \mhndev\hal\Paginated($comments, 5 , 10, $request);
$postResource->addEmbeddedResource($userResource, 'user');
$postResource->addEmbeddedResource($tagsResource, 'tags');
$postResource->addEmbeddedResource($commentsResource, 'comments');$postResource->addLink($next_link);
$postResource->addLink($self_link);header('Content-Type: application/json');
$presenter = new \mhndev\hal\Presenter($postResource);
var_dump((new \mhndev\hal\Presenter($postResource))->asArray());die();
echo $presenter->asJson();
```