src/Domain/Whater/Model/WhaterPointAttribute.php line 19

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\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Ramsey\Uuid\Uuid;
  8. use Whater\Domain\Common\Exception\InvalidUUIDException;
  9. use Whater\Domain\User\Model\User;
  10. use Whater\Domain\Whater\Exception\InvalidWhaterPointAttributeValueException;
  11. /**
  12. * Class WhaterPointAttribute
  13. *
  14. * @package Whater\Domain\Whater\Model
  15. */
  16. class WhaterPointAttribute implements ContainsRecordedEvents
  17. {
  18. use EventRecorderCapabilities;
  19. const ATTR_STATUS_PRE_REGISTER = 'PRE_REGISTER';
  20. const ATTR_STATUS_VALIDATED = 'VALIDATED';
  21. const ATTR_STATUS_CERTIFIED = 'CERTIFIED';
  22. /**
  23. * @var string
  24. */
  25. private $uuid;
  26. /**
  27. * @var WhaterPoint
  28. */
  29. private $whaterPoint;
  30. /**
  31. * @var User
  32. */
  33. private $createdBy;
  34. /**
  35. * @var \DateTime
  36. */
  37. private $createdAt;
  38. /**
  39. * @var \DateTime
  40. */
  41. private $updatedAt;
  42. /**
  43. * @var string
  44. */
  45. private $valueText;
  46. /**
  47. * @var float
  48. */
  49. private $valueNumber;
  50. /**
  51. * @var string
  52. */
  53. private $valueStatus;
  54. /**
  55. * @var WhaterPointAttributeType
  56. */
  57. private $attributeType;
  58. public function __construct(
  59. string $whaterPointAttributeId = null,
  60. WhaterPoint $whaterPoint,
  61. WhaterPointAttributeType $attributeType,
  62. User $createdBy
  63. ) {
  64. try {
  65. $this->uuid = Uuid::fromString($whaterPointAttributeId ?: Uuid::uuid4())->toString();
  66. } catch (\InvalidArgumentException $e) {
  67. throw new InvalidUUIDException();
  68. }
  69. $this->whaterPoint = $whaterPoint;
  70. $this->createdBy = $createdBy;
  71. $this->attributeType = $attributeType;
  72. $this->valueStatus = WhaterPointAttribute::ATTR_STATUS_PRE_REGISTER;
  73. $this->createdAt = new \DateTime();
  74. $this->updatedAt = new \DateTime();
  75. }
  76. public function updateAttribute(
  77. string $valueStatus,
  78. $value = null
  79. ): WhaterPointAttribute {
  80. if ($this->attributeType->attributeType() == WhaterPointAttributeType::ATTR_VALUE_TEXT) {
  81. if ($value == null) {
  82. $this->valueText = null;
  83. } else {
  84. $this->valueText = "" . $value;
  85. }
  86. } else if ($this->attributeType->attributeType() == WhaterPointAttributeType::ATTR_VALUE_FLOAT) {
  87. if ($value == null) {
  88. $this->valueText = null;
  89. } else {
  90. $this->valueNumber = floatval($value);
  91. }
  92. } else if ($this->attributeType->attributeType() == WhaterPointAttributeType::ATTR_VALUE_CHOICES) {
  93. if ($value == null) {
  94. $this->valueText = null;
  95. } else {
  96. $validChoice = false;
  97. foreach ($this->attributeType->choices() as $choice) {
  98. if ($choice->id() == $value) {
  99. $this->valueText = $value;
  100. break;
  101. }
  102. }
  103. if (!$validChoice) {
  104. throw new InvalidWhaterPointAttributeValueException();
  105. }
  106. }
  107. } else {
  108. throw new InvalidWhaterPointAttributeValueException();
  109. }
  110. switch ($valueStatus) {
  111. case WhaterPointAttribute::ATTR_STATUS_PRE_REGISTER:
  112. case WhaterPointAttribute::ATTR_STATUS_CERTIFIED:
  113. case WhaterPointAttribute::ATTR_STATUS_VALIDATED:
  114. $this->valueStatus = $valueStatus;
  115. break;
  116. default:
  117. throw new InvalidWhaterPointAttributeValueException();
  118. }
  119. $this->updatedAt = new \DateTime();
  120. return $this;
  121. }
  122. /**
  123. * @return string
  124. */
  125. public function id(): string
  126. {
  127. return $this->uuid;
  128. }
  129. /**
  130. * @return WhaterPoint
  131. */
  132. public function whaterPoint(): WhaterPoint
  133. {
  134. return $this->whaterPoint;
  135. }
  136. /**
  137. * @return WhaterPointAttributeType
  138. */
  139. public function attributeType(): WhaterPointAttributeType
  140. {
  141. return $this->attributeType;
  142. }
  143. /**
  144. * @return User
  145. */
  146. public function createdBy(): User
  147. {
  148. return $this->createdBy;
  149. }
  150. public function value()
  151. {
  152. if ($this->attributeType->attributeType() == WhaterPointAttributeType::ATTR_VALUE_TEXT) {
  153. return $this->valueText;
  154. } else {
  155. return $this->valueNumber;
  156. }
  157. }
  158. /**
  159. * @return \DateTime
  160. */
  161. public function createdAt(): \DateTime
  162. {
  163. return $this->createdAt;
  164. }
  165. /**
  166. * @return \DateTime
  167. */
  168. public function updatedAt(): \DateTime
  169. {
  170. return $this->updatedAt;
  171. }
  172. /**
  173. * @return bool
  174. */
  175. public function equals(WhaterValoration $whaterValoration)
  176. {
  177. return $this->id() === $whaterValoration->id();
  178. }
  179. }