<?php
namespace Whater\Domain\Whater\Model;
use BornFree\TacticianDomainEvent\Recorder\ContainsRecordedEvents;
use BornFree\TacticianDomainEvent\Recorder\EventRecorderCapabilities;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Ramsey\Uuid\Uuid;
use Whater\Domain\Common\Exception\InvalidUUIDException;
use Whater\Domain\User\Model\User;
use Whater\Domain\Whater\Exception\InvalidWhaterPointAttributeValueException;
/**
* Class WhaterPointAttribute
*
* @package Whater\Domain\Whater\Model
*/
class WhaterPointAttribute implements ContainsRecordedEvents
{
use EventRecorderCapabilities;
const ATTR_STATUS_PRE_REGISTER = 'PRE_REGISTER';
const ATTR_STATUS_VALIDATED = 'VALIDATED';
const ATTR_STATUS_CERTIFIED = 'CERTIFIED';
/**
* @var string
*/
private $uuid;
/**
* @var WhaterPoint
*/
private $whaterPoint;
/**
* @var User
*/
private $createdBy;
/**
* @var \DateTime
*/
private $createdAt;
/**
* @var \DateTime
*/
private $updatedAt;
/**
* @var string
*/
private $valueText;
/**
* @var float
*/
private $valueNumber;
/**
* @var string
*/
private $valueStatus;
/**
* @var WhaterPointAttributeType
*/
private $attributeType;
public function __construct(
string $whaterPointAttributeId = null,
WhaterPoint $whaterPoint,
WhaterPointAttributeType $attributeType,
User $createdBy
) {
try {
$this->uuid = Uuid::fromString($whaterPointAttributeId ?: Uuid::uuid4())->toString();
} catch (\InvalidArgumentException $e) {
throw new InvalidUUIDException();
}
$this->whaterPoint = $whaterPoint;
$this->createdBy = $createdBy;
$this->attributeType = $attributeType;
$this->valueStatus = WhaterPointAttribute::ATTR_STATUS_PRE_REGISTER;
$this->createdAt = new \DateTime();
$this->updatedAt = new \DateTime();
}
public function updateAttribute(
string $valueStatus,
$value = null
): WhaterPointAttribute {
if ($this->attributeType->attributeType() == WhaterPointAttributeType::ATTR_VALUE_TEXT) {
if ($value == null) {
$this->valueText = null;
} else {
$this->valueText = "" . $value;
}
} else if ($this->attributeType->attributeType() == WhaterPointAttributeType::ATTR_VALUE_FLOAT) {
if ($value == null) {
$this->valueText = null;
} else {
$this->valueNumber = floatval($value);
}
} else if ($this->attributeType->attributeType() == WhaterPointAttributeType::ATTR_VALUE_CHOICES) {
if ($value == null) {
$this->valueText = null;
} else {
$validChoice = false;
foreach ($this->attributeType->choices() as $choice) {
if ($choice->id() == $value) {
$this->valueText = $value;
break;
}
}
if (!$validChoice) {
throw new InvalidWhaterPointAttributeValueException();
}
}
} else {
throw new InvalidWhaterPointAttributeValueException();
}
switch ($valueStatus) {
case WhaterPointAttribute::ATTR_STATUS_PRE_REGISTER:
case WhaterPointAttribute::ATTR_STATUS_CERTIFIED:
case WhaterPointAttribute::ATTR_STATUS_VALIDATED:
$this->valueStatus = $valueStatus;
break;
default:
throw new InvalidWhaterPointAttributeValueException();
}
$this->updatedAt = new \DateTime();
return $this;
}
/**
* @return string
*/
public function id(): string
{
return $this->uuid;
}
/**
* @return WhaterPoint
*/
public function whaterPoint(): WhaterPoint
{
return $this->whaterPoint;
}
/**
* @return WhaterPointAttributeType
*/
public function attributeType(): WhaterPointAttributeType
{
return $this->attributeType;
}
/**
* @return User
*/
public function createdBy(): User
{
return $this->createdBy;
}
public function value()
{
if ($this->attributeType->attributeType() == WhaterPointAttributeType::ATTR_VALUE_TEXT) {
return $this->valueText;
} else {
return $this->valueNumber;
}
}
/**
* @return \DateTime
*/
public function createdAt(): \DateTime
{
return $this->createdAt;
}
/**
* @return \DateTime
*/
public function updatedAt(): \DateTime
{
return $this->updatedAt;
}
/**
* @return bool
*/
public function equals(WhaterValoration $whaterValoration)
{
return $this->id() === $whaterValoration->id();
}
}