<?php
namespace App\Entity\Gauge;
use App\Entity\Client\Client;
use App\Entity\Extension\BlameableTrait;
use App\Entity\Extension\TimestampableTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use App\Repository\Gauge\ConsumptionLimitRepository;
/**
* @ORM\Entity(repositoryClass=ConsumptionLimitRepository::class)
* @ORM\Table(name="consumption_limit")
*/
class ConsumptionLimit
{
use BlameableTrait;
use TimestampableTrait;
public const WEEKEND_KEY = 'weekend';
public const WORKDAY_KEY = 'workday';
/**
* @var null|int
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private ?int $id = null;
/**
* @var Client
* @ORM\ManyToOne(targetEntity="App\Entity\Client\Client")
* @ORM\JoinColumn(name="client_id", referencedColumnName="id", nullable=false)
*/
private Client $client;
/**
* @var string
* @ORM\Column(type="string")
*
*/
private string $name;
/**
* @var int
* @ORM\Column(type="integer")
*
*/
private int $gaugeType;
/**
* @var string
* @ORM\Column(type="string")
*/
protected string $systemCode;
/**
* @var int
* @ORM\Column(type="integer")
*/
private int $unit;
/**
* @var null|string
*
* @ORM\Column(type="text", nullable=true)
*/
private ?string $note;
/**
* @var bool
*
* @ORM\Column(type="boolean")
*/
private bool $enabled = true;
/**
* @var Collection|Gauge[]
*
* @ORM\ManyToMany(targetEntity="App\Entity\Gauge\Gauge", inversedBy="consumptionLimits", cascade={"persist"})
*/
protected $gauges;
/**
* @var Collection|ConsumptionLimitItem[]
* @ORM\OneToMany(targetEntity="App\Entity\Gauge\ConsumptionLimitItem", mappedBy="consumptionLimit", cascade={"all"}, orphanRemoval=true)
*/
private $items;
/**
* @var null|int
*
* @ORM\Column(type="integer", nullable=true)
*/
protected ?int $serialNumber = null;
public function __construct()
{
$this->gauges = new ArrayCollection();
$this->items = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function setId(?int $id): ConsumptionLimit
{
$this->id = $id;
return $this;
}
public function getClient(): Client
{
return $this->client;
}
public function setClient(Client $client): ConsumptionLimit
{
$this->client = $client;
return $this;
}
public function getName(): string
{
return $this->name;
}
public function setName(string $name): ConsumptionLimit
{
$this->name = $name;
return $this;
}
public function getGaugeType(): int
{
return $this->gaugeType;
}
public function setGaugeType(int $gaugeType): ConsumptionLimit
{
$this->gaugeType = $gaugeType;
return $this;
}
public function getSystemCode(): string
{
return $this->systemCode;
}
public function setSystemCode(string $systemCode): ConsumptionLimit
{
$this->systemCode = $systemCode;
return $this;
}
public function getUnit(): int
{
return $this->unit;
}
public function setUnit(int $unit): ConsumptionLimit
{
$this->unit = $unit;
return $this;
}
public function getNote(): ?string
{
return $this->note;
}
public function setNote(?string $note): ConsumptionLimit
{
$this->note = $note;
return $this;
}
public function isEnabled(): bool
{
return $this->enabled;
}
public function setEnabled(bool $enabled): ConsumptionLimit
{
$this->enabled = $enabled;
return $this;
}
/**
* @return Gauge[]|Collection
*/
public function getGauges()
{
return $this->gauges;
}
/**
* @param Gauge[]|Collection $gauges
* @return ConsumptionLimit
*/
public function setGauges($gauges)
{
$this->gauges = $gauges;
return $this;
}
/**
* @return ConsumptionLimitItem[]|ArrayCollection|Collection
*/
public function getItems()
{
return $this->items;
}
/**
* @param ConsumptionLimitItem[]|ArrayCollection|Collection $items
* @return ConsumptionLimit
*/
public function setItems($items)
{
$this->items = $items;
return $this;
}
public function getItemsWithKeys(): array
{
$items = [];
foreach ($this->items as $item) {
$dayKey = $item->isWeekend() ? self::WEEKEND_KEY : self::WORKDAY_KEY;
$items[$item->getHour()][$dayKey] = $item->getValue();
}
return $items;
}
public function addNewHourData(int $hour, string $dayKey, float $limitValue): self
{
$newItem = (new ConsumptionLimitItem())
->setHour($hour)
->setWeekend($dayKey === self::WEEKEND_KEY)
->setValue($limitValue)
->setConsumptionLimit($this)
;
$this->items->add($newItem);
return $this;
}
public function getSerialNumber(): ?int
{
return $this->serialNumber;
}
public function setSerialNumber(?int $serialNumber): ConsumptionLimit
{
$this->serialNumber = $serialNumber;
return $this;
}
/**
* üsed for log data
* @return array
*/
public function toArray(): array
{
return [
'id' => $this->getId(),
'systemCode' => $this->getSystemCode(),
'name' => $this->getName(),
];
}
}