AbstractUnit.php 970 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace App\Yr\Forecast\Tabular\Time;
  3. abstract class AbstractUnit implements DiffInterface {
  4. const DEFAULT_VARIANCE = 2;
  5. private $value;
  6. private $unit;
  7. public function __construct(float $value, string $unit) {
  8. $this->value = $value;
  9. $this->unit = $unit;
  10. }
  11. public function getValue(): float {
  12. return $this->value;
  13. }
  14. public function getUnit(): string {
  15. return $this->unit;
  16. }
  17. public function setThresholdValue(int $t): self {
  18. $this->threshold = $t;
  19. }
  20. public function diff(DiffInterface $d): bool {
  21. if ($d instanceof $this)
  22. return abs($this->value - $d->getValue()) > static::DEFAULT_VARIANCE;
  23. return false;
  24. }
  25. public function __toString(): string {
  26. return sprintf(
  27. "%s: %f %s", basename(str_replace(
  28. '\\', DIRECTORY_SEPARATOR, get_class($this)
  29. )), $this->value, $this->unit);
  30. }
  31. }