5656 */
5757abstract class BaseTransformer implements TransformerInterface
5858{
59+ /**
60+ * @var list<string>|null
61+ */
5962 private ?array $ fields = null ;
63+
64+ /**
65+ * @var list<string>|null
66+ */
6067 private ?array $ includes = null ;
68+
6169 protected mixed $ resource = null ;
6270
6371 public function __construct (
6472 private ?IncomingRequest $ request = null ,
65- )
66- {
73+ ) {
6774 $ this ->request = $ request ?? request ();
6875
69- $ fields = $ this ->request ->getGet ('fields ' );
76+ $ fields = $ this ->request ->getGet ('fields ' );
7077 $ this ->fields = is_string ($ fields )
7178 ? array_map ('trim ' , explode (', ' , $ fields ))
7279 : $ fields ;
7380
74- $ includes = $ this ->request ->getGet ('include ' );
81+ $ includes = $ this ->request ->getGet ('include ' );
7582 $ this ->includes = is_string ($ includes )
7683 ? array_map ('trim ' , explode (', ' , $ includes ))
7784 : $ includes ;
@@ -116,14 +123,15 @@ public function transform(mixed $resource = null): array
116123 */
117124 public function transformMany (array $ resources ): array
118125 {
119- return array_map (fn ($ resource ) => $ this ->transform ($ resource ), $ resources );
126+ return array_map (fn ($ resource ): array => $ this ->transform ($ resource ), $ resources );
120127 }
121128
122129 /**
123130 * Conditionally include a value.
124131 *
125132 * @param mixed $value
126133 * @param mixed $default
134+ *
127135 * @return mixed
128136 */
129137 protected function when (bool $ condition , $ value , $ default = null )
@@ -133,16 +141,23 @@ protected function when(bool $condition, $value, $default = null)
133141
134142 /**
135143 * Conditionally exclude a value.
144+ *
145+ * @param mixed $value
146+ * @param mixed|null $default
147+ *
148+ * @return mixed
136149 */
137150 protected function whenNot (bool $ condition , $ value , $ default = null )
138151 {
139- return ! $ condition ? $ value : $ default ;
152+ return $ condition ? $ default : $ value ;
140153 }
141154
142155 /**
143156 * Define which fields can be requested via the 'fields' query parameter.
144157 * Override in child classes to restrict available fields.
145158 * Return null to allow all fields from toArray().
159+ *
160+ * @return list<string>|null
146161 */
147162 protected function getAllowedFields (): ?array
148163 {
@@ -154,6 +169,8 @@ protected function getAllowedFields(): ?array
154169 * Override in child classes to restrict available includes.
155170 * Return null to allow all includes that have corresponding methods.
156171 * Return an empty array to disable all includes.
172+ *
173+ * @return list<string>|null
157174 */
158175 protected function getAllowedIncludes (): ?array
159176 {
@@ -163,6 +180,10 @@ protected function getAllowedIncludes(): ?array
163180 /**
164181 * Limits the given data array to only the fields specified
165182 *
183+ * @param array<string, mixed> $data
184+ *
185+ * @return array<string, mixed>
186+ *
166187 * @throws InvalidArgumentException
167188 */
168189 private function limitFields (array $ data ): array
@@ -188,6 +209,10 @@ private function limitFields(array $data): array
188209 /**
189210 * Checks the request for 'include' query variable, and if present,
190211 * calls the corresponding include{Resource} methods to add related data.
212+ *
213+ * @param array<string, mixed> $data
214+ *
215+ * @return array<string, mixed>
191216 */
192217 private function insertIncludes (array $ data ): array
193218 {
@@ -201,7 +226,7 @@ private function insertIncludes(array $data): array
201226 return $ data ; // No includes allowed
202227 }
203228
204- // If whitelist is defined, filter the requested includes
229+ // If whitelist is defined, filter the requested includes
205230 if ($ allowedIncludes !== null ) {
206231 $ invalidIncludes = array_diff ($ this ->includes , $ allowedIncludes );
207232
@@ -213,7 +238,7 @@ private function insertIncludes(array $data): array
213238 foreach ($ this ->includes as $ include ) {
214239 $ method = 'include ' . ucfirst ($ include );
215240 if (method_exists ($ this , $ method )) {
216- $ data [$ include ] = $ this ->$ method ();
241+ $ data [$ include ] = $ this ->{ $ method} ();
217242 } else {
218243 throw ApiException::forMissingInclude ($ include );
219244 }
0 commit comments