PHP code example of larapie / data-transfer-object
1. Go to this page and download the library: Download larapie/data-transfer-object 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/ */
larapie / data-transfer-object example snippets
public function handleRequest(array $dataFromRequest)
{
$dataFromRequest[/* what to do now?? */];
}
public function create(PostData $data, User $user)
{
$data->with('user_id', $user->id);
return $this->repository->create($data->toArray());
}
public function create(PostData $data, User $user)
{
if($this->user->isAdmin()){
$data->override('name', 'admin');
}
$data->with('user_id', $user->id);
return $this->repository->create($data->toArray());
}
class PostData extends DataTransferObject
{
/**
* @var string
* @Assert\NotBlank()
* @Assert\Length(min = 3, max = 20)
*/
public $name;
}
class UpdatePostData extends PostData
{
/**
* @Optional
* @Inherit
*/
public $name;
}
use \Spatie\DataTransferObject\DataTransferObjectCollection;
class PostCollection extends DataTransferObjectCollection
{
public function current(): PostData
{
return parent::current();
}
}
foreach ($postCollection as $postData) {
$postData-> // … your IDE will provide autocompletion.
}
$postCollection[0]-> // … and also here.
class PostCollection extends DataTransferObjectCollection
{
public static function create(array $data): PostCollection
{
$collection = [];
foreach ($data as $item)
{
$collection[] = PostData::create($item);
}
return new self($collection);
}
}
class PostData extends DataTransferObject
{
/** @var \AuthorData */
public $author;
}