PHP code example of timehunter / laravel-dto-generator

1. Go to this page and download the library: Download timehunter/laravel-dto-generator 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/ */

    

timehunter / laravel-dto-generator example snippets


        'post'     => (object)[
            'author'    => (object)[
                'id'         => 1,
                'note'       => null,
                'rating'     => 4.6,
                'first_name' => '',
                'last_name'  => '',
            ],
            'comment'   => (object)[
                'comment_by' => (object)[
                    'is_active'  => false,
                    'first_name' => '',
                    'last_name'  => ''
                ],
                'content'    => ''
            ],
            'followers' => (object)[
                'id'             => 1,
                'follower_users' => (array)[
                    'first_name' => '',
                    'last_name'  => ''
                ]
            ],
            'views'     => 123,
            'text'      => '',
            'date'      => '2019-01-01'
        ],
        'feedback' => (object)[
            'comment' => ''
        ]

    $result = Followers::create()->setId(1)
            ->addFollowerUsers(
                FollowerUsers::create()
                    ->setFirstName('ss')
                    ->setLastName('dd')
            )
            ->addFollowerUsers(
                FollowerUsers::create()
                    ->setFirstName('ss')
                    ->setLastName('dd')
            );
        var_dump($result->toArray());



namespace App;

class User
{
	/** @var bool $isActive */
	private $isActive;

	/** @var string $firstName */
	private $firstName;

	/** @var string $lastName */
	private $lastName;


	/**
	 * @return User
	 */
	public static function create()
	{
		return new self;
	}


	/**
	 * @return bool
	 */
	public function getIsActive(): bool
	{
		return $this->isActive;
	}


	/**
	 * @param bool $isActive
	 * @return $this
	 */
	public function setIsActive(bool $isActive): self
	{
		$this->isActive = $isActive;
		return $this;
	}


	/**
	 * @return string
	 */
	public function getFirstName(): string
	{
		return $this->firstName;
	}


	/**
	 * @param string $firstName
	 * @return $this
	 */
	public function setFirstName(string $firstName): self
	{
		$this->firstName = $firstName;
		return $this;
	}


	/**
	 * @return string
	 */
	public function getLastName(): string
	{
		return $this->lastName;
	}


	/**
	 * @param string $lastName
	 * @return $this
	 */
	public function setLastName(string $lastName): self
	{
		$this->lastName = $lastName;
		return $this;
	}


	/**
	 * @return array
	 */
	public function toArray(): array
	{
		return [
		   'is_active' => $this->isActive,
		   'first_name' => $this->firstName,
		   'last_name' => $this->lastName,
		];
	}
}



namespace App;

class Comment
{
	/** @var User $user */
	private $user;

	/** @var string $content */
	private $content;


	/**
	 * @return Comment
	 */
	public static function create()
	{
		return new self;
	}


	/**
	 * @param user $user
	 * @return $this
	 */
	public function addUser(User $user): self
	{
		$this->user = $user;
		return $this;
	}


	/**
	 * @return User
	 */
	public function getUser(): User
	{
		return $this->user;
	}


	/**
	 * @param User $user
	 * @return $this
	 */
	public function setUser(User $user): self
	{
		$this->user = $user;
		return $this;
	}


	/**
	 * @return string
	 */
	public function getContent(): string
	{
		return $this->content;
	}


	/**
	 * @param string $content
	 * @return $this
	 */
	public function setContent(string $content): self
	{
		$this->content = $content;
		return $this;
	}


	/**
	 * @return array
	 */
	public function toArray(): array
	{
		return [
		   'user' => $this->user->toArray(),
		   'content' => $this->content,
		];
	}
}



namespace App;

class Author
{
	/** @var int $id */
	private $id;

	/** @var  $note */
	private $note;

	/** @var float $rating */
	private $rating;

	/** @var string $firstName */
	private $firstName;

	/** @var string $lastName */
	private $lastName;


	/**
	 * @return Author
	 */
	public static function create()
	{
		return new self;
	}


	/**
	 * @return int
	 */
	public function getId(): int
	{
		return $this->id;
	}


	/**
	 * @param int $id
	 * @return $this
	 */
	public function setId(int $id): self
	{
		$this->id = $id;
		return $this;
	}


	/**
	 * @return mixed
	 */
	public function getNote()
	{
		return $this->note;
	}


	/**
	 * @param  $note
	 * @return $this
	 */
	public function setNote($note): self
	{
		$this->note = $note;
		return $this;
	}


	/**
	 * @return float
	 */
	public function getRating(): float
	{
		return $this->rating;
	}


	/**
	 * @param float $rating
	 * @return $this
	 */
	public function setRating(float $rating): self
	{
		$this->rating = $rating;
		return $this;
	}


	/**
	 * @return string
	 */
	public function getFirstName(): string
	{
		return $this->firstName;
	}


	/**
	 * @param string $firstName
	 * @return $this
	 */
	public function setFirstName(string $firstName): self
	{
		$this->firstName = $firstName;
		return $this;
	}


	/**
	 * @return string
	 */
	public function getLastName(): string
	{
		return $this->lastName;
	}


	/**
	 * @param string $lastName
	 * @return $this
	 */
	public function setLastName(string $lastName): self
	{
		$this->lastName = $lastName;
		return $this;
	}


	/**
	 * @return array
	 */
	public function toArray(): array
	{
		return [
		   'id' => $this->id,
		   'note' => $this->note,
		   'rating' => $this->rating,
		   'first_name' => $this->firstName,
		   'last_name' => $this->lastName,
		];
	}
}


namespace App;

class Followers
{
	/** @var int $id */
	private $id;

	/** @var FollowerUsers[] $followerUsers */
	private $followerUsers = [];


	/**
	 * @return Followers
	 */
	public static function create()
	{
		return new self;
	}


	/**
	 * @return int
	 */
	public function getId(): int
	{
		return $this->id;
	}


	/**
	 * @param int $id
	 * @return $this
	 */
	public function setId(int $id): self
	{
		$this->id = $id;
		return $this;
	}


	/**
	 * @param followerUsers $followerUsers
	 * @return $this
	 */
	public function addFollowerUsers(FollowerUsers $followerUsers): self
	{
		$this->followerUsers[] = $followerUsers;
		return $this;
	}


	/**
	 * @return FollowerUsers[]
	 */
	public function getFollowerUsers()
	{
		return $this->followerUsers;
	}


	/**
	 * @param FollowerUsers[] $followerUsers
	 * @return $this
	 */
	public function setFollowerUsers($followerUsers): self
	{
		$this->followerUsers = $followerUsers;
		return $this;
	}


	/**
	 * @return array
	 */
	public function toArray(): array
	{
		return [
		   'id' => $this->id,
		   'follower_users' => array_map(function (FollowerUsers $data){
		          return $data->toArray();
		      }, $this->followerUsers),
		];
	}
}


namespace App;

class FollowerUsers
{
	/** @var string $firstName */
	private $firstName;

	/** @var string $lastName */
	private $lastName;


	/**
	 * @return FollowerUsers
	 */
	public static function create()
	{
		return new self;
	}


	/**
	 * @return string
	 */
	public function getFirstName(): string
	{
		return $this->firstName;
	}


	/**
	 * @param string $firstName
	 * @return $this
	 */
	public function setFirstName(string $firstName): self
	{
		$this->firstName = $firstName;
		return $this;
	}


	/**
	 * @return string
	 */
	public function getLastName(): string
	{
		return $this->lastName;
	}


	/**
	 * @param string $lastName
	 * @return $this
	 */
	public function setLastName(string $lastName): self
	{
		$this->lastName = $lastName;
		return $this;
	}


	/**
	 * @return array
	 */
	public function toArray(): array
	{
		return [
		   'first_name' => $this->firstName,
		   'last_name' => $this->lastName,
		];
	}
}

`bash
php artisan vendor:publish --provider="TimeHunter\LaravelDTOGenerator\LaravelDTOGeneratorServiceProvider"
`bash
php artisan make:dto