src/Entity/Gauge/GaugeReading.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Gauge;
  3. use App\Entity\Extension\BlameableTrait;
  4. use App\Entity\Extension\ImportableInterface;
  5. use App\Entity\Extension\TimestampableTrait;
  6. use App\Entity\Gauge\RevisioRecord;
  7. use App\Entity\Import\DataImport;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\Common\Collections\Collection;
  10. use Doctrine\ORM\Mapping as ORM;
  11. use App\Repository\Gauge\GaugeReadingRepository;
  12. use App\Validator as AcmeAssert;
  13. use Symfony\Component\Validator\Constraints as Assert;
  14. /**
  15. * @ORM\Entity(repositoryClass=GaugeReadingRepository::class)
  16. * @ORM\Table(name="gauge_reading", indexes={@ORM\Index(name="read_at_idx", columns={"read_at"})})
  17. */
  18. class GaugeReading implements ImportableInterface
  19. {
  20. public const STATE_NEW = 0;
  21. public const STATE_BASIC = 1;
  22. public const STATE_FINAL = 2;
  23. /**
  24. * @var int|null
  25. * @ORM\Id()
  26. * @ORM\GeneratedValue()
  27. * @ORM\Column(type="integer")
  28. */
  29. private ?int $id = null;
  30. /**
  31. * @var Gauge $gauge
  32. * @ORM\ManyToOne(targetEntity="App\Entity\Gauge\Gauge", inversedBy="readings")
  33. * @ORM\JoinColumn(name="gauge_id", referencedColumnName="id", nullable=false)
  34. */
  35. private Gauge $gauge;
  36. /**
  37. * 0 = nový stav, 1 = odečet, 2 = konečný stav
  38. * @var int
  39. * @ORM\Column(type="smallint", options={"comment":"0 = nový stav, 1 = odečet, 2 = konečný stav"})
  40. */
  41. private int $state = self::STATE_NEW;
  42. /**
  43. * @var \DateTime
  44. * @AcmeAssert\DateTimeRange()
  45. * @ORM\Column(name="read_at", type="datetime", nullable=false)
  46. */
  47. protected \DateTime $readAt;
  48. /**
  49. * @var Collection|GaugeReadingValue[]
  50. * @ORM\OneToMany(targetEntity="App\Entity\Gauge\GaugeReadingValue", mappedBy="reading", cascade={"all"})
  51. */
  52. private $values;
  53. /**
  54. * is set if reading (state=0) is connected with gaugeUnit change
  55. * @var GaugeUnit|null
  56. * @ORM\OneToOne(targetEntity="App\Entity\Gauge\GaugeUnit", mappedBy="gaugeNewStateReading", cascade={"all"})
  57. */
  58. private ?GaugeUnit $gaugeUnit;
  59. /**
  60. * @var DataImport|null
  61. * @ORM\ManyToOne(targetEntity="App\Entity\Import\DataImport", inversedBy="readings")
  62. * @ORM\JoinColumn(nullable=true)
  63. */
  64. private ?DataImport $import = null;
  65. /**
  66. * record_id in Revisio source database, also referencing an entry in RevisioRecord
  67. * @var ?RevisioRecord
  68. * @ORM\ManyToOne(targetEntity="App\Entity\Gauge\RevisioRecord", cascade={"all"})
  69. * @ORM\JoinColumn(name="revisio_record_id", referencedColumnName="record_id", nullable=true)
  70. */
  71. private ?RevisioRecord $revisioRecord = null;
  72. use BlameableTrait;
  73. use TimestampableTrait;
  74. public function __construct()
  75. {
  76. $this->values = new ArrayCollection();
  77. }
  78. /**
  79. * @return int|null
  80. */
  81. public function getId(): ?int
  82. {
  83. return $this->id;
  84. }
  85. /**
  86. * @param int|null $id
  87. * @return GaugeReading
  88. */
  89. public function setId(?int $id): GaugeReading
  90. {
  91. $this->id = $id;
  92. return $this;
  93. }
  94. /**
  95. * @return Gauge
  96. */
  97. public function getGauge(): Gauge
  98. {
  99. return $this->gauge;
  100. }
  101. /**
  102. * @param Gauge $gauge
  103. * @return GaugeReading
  104. */
  105. public function setGauge(Gauge $gauge): GaugeReading
  106. {
  107. $this->gauge = $gauge;
  108. return $this;
  109. }
  110. /**
  111. * @return int
  112. */
  113. public function getState(): int
  114. {
  115. return $this->state;
  116. }
  117. /**
  118. * @param int $state
  119. * @return GaugeReading
  120. */
  121. public function setState(int $state): GaugeReading
  122. {
  123. $this->state = $state;
  124. return $this;
  125. }
  126. /**
  127. * @return \DateTime
  128. */
  129. public function getReadAt(): \DateTime
  130. {
  131. return $this->readAt;
  132. }
  133. /**
  134. * @param \DateTime $readAt
  135. * @return GaugeReading
  136. */
  137. public function setReadAt(\DateTime $readAt): GaugeReading
  138. {
  139. $this->readAt = $readAt;
  140. return $this;
  141. }
  142. /**
  143. * @return GaugeReadingValue[]|Collection
  144. */
  145. public function getValues()
  146. {
  147. return $this->values;
  148. }
  149. /**
  150. * @param GaugeReadingValue[]|Collection $values
  151. * @return GaugeReading
  152. */
  153. public function setValues($values)
  154. {
  155. $this->values = $values;
  156. return $this;
  157. }
  158. /**
  159. * @param GaugeReadingValue $value
  160. * @return $this
  161. */
  162. public function addValue(GaugeReadingValue $value): self
  163. {
  164. $value->setReading($this);
  165. $this->values->add($value);
  166. return $this;
  167. }
  168. /**
  169. * @return bool
  170. */
  171. public function isNew() :bool
  172. {
  173. return $this->getState() === self::STATE_NEW;
  174. }
  175. /**
  176. * @return bool
  177. */
  178. public function isFinal() :bool
  179. {
  180. return $this->getState() === self::STATE_FINAL;
  181. }
  182. /**
  183. * @param int $segment
  184. * @return float|null
  185. */
  186. public function getSegmentValue(int $segment) : ?float
  187. {
  188. foreach ($this->getValues() as $readingValue) {
  189. if ($readingValue->getSegment() === $segment) {
  190. return $readingValue->getValue();
  191. }
  192. }
  193. return null;
  194. }
  195. /**
  196. * @param int $segment
  197. * @return int|null
  198. */
  199. public function getSegmentUnit(int $segment) : ?int
  200. {
  201. foreach ($this->getValues() as $readingValue) {
  202. if ($readingValue->getSegment() === $segment) {
  203. return $readingValue->getUnit();
  204. }
  205. }
  206. return null;
  207. }
  208. /**
  209. * @param int $segment
  210. * @return \App\Entity\Gauge\GaugeReadingValue|null
  211. */
  212. public function getValueBySegment(int $segment): ?GaugeReadingValue
  213. {
  214. foreach ($this->values as $gRValue) {
  215. if ($segment === $gRValue->getSegment()) {
  216. return $gRValue;
  217. }
  218. }
  219. return null;
  220. }
  221. /**
  222. * @return \App\Entity\Gauge\GaugeUnit|null
  223. */
  224. public function getGaugeUnit(): ?GaugeUnit
  225. {
  226. return $this->gaugeUnit;
  227. }
  228. /**
  229. * @param \App\Entity\Gauge\GaugeUnit|null $gaugeUnit
  230. * @return GaugeReading
  231. */
  232. public function setGaugeUnit(?GaugeUnit $gaugeUnit): GaugeReading
  233. {
  234. $this->gaugeUnit = $gaugeUnit;
  235. return $this;
  236. }
  237. public function getImport(): ?DataImport
  238. {
  239. return $this->import;
  240. }
  241. public function setImport(?DataImport $import): GaugeReading
  242. {
  243. $this->import = $import;
  244. return $this;
  245. }
  246. /**
  247. * @return RevisioRecord|null
  248. */
  249. public function getRevisioRecord(): ?RevisioRecord
  250. {
  251. return $this->revisioRecord;
  252. }
  253. /**
  254. * @param RevisioRecord|null $revisioRecord
  255. * @return $this
  256. */
  257. public function setRevisioRecord(?RevisioRecord $revisioRecord): GaugeReading
  258. {
  259. $this->revisioRecord = $revisioRecord;
  260. return $this;
  261. }
  262. }