/vendor/league/fractal/src/TransformerAbstract.php

https://gitlab.com/Pasantias/pasantiasASLG · PHP · 276 lines · 107 code · 33 blank · 136 comment · 4 complexity · 7ef991d600abbdb1e82cc5b5e5b16d53 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the League\Fractal package.
  4. *
  5. * (c) Phil Sturgeon <me@philsturgeon.uk>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace League\Fractal;
  11. use League\Fractal\Resource\Collection;
  12. use League\Fractal\Resource\Item;
  13. use League\Fractal\Resource\NullResource;
  14. use League\Fractal\Resource\ResourceAbstract;
  15. /**
  16. * Transformer Abstract
  17. *
  18. * All Transformer classes should extend this to utilize the convenience methods
  19. * collection() and item(), and make the self::$availableIncludes property available.
  20. * Extend it and add a `transform()` method to transform any default or included data
  21. * into a basic array.
  22. */
  23. abstract class TransformerAbstract
  24. {
  25. /**
  26. * Resources that can be included if requested.
  27. *
  28. * @var array
  29. */
  30. protected $availableIncludes = [];
  31. /**
  32. * Include resources without needing it to be requested.
  33. *
  34. * @var array
  35. */
  36. protected $defaultIncludes = [];
  37. /**
  38. * The transformer should know about the current scope, so we can fetch relevant params.
  39. *
  40. * @var Scope
  41. */
  42. protected $currentScope;
  43. /**
  44. * Getter for availableIncludes.
  45. *
  46. * @return array
  47. */
  48. public function getAvailableIncludes()
  49. {
  50. return $this->availableIncludes;
  51. }
  52. /**
  53. * Getter for defaultIncludes.
  54. *
  55. * @return array
  56. */
  57. public function getDefaultIncludes()
  58. {
  59. return $this->defaultIncludes;
  60. }
  61. /**
  62. * Getter for currentScope.
  63. *
  64. * @return \League\Fractal\Scope
  65. */
  66. public function getCurrentScope()
  67. {
  68. return $this->currentScope;
  69. }
  70. /**
  71. * Figure out which includes we need.
  72. *
  73. * @internal
  74. *
  75. * @param Scope $scope
  76. *
  77. * @return array
  78. */
  79. private function figureOutWhichIncludes(Scope $scope)
  80. {
  81. $includes = $this->getDefaultIncludes();
  82. foreach ($this->getAvailableIncludes() as $include) {
  83. if ($scope->isRequested($include)) {
  84. $includes[] = $include;
  85. }
  86. }
  87. return $includes;
  88. }
  89. /**
  90. * This method is fired to loop through available includes, see if any of
  91. * them are requested and permitted for this scope.
  92. *
  93. * @internal
  94. *
  95. * @param Scope $scope
  96. * @param mixed $data
  97. *
  98. * @return array
  99. */
  100. public function processIncludedResources(Scope $scope, $data)
  101. {
  102. $includedData = [];
  103. $includes = $this->figureOutWhichIncludes($scope);
  104. foreach ($includes as $include) {
  105. $includedData = $this->includeResourceIfAvailable(
  106. $scope,
  107. $data,
  108. $includedData,
  109. $include
  110. );
  111. }
  112. return $includedData === [] ? false : $includedData;
  113. }
  114. /**
  115. * Include a resource only if it is available on the method.
  116. *
  117. * @internal
  118. *
  119. * @param Scope $scope
  120. * @param mixed $data
  121. * @param array $includedData
  122. * @param string $include
  123. *
  124. * @return array
  125. */
  126. private function includeResourceIfAvailable(
  127. Scope $scope,
  128. $data,
  129. $includedData,
  130. $include
  131. ) {
  132. if ($resource = $this->callIncludeMethod($scope, $include, $data)) {
  133. $childScope = $scope->embedChildScope($include, $resource);
  134. $includedData[$include] = $childScope->toArray();
  135. }
  136. return $includedData;
  137. }
  138. /**
  139. * Call Include Method.
  140. *
  141. * @internal
  142. *
  143. * @param Scope $scope
  144. * @param string $includeName
  145. * @param mixed $data
  146. *
  147. * @throws \Exception
  148. *
  149. * @return \League\Fractal\Resource\ResourceInterface
  150. */
  151. protected function callIncludeMethod(Scope $scope, $includeName, $data)
  152. {
  153. $scopeIdentifier = $scope->getIdentifier($includeName);
  154. $params = $scope->getManager()->getIncludeParams($scopeIdentifier);
  155. // Check if the method name actually exists
  156. $methodName = 'include'.str_replace(' ', '', ucwords(str_replace('_', ' ', str_replace('-', ' ', $includeName))));
  157. $resource = call_user_func([$this, $methodName], $data, $params);
  158. if ($resource === null) {
  159. return false;
  160. }
  161. if (! $resource instanceof ResourceAbstract) {
  162. throw new \Exception(sprintf(
  163. 'Invalid return value from %s::%s(). Expected %s, received %s.',
  164. __CLASS__,
  165. $methodName,
  166. 'League\Fractal\Resource\ResourceAbstract',
  167. gettype($resource)
  168. ));
  169. }
  170. return $resource;
  171. }
  172. /**
  173. * Setter for availableIncludes.
  174. *
  175. * @param array $availableIncludes
  176. *
  177. * @return $this
  178. */
  179. public function setAvailableIncludes($availableIncludes)
  180. {
  181. $this->availableIncludes = $availableIncludes;
  182. return $this;
  183. }
  184. /**
  185. * Setter for defaultIncludes.
  186. *
  187. * @param array $defaultIncludes
  188. *
  189. * @return $this
  190. */
  191. public function setDefaultIncludes($defaultIncludes)
  192. {
  193. $this->defaultIncludes = $defaultIncludes;
  194. return $this;
  195. }
  196. /**
  197. * Setter for currentScope.
  198. *
  199. * @param Scope $currentScope
  200. *
  201. * @return $this
  202. */
  203. public function setCurrentScope($currentScope)
  204. {
  205. $this->currentScope = $currentScope;
  206. return $this;
  207. }
  208. /**
  209. * Create a new item resource object.
  210. *
  211. * @param mixed $data
  212. * @param TransformerAbstract|callable $transformer
  213. * @param string $resourceKey
  214. *
  215. * @return Item
  216. */
  217. protected function item($data, $transformer, $resourceKey = null)
  218. {
  219. return new Item($data, $transformer, $resourceKey);
  220. }
  221. /**
  222. * Create a new collection resource object.
  223. *
  224. * @param mixed $data
  225. * @param TransformerAbstract|callable $transformer
  226. * @param string $resourceKey
  227. *
  228. * @return Collection
  229. */
  230. protected function collection($data, $transformer, $resourceKey = null)
  231. {
  232. return new Collection($data, $transformer, $resourceKey);
  233. }
  234. /**
  235. * Create a new null resource object.
  236. *
  237. * @return NullResource
  238. */
  239. protected function null()
  240. {
  241. return new NullResource();
  242. }
  243. }