AbstractUnit.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace App\Yr\Forecast\Tabular\Time;
  3. /**
  4. * Time-object entity should inherit this
  5. */
  6. abstract class AbstractUnit implements DiffInterface {
  7. const DEFAULT_VARIANCE = 2;
  8. private $value;
  9. private $unit;
  10. /**
  11. * @param float The value
  12. * @param float The unit
  13. */
  14. public function __construct(float $value, string $unit) {
  15. $this->value = $value;
  16. $this->unit = $unit;
  17. }
  18. /**
  19. * Get the value
  20. *
  21. * @return float
  22. */
  23. public function getValue(): float {
  24. return $this->value;
  25. }
  26. /**
  27. * Return the unit (e.g «degree»)
  28. *
  29. * @return string
  30. */
  31. public function getUnit(): string {
  32. return $this->unit;
  33. }
  34. /**
  35. * {@inheritDoc}
  36. */
  37. public function diff(DiffInterface $d): int {
  38. if ($d instanceof $this)
  39. return $this->value - $d->getValue();
  40. return 0;
  41. }
  42. public function __toString(): string {
  43. return sprintf(
  44. "%s: %f %s", basename(str_replace(
  45. '\\', DIRECTORY_SEPARATOR, get_class($this)
  46. )), $this->value, $this->unit);
  47. }
  48. }