1. Go to this page and download the library: Download deefour/transformer 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/ */
deefour / transformer example snippets
$input = [
'title' => 'a whole new world',
'price' => '29.95',
'publication_date' => '2010-12-09',
'author' => 'Jason Daly',
];
use Deefour\Transformer\Transformer;
use Carbon\Carbon;
class BookTransformer extends Transformer
{
protected $casts [
'price' => 'float',
];
public function title()
{
return trim(ucwords($this->raw('title')));
}
public function publicationDate()
{
return Carbon::parse($this->raw('publication_date'));
}
}
$transform = new BookTransformer($input);
$transform->get('title'); //=> 'A Whole New World'
$transform->get('price'); //=> 29.95 (cast to a float)
$transform->get('publication_date'); //=> Carbon\Carbon instance
class BookTransformer extends Transformer
{
protected $casts [
'price' => 'float',
];
}
$attributes = [ 'price' => '3.23' ];
$transformer = new BookTransformer($attributes);
$transformer->price; //=> 3.23 (cast to a float)
class BookTransformer extends Transformer
{
/**
* Is the book considered old?
*
* @attribute
* @return string
*/
public function isOld()
{
return $this->publication_date < Carbon::now()->subYears(10);
}
/**
* Is the book nonfiction?
*
* @return boolean
*/
public function internalSlug()
{
return sha1($this->title . (string)$this->publication_date);
}
}
$transform = new BookTransformer([ 'title' => 'A Whole New World' ]);
$transform->get('title'); //=> 'A Whole New World'
$transform->get('is_old') //=> false
$transformer->get('internal_slug') //=> null
$transform->all(); //=> [ 'title' => 'A Whole New World', 'is_old' => false ]