@@ -41,76 +41,154 @@ php artisan make:jsontoclass
4141#### PHP Array
4242
4343```` php
44- 'message ' => [
45- 'author' => [
44+ 'post ' => [
45+ 'author' => (object) [
4646 'first_name' => '',
4747 'last_name' => '',
4848 ],
49- 'text' => '',
50- 'date' => '2019-01-01'
49+ 'comment' => (object)[
50+ 'user' => (object)[
51+ 'first_name' => '',
52+ 'last_name' => ''
53+ ],
54+ 'content' => ''
55+ ],
56+ 'followers' => (array)[
57+ 'id' => '',
58+ 'follower_user' => [
59+ 'first_name' => '',
60+ 'last_name' => ''
61+ ]
62+ ],
63+ 'text' => '',
64+ 'date' => '2019-01-01'
5165 ]
5266````
5367
68+ Note: each object should have key name defined.
69+
70+ - If it is object please put ` object ` .
71+ - If your components are array of object, please define it as a pure array.
72+
5473#### or JSON
5574
56- ```` json
57- {
58- "message" :{
59- "author" : {
60- "first_name" : " " ,
61- "last_name" : " "
62- },
63- "text" : " " ,
64- "date" : " 2019-01-01" ,
65- }
66- }
67- ````
75+ Note: I have disabled JSON method, you need to convert JSON to PHP array yourself. (You can use online convert tool if necessary)
76+
6877
6978
7079### Output - Classes:
7180
72- #### Message Class
81+ #### Author Class
7382```` php
7483<?php
7584
76- namespace App\Test;
7785
78- class Message
86+ class Author
7987{
80- public $author;
88+ /** @var $firstName */
89+ public $firstName;
90+
91+ /** @var $lastName */
92+ public $lastName;
8193
82- public $text;
8394
84- public $date;
95+ /**
96+ * @return Author
97+ */
98+ public static function create()
99+ {
100+ return new self;
101+ }
85102
86103
104+ /**
105+ * @return array
106+ */
87107 public function toArray(): array
88108 {
89109 return [
90- 'author' => collect($this->author)->map(function (Author $data){
91- return $data->toArray();
92- })->toArray(),
93- 'text' => $this->text,
94- 'date' => $this->date,
110+ 'first_name' => $this->firstName,
111+ 'last_name' => $this->lastName,
95112 ];
96113 }
97114}
98115
116+
99117````
100118
101- #### Author Class
119+ #### Followers Class
102120```` php
103121<?php
122+ class Followers
123+ {
124+ /** @var $id */
125+ public $id;
104126
105- namespace App\Test;
127+ /** @var FollowerUser $followerUser */
128+ public $followerUser;
106129
107- class Author
130+
131+ /**
132+ * @return Followers
133+ */
134+ public static function create()
135+ {
136+ return new self;
137+ }
138+
139+
140+ /**
141+ * @param FollowerUser $followerUser
142+ * @return $this
143+ */
144+ public function addFollowerUser($followerUser)
145+ {
146+ $this->followerUser[] = $followerUser;
147+ return $this;
148+ }
149+
150+
151+ /**
152+ * @return array
153+ */
154+ public function toArray(): array
155+ {
156+ return [
157+ 'id' => $this->id,
158+ 'follower_user' => collect($this->followerUser)->map(function (FollowerUser $data){
159+ return $data->toArray();
160+ })->toArray(),
161+ ];
162+ }
163+ }
164+
165+
166+ ````
167+
168+
169+ #### FollowerUser Class
170+ ```` php
171+ class FollowerUser
108172{
173+ /** @var $firstName */
109174 public $firstName;
110175
176+ /** @var $lastName */
111177 public $lastName;
112178
113179
180+ /**
181+ * @return FollowerUser
182+ */
183+ public static function create()
184+ {
185+ return new self;
186+ }
187+
188+
189+ /**
190+ * @return array
191+ */
114192 public function toArray(): array
115193 {
116194 return [
@@ -121,7 +199,6 @@ class Author
121199}
122200
123201````
124-
125202## PHPStorm
126203
127204If you are using PHPStorm and you want to put ` return $this ` in each set function.
0 commit comments