vendor/api-platform/core/src/Core/Metadata/Property/PropertyMetadata.php line 419

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\Property;
  12. use Symfony\Component\PropertyInfo\Type;
  13. /**
  14. * Property metadata.
  15. *
  16. * @author Kévin Dunglas <dunglas@gmail.com>
  17. */
  18. final class PropertyMetadata
  19. {
  20. /**
  21. * @deprecated since 2.7, to be removed in 3.0, renamed as builtinTypes
  22. */
  23. private $type;
  24. private $description;
  25. private $readable;
  26. private $writable;
  27. private $readableLink;
  28. private $writableLink;
  29. private $required;
  30. /**
  31. * @deprecated since 2.7, to be removed in 3.0, renamed as types
  32. */
  33. private $iri;
  34. private $identifier;
  35. /**
  36. * @deprecated since 2.6, to be removed in 3.0
  37. */
  38. private $childInherited;
  39. private $attributes;
  40. private $subresource;
  41. private $initializable;
  42. /**
  43. * @var null
  44. */
  45. private $default;
  46. /**
  47. * @var null
  48. */
  49. private $example;
  50. private $schema;
  51. public function __construct(Type $type = null, string $description = null, bool $readable = null, bool $writable = null, bool $readableLink = null, bool $writableLink = null, bool $required = null, bool $identifier = null, string $iri = null, $childInherited = null, array $attributes = null, SubresourceMetadata $subresource = null, bool $initializable = null, $default = null, $example = null, array $schema = null)
  52. {
  53. $this->type = $type;
  54. $this->description = $description;
  55. $this->readable = $readable;
  56. $this->writable = $writable;
  57. $this->readableLink = $readableLink;
  58. $this->writableLink = $writableLink;
  59. $this->required = $required;
  60. $this->identifier = $identifier;
  61. $this->iri = $iri;
  62. if (null !== $childInherited) {
  63. @trigger_error(sprintf('Providing a non-null value for the 10th argument ($childInherited) of the "%s" constructor is deprecated since 2.6 and will not be supported in 3.0.', __CLASS__), \E_USER_DEPRECATED);
  64. }
  65. $this->childInherited = $childInherited;
  66. $this->attributes = $attributes;
  67. $this->subresource = $subresource;
  68. $this->initializable = $initializable;
  69. $this->default = $default;
  70. $this->example = $example;
  71. $this->schema = $schema;
  72. }
  73. /**
  74. * Gets type.
  75. *
  76. * @deprecated since 2.7, to be removed in 3.0, renamed as getBuiltinTypes
  77. */
  78. public function getType(): ?Type
  79. {
  80. return $this->type;
  81. }
  82. /**
  83. * Returns a new instance with the given type.
  84. *
  85. * @deprecated since 2.7, to be removed in 3.0, renamed as withBuiltinTypes
  86. */
  87. public function withType(Type $type): self
  88. {
  89. $metadata = clone $this;
  90. $metadata->type = $type;
  91. return $metadata;
  92. }
  93. /**
  94. * Gets description.
  95. */
  96. public function getDescription(): ?string
  97. {
  98. return $this->description;
  99. }
  100. /**
  101. * Returns a new instance with the given description.
  102. */
  103. public function withDescription(string $description): self
  104. {
  105. $metadata = clone $this;
  106. $metadata->description = $description;
  107. return $metadata;
  108. }
  109. /**
  110. * Is readable?
  111. */
  112. public function isReadable(): ?bool
  113. {
  114. return $this->readable;
  115. }
  116. /**
  117. * Returns a new instance of Metadata with the given readable flag.
  118. */
  119. public function withReadable(bool $readable): self
  120. {
  121. $metadata = clone $this;
  122. $metadata->readable = $readable;
  123. return $metadata;
  124. }
  125. /**
  126. * Is writable?
  127. */
  128. public function isWritable(): ?bool
  129. {
  130. return $this->writable;
  131. }
  132. /**
  133. * Returns a new instance with the given writable flag.
  134. */
  135. public function withWritable(bool $writable): self
  136. {
  137. $metadata = clone $this;
  138. $metadata->writable = $writable;
  139. return $metadata;
  140. }
  141. /**
  142. * Is required?
  143. */
  144. public function isRequired(): ?bool
  145. {
  146. if (true === $this->required && false === $this->writable) {
  147. return false;
  148. }
  149. return $this->required;
  150. }
  151. /**
  152. * Returns a new instance with the given required flag.
  153. */
  154. public function withRequired(bool $required): self
  155. {
  156. $metadata = clone $this;
  157. $metadata->required = $required;
  158. return $metadata;
  159. }
  160. /**
  161. * Should an IRI or an object be provided in write context?
  162. */
  163. public function isWritableLink(): ?bool
  164. {
  165. return $this->writableLink;
  166. }
  167. /**
  168. * Returns a new instance with the given writable link flag.
  169. */
  170. public function withWritableLink(bool $writableLink): self
  171. {
  172. $metadata = clone $this;
  173. $metadata->writableLink = $writableLink;
  174. return $metadata;
  175. }
  176. /**
  177. * Is an IRI or an object generated in read context?
  178. */
  179. public function isReadableLink(): ?bool
  180. {
  181. return $this->readableLink;
  182. }
  183. /**
  184. * Returns a new instance with the given readable link flag.
  185. */
  186. public function withReadableLink(bool $readableLink): self
  187. {
  188. $metadata = clone $this;
  189. $metadata->readableLink = $readableLink;
  190. return $metadata;
  191. }
  192. /**
  193. * Gets IRI of this property.
  194. */
  195. public function getIri(): ?string
  196. {
  197. return $this->iri;
  198. }
  199. /**
  200. * Returns a new instance with the given IRI.
  201. */
  202. public function withIri(string $iri = null): self
  203. {
  204. $metadata = clone $this;
  205. $metadata->iri = $iri;
  206. return $metadata;
  207. }
  208. /**
  209. * Is this attribute an identifier?
  210. */
  211. public function isIdentifier(): ?bool
  212. {
  213. return $this->identifier;
  214. }
  215. /**
  216. * Returns a new instance with the given identifier flag.
  217. */
  218. public function withIdentifier(bool $identifier): self
  219. {
  220. $metadata = clone $this;
  221. $metadata->identifier = $identifier;
  222. return $metadata;
  223. }
  224. /**
  225. * Gets attributes.
  226. */
  227. public function getAttributes(): ?array
  228. {
  229. return $this->attributes;
  230. }
  231. /**
  232. * Gets an attribute.
  233. *
  234. * @param mixed|null $defaultValue
  235. */
  236. public function getAttribute(string $key, $defaultValue = null)
  237. {
  238. return $this->attributes[$key] ?? $defaultValue;
  239. }
  240. /**
  241. * Returns a new instance with the given attribute.
  242. */
  243. public function withAttributes(array $attributes): self
  244. {
  245. $metadata = clone $this;
  246. $metadata->attributes = $attributes;
  247. return $metadata;
  248. }
  249. /**
  250. * @deprecated since 2.6, to be removed in 3.0
  251. */
  252. public function getChildInherited(): ?string
  253. {
  254. return $this->childInherited;
  255. }
  256. /**
  257. * @deprecated since 2.6, to be removed in 3.0
  258. */
  259. public function hasChildInherited(): bool
  260. {
  261. return null !== $this->childInherited;
  262. }
  263. /**
  264. * @deprecated since 2.4, to be removed in 3.0
  265. */
  266. public function isChildInherited(): ?string
  267. {
  268. @trigger_error(sprintf('"%s::%s" is deprecated since 2.4 and will be removed in 3.0.', __CLASS__, __METHOD__), \E_USER_DEPRECATED);
  269. return $this->getChildInherited();
  270. }
  271. /**
  272. * @deprecated since 2.6, to be removed in 3.0
  273. */
  274. public function withChildInherited(string $childInherited): self
  275. {
  276. @trigger_error(sprintf('"%s::%s" is deprecated since 2.6 and will be removed in 3.0.', __CLASS__, __METHOD__), \E_USER_DEPRECATED);
  277. $metadata = clone $this;
  278. $metadata->childInherited = $childInherited;
  279. return $metadata;
  280. }
  281. /**
  282. * Represents whether the property has a subresource.
  283. */
  284. public function hasSubresource(): bool
  285. {
  286. return null !== $this->subresource;
  287. }
  288. /**
  289. * Gets the subresource metadata.
  290. */
  291. public function getSubresource(): ?SubresourceMetadata
  292. {
  293. return $this->subresource;
  294. }
  295. /**
  296. * Returns a new instance with the given subresource.
  297. */
  298. public function withSubresource(SubresourceMetadata $subresource = null): self
  299. {
  300. $metadata = clone $this;
  301. $metadata->subresource = $subresource;
  302. return $metadata;
  303. }
  304. /**
  305. * Is initializable?
  306. */
  307. public function isInitializable(): ?bool
  308. {
  309. return $this->initializable;
  310. }
  311. /**
  312. * Returns a new instance with the given initializable flag.
  313. */
  314. public function withInitializable(bool $initializable): self
  315. {
  316. $metadata = clone $this;
  317. $metadata->initializable = $initializable;
  318. return $metadata;
  319. }
  320. /**
  321. * Returns the default value of the property or NULL if the property doesn't have a default value.
  322. */
  323. public function getDefault()
  324. {
  325. return $this->default;
  326. }
  327. /**
  328. * Returns a new instance with the given default value for the property.
  329. */
  330. public function withDefault($default): self
  331. {
  332. $metadata = clone $this;
  333. $metadata->default = $default;
  334. return $metadata;
  335. }
  336. /**
  337. * Returns an example of the value of the property.
  338. */
  339. public function getExample()
  340. {
  341. return $this->example;
  342. }
  343. /**
  344. * Returns a new instance with the given example.
  345. */
  346. public function withExample($example): self
  347. {
  348. $metadata = clone $this;
  349. $metadata->example = $example;
  350. return $metadata;
  351. }
  352. public function getSchema(): ?array
  353. {
  354. return $this->schema;
  355. }
  356. /**
  357. * Returns a new instance with the given schema.
  358. */
  359. public function withSchema(array $schema = null): self
  360. {
  361. $metadata = clone $this;
  362. $metadata->schema = $schema;
  363. return $metadata;
  364. }
  365. }