Skip to content

Commit

Permalink
Update readme.md
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanDaDeng authored Aug 9, 2019
1 parent 878834e commit 0cc9955
Showing 1 changed file with 108 additions and 31 deletions.
139 changes: 108 additions & 31 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,76 +41,154 @@ php artisan make:jsontoclass
#### PHP Array

````php
'message' => [
'author' => [
'post' => [
'author' => (object)[
'first_name' => '',
'last_name' => '',
],
'text' => '',
'date' => '2019-01-01'
'comment' => (object)[
'user' => (object)[
'first_name' => '',
'last_name' => ''
],
'content' => ''
],
'followers' => (array)[
'id' => '',
'follower_user' => [
'first_name' => '',
'last_name' => ''
]
],
'text' => '',
'date' => '2019-01-01'
]
````

Note: each object should have key name defined.

- If it is object please put `object`.
- If your components are array of object, please define it as a pure array.

#### or JSON

````json
{
"message":{
"author": {
"first_name": "",
"last_name": ""
},
"text": "",
"date": "2019-01-01",
}
}
````
Note: I have disabled JSON method, you need to convert JSON to PHP array yourself. (You can use online convert tool if necessary)



### Output - Classes:

#### Message Class
#### Author Class
````php
<?php

namespace App\Test;

class Message
class Author
{
public $author;
/** @var $firstName */
public $firstName;

/** @var $lastName */
public $lastName;

public $text;

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


/**
* @return array
*/
public function toArray(): array
{
return [
'author' => collect($this->author)->map(function (Author $data){
return $data->toArray();
})->toArray(),
'text' => $this->text,
'date' => $this->date,
'first_name' => $this->firstName,
'last_name' => $this->lastName,
];
}
}


````

#### Author Class
#### Followers Class
````php
<?php
class Followers
{
/** @var $id */
public $id;

namespace App\Test;
/** @var FollowerUser $followerUser */
public $followerUser;

class Author

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


/**
* @param FollowerUser $followerUser
* @return $this
*/
public function addFollowerUser($followerUser)
{
$this->followerUser[] = $followerUser;
return $this;
}


/**
* @return array
*/
public function toArray(): array
{
return [
'id' => $this->id,
'follower_user' => collect($this->followerUser)->map(function (FollowerUser $data){
return $data->toArray();
})->toArray(),
];
}
}


````


#### FollowerUser Class
````php
class FollowerUser
{
/** @var $firstName */
public $firstName;

/** @var $lastName */
public $lastName;


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


/**
* @return array
*/
public function toArray(): array
{
return [
Expand All @@ -121,7 +199,6 @@ class Author
}

````

## PHPStorm

If you are using PHPStorm and you want to put `return $this` in each set function.
Expand Down

0 comments on commit 0cc9955

Please sign in to comment.