vendor/api-platform/core/src/Core/Metadata/Resource/ResourceMetadata.php line 34

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the API Platform project.
  4. *
  5. * (c) Kévin Dunglas <dunglas@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace ApiPlatform\Core\Metadata\Resource;
  12. use ApiPlatform\Core\Api\OperationType;
  13. /**
  14. * Resource metadata.
  15. *
  16. * @author Kévin Dunglas <dunglas@gmail.com>
  17. */
  18. final class ResourceMetadata
  19. {
  20. private $shortName;
  21. private $description;
  22. private $iri;
  23. private $itemOperations;
  24. private $collectionOperations;
  25. private $subresourceOperations;
  26. private $graphql;
  27. private $attributes;
  28. public function __construct(string $shortName = null, string $description = null, string $iri = null, array $itemOperations = null, array $collectionOperations = null, array $attributes = null, array $subresourceOperations = null, array $graphql = null)
  29. {
  30. $this->shortName = $shortName;
  31. $this->description = $description;
  32. $this->iri = $iri;
  33. $this->itemOperations = $itemOperations;
  34. $this->collectionOperations = $collectionOperations;
  35. $this->subresourceOperations = $subresourceOperations;
  36. $this->graphql = $graphql;
  37. $this->attributes = $attributes;
  38. }
  39. /**
  40. * Gets the short name.
  41. */
  42. public function getShortName(): ?string
  43. {
  44. return $this->shortName;
  45. }
  46. /**
  47. * Returns a new instance with the given short name.
  48. */
  49. public function withShortName(string $shortName): self
  50. {
  51. $metadata = clone $this;
  52. $metadata->shortName = $shortName;
  53. return $metadata;
  54. }
  55. /**
  56. * Gets the description.
  57. */
  58. public function getDescription(): ?string
  59. {
  60. return $this->description;
  61. }
  62. /**
  63. * Returns a new instance with the given description.
  64. */
  65. public function withDescription(string $description): self
  66. {
  67. $metadata = clone $this;
  68. $metadata->description = $description;
  69. return $metadata;
  70. }
  71. /**
  72. * Gets the associated IRI.
  73. */
  74. public function getIri(): ?string
  75. {
  76. return $this->iri;
  77. }
  78. /**
  79. * Returns a new instance with the given IRI.
  80. */
  81. public function withIri(string $iri): self
  82. {
  83. $metadata = clone $this;
  84. $metadata->iri = $iri;
  85. return $metadata;
  86. }
  87. /**
  88. * Gets item operations.
  89. */
  90. public function getItemOperations(): ?array
  91. {
  92. return $this->itemOperations;
  93. }
  94. /**
  95. * Returns a new instance with the given item operations.
  96. */
  97. public function withItemOperations(array $itemOperations): self
  98. {
  99. $metadata = clone $this;
  100. $metadata->itemOperations = $itemOperations;
  101. return $metadata;
  102. }
  103. /**
  104. * Gets collection operations.
  105. */
  106. public function getCollectionOperations(): ?array
  107. {
  108. return $this->collectionOperations;
  109. }
  110. /**
  111. * Returns a new instance with the given collection operations.
  112. */
  113. public function withCollectionOperations(array $collectionOperations): self
  114. {
  115. $metadata = clone $this;
  116. $metadata->collectionOperations = $collectionOperations;
  117. return $metadata;
  118. }
  119. /**
  120. * Gets subresource operations.
  121. */
  122. public function getSubresourceOperations(): ?array
  123. {
  124. return $this->subresourceOperations;
  125. }
  126. /**
  127. * Returns a new instance with the given subresource operations.
  128. */
  129. public function withSubresourceOperations(array $subresourceOperations): self
  130. {
  131. $metadata = clone $this;
  132. $metadata->subresourceOperations = $subresourceOperations;
  133. return $metadata;
  134. }
  135. /**
  136. * Gets a collection operation attribute, optionally fallback to a resource attribute.
  137. *
  138. * @param mixed|null $defaultValue
  139. */
  140. public function getCollectionOperationAttribute(?string $operationName, string $key, $defaultValue = null, bool $resourceFallback = false)
  141. {
  142. return $this->findOperationAttribute($this->collectionOperations, $operationName, $key, $defaultValue, $resourceFallback);
  143. }
  144. /**
  145. * Gets an item operation attribute, optionally fallback to a resource attribute.
  146. *
  147. * @param mixed|null $defaultValue
  148. */
  149. public function getItemOperationAttribute(?string $operationName, string $key, $defaultValue = null, bool $resourceFallback = false)
  150. {
  151. return $this->findOperationAttribute($this->itemOperations, $operationName, $key, $defaultValue, $resourceFallback);
  152. }
  153. /**
  154. * Gets a subresource operation attribute, optionally fallback to a resource attribute.
  155. *
  156. * @param mixed|null $defaultValue
  157. */
  158. public function getSubresourceOperationAttribute(?string $operationName, string $key, $defaultValue = null, bool $resourceFallback = false)
  159. {
  160. return $this->findOperationAttribute($this->subresourceOperations, $operationName, $key, $defaultValue, $resourceFallback);
  161. }
  162. public function getGraphqlAttribute(string $operationName, string $key, $defaultValue = null, bool $resourceFallback = false)
  163. {
  164. if (isset($this->graphql[$operationName][$key])) {
  165. return $this->graphql[$operationName][$key];
  166. }
  167. if ($resourceFallback && isset($this->attributes[$key])) {
  168. return $this->attributes[$key];
  169. }
  170. return $defaultValue;
  171. }
  172. /**
  173. * Gets the first available operation attribute according to the following order: collection, item, subresource, optionally fallback to a default value.
  174. *
  175. * @param mixed|null $defaultValue
  176. */
  177. public function getOperationAttribute(array $attributes, string $key, $defaultValue = null, bool $resourceFallback = false)
  178. {
  179. if (isset($attributes['collection_operation_name'])) {
  180. return $this->getCollectionOperationAttribute($attributes['collection_operation_name'], $key, $defaultValue, $resourceFallback);
  181. }
  182. if (isset($attributes['item_operation_name'])) {
  183. return $this->getItemOperationAttribute($attributes['item_operation_name'], $key, $defaultValue, $resourceFallback);
  184. }
  185. if (isset($attributes['subresource_operation_name'])) {
  186. return $this->getSubresourceOperationAttribute($attributes['subresource_operation_name'], $key, $defaultValue, $resourceFallback);
  187. }
  188. if ($resourceFallback && isset($this->attributes[$key])) {
  189. return $this->attributes[$key];
  190. }
  191. return $defaultValue;
  192. }
  193. /**
  194. * Gets an attribute for a given operation type and operation name.
  195. *
  196. * @param mixed|null $defaultValue
  197. */
  198. public function getTypedOperationAttribute(string $operationType, string $operationName, string $key, $defaultValue = null, bool $resourceFallback = false)
  199. {
  200. switch ($operationType) {
  201. case OperationType::COLLECTION:
  202. return $this->getCollectionOperationAttribute($operationName, $key, $defaultValue, $resourceFallback);
  203. case OperationType::ITEM:
  204. return $this->getItemOperationAttribute($operationName, $key, $defaultValue, $resourceFallback);
  205. default:
  206. return $this->getSubresourceOperationAttribute($operationName, $key, $defaultValue, $resourceFallback);
  207. }
  208. }
  209. /**
  210. * Gets attributes.
  211. */
  212. public function getAttributes(): ?array
  213. {
  214. return $this->attributes;
  215. }
  216. /**
  217. * Gets an attribute.
  218. *
  219. * @param mixed|null $defaultValue
  220. */
  221. public function getAttribute(string $key, $defaultValue = null)
  222. {
  223. return $this->attributes[$key] ?? $defaultValue;
  224. }
  225. /**
  226. * Returns a new instance with the given attribute.
  227. */
  228. public function withAttributes(array $attributes): self
  229. {
  230. $metadata = clone $this;
  231. $metadata->attributes = $attributes;
  232. return $metadata;
  233. }
  234. /**
  235. * Gets options of for the GraphQL query.
  236. */
  237. public function getGraphql(): ?array
  238. {
  239. return $this->graphql;
  240. }
  241. /**
  242. * Returns a new instance with the given GraphQL options.
  243. */
  244. public function withGraphql(array $graphql): self
  245. {
  246. $metadata = clone $this;
  247. $metadata->graphql = $graphql;
  248. return $metadata;
  249. }
  250. /**
  251. * Gets an operation attribute, optionally fallback to a resource attribute.
  252. *
  253. * @param mixed|null $defaultValue
  254. */
  255. private function findOperationAttribute(?array $operations, ?string $operationName, string $key, $defaultValue, bool $resourceFallback)
  256. {
  257. if (null !== $operationName && isset($operations[$operationName][$key])) {
  258. return $operations[$operationName][$key];
  259. }
  260. if ($resourceFallback && isset($this->attributes[$key])) {
  261. return $this->attributes[$key];
  262. }
  263. return $defaultValue;
  264. }
  265. }