PropertyWriteInfo.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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. namespace Symfony\Component\PropertyInfo;
  11. /**
  12. * The write mutator defines how a property can be written.
  13. *
  14. * @author Joel Wurtz <jwurtz@jolicode.com>
  15. */
  16. final class PropertyWriteInfo
  17. {
  18. public const TYPE_NONE = 'none';
  19. public const TYPE_METHOD = 'method';
  20. public const TYPE_PROPERTY = 'property';
  21. public const TYPE_ADDER_AND_REMOVER = 'adder_and_remover';
  22. public const TYPE_CONSTRUCTOR = 'constructor';
  23. public const VISIBILITY_PUBLIC = 'public';
  24. public const VISIBILITY_PROTECTED = 'protected';
  25. public const VISIBILITY_PRIVATE = 'private';
  26. private ?self $adderInfo = null;
  27. private ?self $removerInfo = null;
  28. private array $errors = [];
  29. public function __construct(
  30. private readonly string $type = self::TYPE_NONE,
  31. private readonly ?string $name = null,
  32. private readonly ?string $visibility = null,
  33. private readonly ?bool $static = null,
  34. ) {
  35. }
  36. public function getType(): string
  37. {
  38. return $this->type;
  39. }
  40. public function getName(): string
  41. {
  42. if (null === $this->name) {
  43. throw new \LogicException("Calling getName() when having a mutator of type {$this->type} is not tolerated.");
  44. }
  45. return $this->name;
  46. }
  47. public function setAdderInfo(self $adderInfo): void
  48. {
  49. $this->adderInfo = $adderInfo;
  50. }
  51. public function getAdderInfo(): self
  52. {
  53. if (null === $this->adderInfo) {
  54. throw new \LogicException("Calling getAdderInfo() when having a mutator of type {$this->type} is not tolerated.");
  55. }
  56. return $this->adderInfo;
  57. }
  58. public function setRemoverInfo(self $removerInfo): void
  59. {
  60. $this->removerInfo = $removerInfo;
  61. }
  62. public function getRemoverInfo(): self
  63. {
  64. if (null === $this->removerInfo) {
  65. throw new \LogicException("Calling getRemoverInfo() when having a mutator of type {$this->type} is not tolerated.");
  66. }
  67. return $this->removerInfo;
  68. }
  69. public function getVisibility(): string
  70. {
  71. if (null === $this->visibility) {
  72. throw new \LogicException("Calling getVisibility() when having a mutator of type {$this->type} is not tolerated.");
  73. }
  74. return $this->visibility;
  75. }
  76. public function isStatic(): bool
  77. {
  78. if (null === $this->static) {
  79. throw new \LogicException("Calling isStatic() when having a mutator of type {$this->type} is not tolerated.");
  80. }
  81. return $this->static;
  82. }
  83. public function setErrors(array $errors): void
  84. {
  85. $this->errors = $errors;
  86. }
  87. public function getErrors(): array
  88. {
  89. return $this->errors;
  90. }
  91. public function hasErrors(): bool
  92. {
  93. return (bool) \count($this->errors);
  94. }
  95. }