1. Go to this page and download the library: Download saritasa/transformers 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 AnotherTransformerWrapper implements IDataTransformer
{
public function __construct(IDataTransformer $nestedTransformer) { ... }
}
class User extends \Illuminate\Database\Eloquent\Model {
// "full_name" is a property calculated from first_name and last_name
protected $visible = ['full_name', 'created_at'];
protected $hidden = ['email', 'password'];
protected $dates = ['created_at', 'updated_at', 'birthday'];
}
class UserController extends BaseApiController {
public function myProfile(): \Dingo\Api\Http\Response {
$user = $this->user(); // Returns Eloquent model
return $this->response->item($user, new BaseTransformer);
// Output will be JSON
// { "full_name": "Ivan Ivanov", "created_at": "2017-04-12T23:20:50.52Z" }
}
}
$user = User::find($userId);
class User extends \Illuminate\Database\Eloquent\Model {
// "full_name" is a property calculated from first_name and last_name
protected $visible = ['full_name', 'created_at'];
protected $hidden = ['email', 'password'];
protected $dates = ['created_at', 'updated_at', 'birthday'];
}
class UserController extends BaseApiController {
public function myProfile(): \Dingo\Api\Http\Response {
$user = $this->user(); // Returns Eloquent model
$profileTransformer = new ObjectFieldsTransformer('first_name', 'last_name', 'email', 'birthday');
return $this->response->item($user, $profileTransformer);
// Output will be JSON
// { "first_name": "Ivan", "last_name": "Ivanov", "email": "[email protected]", "birthday": "1985-04-12T00:00:00.00Z" }
}
}
$user = User::find($userId);
class UserProfileTransformer extends CombineTransformer
{
public function __construct()
{
parent::__construct(
new PreloadUserAvatarTransformer(),
new PreloadUserSettingsTransformer()
);
}
}
class User extends \Illuminate\Database\Eloquent\Model {
protected $visible = ['full_name', 'created_at'];
protected $hidden = ['email', 'password'];
protected $dates = ['created_at', 'updated_at', 'birthday'];
}
class UserController extends BaseApiController {
public function myProfile(): \Dingo\Api\Http\Response {
$user = $this->user(); // Returns Eloquent model
$publicProfileTransformer = new LimitFieldsTransformer('full_name', 'birthday');
return $this->response->item($user, new BaseTransformer);
// Output will be JSON
// { "full_name": "Ivan Ivanov" }
}
}
$user = User::find($userId);
function transform(Arrayable $data) {
if (!$data->author) {
new TransformException($this, "Author may not be empty");
}
// ...
}
class UserTransformer extends BaseTransformer {
public function transform(Arrayable $model) {
if (!$model instanceof User) {
throw new TransformTypeMismatchException($this, User::class, get_class($model));
}
return transformUser($model);
}
private function transformUser(User $user) {
... // Handle strong-typed model
}
}
vendor/bin/phpcs
vendor/bin/phpcbf
vendor/bin/phpunit
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.