GithubHelp home page GithubHelp logo

ryandadeng / laravel-dto-generator Goto Github PK

View Code? Open in Web Editor NEW
47.0 1.0 2.0 46 KB

A generator that creates PHP Data Transfer Object by array schema.

License: Other

PHP 100.00%
json-to-class json-to-model json-generator class-generator php-class laravel-generator array-to-class array-to-model json-to-class-generator php

laravel-dto-generator's Introduction

Laravel DTO Generator

Latest Version on Packagist Total Downloads

A DTO generator that helps you create a bunch of models instead of repeating copy/paste.

Star me if you find its useful. ^.^

Please update your version to be "~v2.0" and re-publish your config file

Install the package in development dependencies:

Via Composer

composer require --dev timehunter/laravel-dto-generator "~2.2.1"

Installation

  1. Create your local config file
php artisan vendor:publish --provider="TimeHunter\LaravelDTOGenerator\LaravelDTOGeneratorServiceProvider"
  1. Add your array schema in config
  2. Run artisan command:
php artisan make:dto
  1. Check your files under your specified file location

Example

Input

PHP Array Schema

        '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' => ''
        ]

Note: each object should have key name defined.

  • If it is an object please define it as a object, see the above.
  • If your components are array of objects, please define it as a pure array.
  • You have to assign a value for each property, for example, an empty string '' for first_name.

Example Output Usage

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

Output - DTOs:

User Class

<?php

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,
		];
	}
}

Comment Class

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,
		];
	}
}

Author Class

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,
		];
	}
}

Followers Class

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),
		];
	}
}

FollowerUser Class

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,
		];
	}
}

License

MIT. Please see the license file for more information.

laravel-dto-generator's People

Contributors

ryandadeng avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

ab92015359 xngwyz

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.