PHP code example of tychovbh / laravel-resource-mapper
1. Go to this page and download the library: Download tychovbh/laravel-resource-mapper 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/ */
tychovbh / laravel-resource-mapper example snippets
use Tychovbh\ResourceMapper\RawResource;
// Let's say i'm retrieving a User, Company and Person from an api:
return [
'user' => [
// this will map ['RawEmail' => '[email protected]'] to ['email' => '[email protected]']
'email' => 'RawEmail'
]
'company' => [
// You can map recursive resource value as well via dot notation
// this will map: ['company' => ['name' => 'Bespoke Web']] to ['company' => 'Bespoke Web']
'company' => 'company.name'
]
'person' => [
// You can also set a callback, then a RawResource object will be available.
// You can use RawResource to access all resource values and map is as you like.
// this will map ['RawFirstname' => 'john'] to ['firstname' => 'John']
'firstname' => function (RawResource $resource) {
return ucfirst($resource->get('RawFirstname'));
}
// this will map ['firstname' => 'John', 'suffix' => 'v.', 'lastname' => 'Doe'] to ['fullname' => 'John v. Doe']
'fullname' => function (RawResource $resource) {
return $resource->join(' ', 'firstname', 'suffix', 'lastname');
}
]
// You can also do some recursive array mapping
'company' => [
'user' => function (RawResource $resource) {
return $this->config('user')->map($resource->get('RawCompany.RawUser'));
}
],
'users' => [
'items' => function (RawResource $resource) {
return $this->config('user')->mapCollection($resource->get('RawItems'));
}
]
];
$RawResource = new RawResource([
'title' => 'my new website',
'firstname' => 'John',
'prefix' => 'v',
'lastname' => 'Doe',
'company' => [
'name' => 'Bespoke Web'
]
])
// Get value from array
$title = $RawResource->get('title')
echo $title; // my new website
// Get recursive value from array
$company = $RawResource->get('company.name')
echo $company; // Bespoke Web
// Join values from array with a delimiter
$title = $RawResource->join(' ', 'firstname', 'prefix, 'lastname')
echo $title; // John v Doe
php
use Tychovbh\ResourceMapper\ResourceMapper;
// Use class injection
Route::get('/user', function(ResourceMapper $mapper) {
$res = $guzzleClient->request('GET', 'https://some-api.com/user');
$result = json_decode($res->getBody()->getContents(), true);
return $mapper->config('user')->map($result);
});
// Or use Laravel helper app()
Route::get('/user', function() {
$mapper = app('resource-mapper');
$res = $guzzleClient->request('GET', 'https://some-api.com/user');
$result = json_decode($res->getBody()->getContents(), true);
return $mapper->config('user')->map($result);
});
php
// Map via config with $RawResource as array
$mapped = $mapper->config('user')->map($RawResource)
// Map via config with $RawResource as json
$mapped = $mapper->config('user')->mapJson($RawResource)
// Skip config file and use custom mapping
$mapped = $mapper->mapping([
'title' => 'RawTitle'
])->map($RawResource)
// Map via config with a $RawResource as a Collection of items
$mapped = $mapper->config('user')->mapCollection([
[
'RawFistname' => 'John',
],
[
'RawFistname' => 'Alex',
],
])
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.