1. Go to this page and download the library: Download php-grape/grape-entity library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
class AEntity extends Entity
{
use EntityTrait;
// `initialized` will be called automatically when needed :)
private static function initialize()
{
self::extends(BEntity::class, CEntity::class, DEntity::class);
...
}
}
self::expose('ip', ['if' => ['type' => 'full']]);
// Exposed if the function evaluates to true
self::expose('ip', ['if' => function($instance, $options) {
return isset($options['type']) && $options['type'] === 'full';
}]);
// Exposed if 'type' is set in the options array and is truthy
self::expose('ip', ['if' => 'type']);
// Exposed if $options['type'] is set and equals 'full'
self::expose('ip', ['if' => ['type' => 'full']]);
// `collection_name` is optional and defaults to `items`
self::presentCollection(true, 'collection_name');
self::expose('collection_name', ['using' => ItemEntity::class]);
self::expose('status', ['merge' => function($key, $oldVal, $newVal) {
// You don't need to check if $oldVal is set here
return $oldVal && $newVal ? $oldVal + $newVal : null;
}]);
class ExampleEntity extends Entity
{
use EntityTrait;
private static function initialize()
{
self::expose('attr_not_on_wrapped_object');
}
private function attr_not_on_wrapped_object()
{
return 42;
}
}
class ExampleEntity extends Entity
{
use EntityTrait;
private static function initialize()
{
self::expose('formatted_value');
}
private function formatted_value()
{
return "+ X {$this->object->value} {$this->options['y']}"
}
}
class UserEntity extends Entity
{
use EntityTrait;
private static function initialize()
{
self::expose('name');
self::expose('address1');
self::expose('address2');
self::expose('address_state');
self::expose('address_city');
self::expose('email');
self::expose('phone');
}
}
class MailingEntity extends Entity
{
use EntityTrait;
private static function initialize()
{
self::extends(UserEntity::class);
self::unexpose('email');
self::unexpose('phone');
}
}
class UserEntity extends Entity
{
use EntityTrait;
private static function initialize()
{
self::expose('name');
}
}
class EmployeeEntity extends Entity
{
use EntityTrait;
private static function initialize()
{
self::extends(UserEntity::class);
self::expose('name', ['as' => 'employe_name', 'override' => true]);
}
}
class UserEntity extends Entity
{
use EntityTrait;
private static function initialize()
{
self::expose('id');
self::expose('name');
self::expose('email');
}
}
class ExampleEntity extends Entity
{
use EntityTrait;
private static function initialize()
{
self::expose('id');
self::expose('title');
self::expose('user', ['using' => UserEntity::class]);
}
}
$data = ExampleEntity::represent($model, [
'only' => ['title', ['user' => ['name', 'email']]]
]);
json_encode($data);
class MyEntity extends Entity
{
use EntityTrait;
private static function initialize()
{
self::formatWith('iso_timestamp', function($dt) {
return $dt->format(DateTime::ISO8601);
});
self::withOptions(['format_with' => 'iso_timestamp'], function() {
self::expose('created_at');
self::expose('updated_at');
});
}
}
Entity::formatWith('utc', function($dt) {
return $dt->setTimezone(new DateTimeZone('UTC'));
});
class AnotherEntity extends Entity
{
use EntityTrait;
private static function initialize()
{
self::expose('created_at', ['format_with' => 'utc']);
}
}
[
'name' => null,
'age' => 100
]
class MyEntity extends Entity
{
use EntityTrait;
private static function initialize()
{
self::expose('name', ['expose_null' => false]);
self::expose('age', ['expose_null' => false]);
}
}
class MyEntity extends Entity
{
use EntityTrait;
private static function initialize()
{
self::expose('name', ['expose_null' => false]);
// since expose_null is omitted null values will be rendered
self::expose('age');
}
}
class MyEntity extends Entity
{
use EntityTrait;
private static function initialize()
{
// None of the exposures in the withOptions closure will render null values
self::withOptions(['expose_null' => false], function() {
self::expose('name');
self::expose('age');
});
}
}
class MyEntity extends Entity
{
use EntityTrait;
private static function initialize()
{
// None of the exposures in the withOptions closure will render null values
self::withOptions(['expose_null' => false], function() {
self::expose('name');
// null values would be rendered in the JSON
self::expose('age', ['expose_null' => true]);
});
}
}
class MyEntity extends Entity
{
use EntityTrait;
private static function initialize()
{
self::expose('name', ['default' => '']);
self::expose('age', ['default' => 60]);
}
}
// api/ContactEntity.php
self::expose('contact_info', function() {
self::expose('phone');
self::expose('address', function($instance, $options) {
// use `array_merge` to extend options and then pass the new version of options to the nested entity
$options = array_merge(['full_format' => $instance->needFullFormat()], $options);
return AddressEntity::represent($instance->address, $options);
});
self::expose('email', ['if' => ['type' => 'full']]);
}
// api/AddressEntity.php
// the new option could be retrieved in options array for conditional exposure
self::expose('state', ['if' => 'full_format']);
self::expose('city', ['if' => 'full_format']);
self::expose('street', function($instance, $options) {
// the new option could be retrieved in options hash for runtime exposure
return $options['full_format'] ? $instance->full_street_name : $instance->simple_street_name;
});
class MyEntity extends Entity
{
use EntityTrait;
private static function initialize()
{
self::expose('user'); // path is ['user']
self::expose('foo', ['as' => 'bar']); // path is ['bar']
self::expose('a', function() {
self::expose('b', ['as' => 'xx'], function() {
self::expose('c'); // path is ['a', 'xx', 'c']
});
});
}
}
class ApiController
{
use UserHelpers;
public function statuses()
{
$statuses = Status::all();
$type = $this->user()->isAdmin() ? 'full' : 'default';
$representation = StatusEntity::represent($statuses, ['type' => $type]);
// PhpGrape\Entity implements JsonSerializable
return response()->json($representation, 200);
}
}
// Entity used is binded to $this
// so you have access to $this->object and $this->options
Entity::setPropValueAdapter('MyAdapter', [
'condition' => function ($prop, $safe) {
$model = 'MyProject\MyModel';
return $this->object instanceof $model;
},
'getPropValue' => function ($prop, $safe, $cache) {
$class = get_class($this->object);
// you can use $cache to speed up future lookups
if ($cache('get', $class, $prop)) return $this->object->{$prop};
if ($this->object->hasAttribute($prop)) {
$cache('set', $class, $prop);
return $this->object->{$prop};
}
// Prop not found
return $this->handleMissingProperty($prop, $safe);
}
]);
Entity::setPropValueAdapter('MyAdapter', null);
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.