src/Domain/Whater/Model/WhaterPointAttributeType.php line 17

Open in your IDE?
  1. <?php
  2. namespace Whater\Domain\Whater\Model;
  3. use BornFree\TacticianDomainEvent\Recorder\ContainsRecordedEvents;
  4. use BornFree\TacticianDomainEvent\Recorder\EventRecorderCapabilities;
  5. use Doctrine\Common\Collections\Collection;
  6. use Ramsey\Uuid\Uuid;
  7. use Whater\Domain\Common\Exception\InvalidUUIDException;
  8. /**
  9. * Class WhaterPointAttribute
  10. *
  11. * @package Whater\Domain\Whater\Model
  12. */
  13. class WhaterPointAttributeType implements ContainsRecordedEvents
  14. {
  15. use EventRecorderCapabilities;
  16. const ATTR_VALUE_TEXT = 'ATTR_TYPE_TEXT';
  17. const ATTR_VALUE_FLOAT = 'ATTR_TYPE_FLOAT';
  18. const ATTR_VALUE_CHOICES = 'ATTR_TYPE_CHOICES';
  19. /**
  20. * @var string
  21. */
  22. private $uuid;
  23. /**
  24. * @var \DateTime
  25. */
  26. private $createdAt;
  27. /**
  28. * @var \DateTime
  29. */
  30. private $updatedAt;
  31. /**
  32. * @var string
  33. */
  34. private $name;
  35. /**
  36. * @var string
  37. */
  38. private $slug;
  39. /**
  40. * @var string
  41. */
  42. private $attributeType;
  43. /**
  44. * @var Collection
  45. */
  46. private $choices;
  47. public function __construct(
  48. string $whaterPointAttributeTypeId = null,
  49. string $name,
  50. string $slug,
  51. string $attributeType,
  52. Collection $choices = null
  53. ) {
  54. try {
  55. $this->uuid = Uuid::fromString($whaterPointAttributeTypeId ?: Uuid::uuid4())->toString();
  56. } catch (\InvalidArgumentException $e) {
  57. throw new InvalidUUIDException();
  58. }
  59. $this->name = $name;
  60. $this->slug = $slug;
  61. $this->attributeType = $attributeType;
  62. if ($attributeType == WhaterPointAttributeType::ATTR_VALUE_CHOICES) {
  63. $this->updateAttributeTypeChoices($choices);
  64. }
  65. $this->createdAt = new \DateTime();
  66. $this->updatedAt = new \DateTime();
  67. }
  68. public function updateAttributeTypeChoices(
  69. Collection $choices
  70. ) {
  71. $this->choices = $choices;
  72. $this->updatedAt = new \DateTime();
  73. }
  74. /**
  75. * @return string
  76. */
  77. public function id(): string
  78. {
  79. return $this->uuid;
  80. }
  81. /**
  82. * @return string
  83. */
  84. public function name(): string
  85. {
  86. return $this->name;
  87. }
  88. /**
  89. * @return string
  90. */
  91. public function slug(): string
  92. {
  93. return $this->slug;
  94. }
  95. /**
  96. * @return string
  97. */
  98. public function attributeType(): string
  99. {
  100. return $this->attributeType;
  101. }
  102. /**
  103. * @return Collection|null
  104. */
  105. public function choices(): ?Collection
  106. {
  107. return $this->choices;
  108. }
  109. /**
  110. * @return \DateTime
  111. */
  112. public function createdAt(): \DateTime
  113. {
  114. return $this->createdAt;
  115. }
  116. /**
  117. * @return \DateTime
  118. */
  119. public function updatedAt(): \DateTime
  120. {
  121. return $this->updatedAt;
  122. }
  123. /**
  124. * @return bool
  125. */
  126. public function equals(WhaterPointAttributeType $whaterPointAttributeType)
  127. {
  128. return $this->id() === $whaterPointAttributeType->id();
  129. }
  130. }