Length.class.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace Lib;
  3. class Length {
  4. private $lengths = array();
  5. public function __construct($registry) {
  6. $length_class_query = M()->query("SELECT * FROM " . C('DB_PREFIX') . "length_class");
  7. foreach ($length_class_query as $result) {
  8. $this->lengths[$result['length_class_id']] = array(
  9. 'length_class_id' => $result['length_class_id'],
  10. 'title' => $result['title'],
  11. 'unit' => $result['unit'],
  12. 'value' => $result['value']
  13. );
  14. }
  15. }
  16. public function convert($value, $from, $to) {
  17. if ($from == $to) {
  18. return $value;
  19. }
  20. if (isset($this->lengths[$from])) {
  21. $from = $this->lengths[$from]['value'];
  22. } else {
  23. $from = 1;
  24. }
  25. if (isset($this->lengths[$to])) {
  26. $to = $this->lengths[$to]['value'];
  27. } else {
  28. $to = 1;
  29. }
  30. return $value * ($to / $from);
  31. }
  32. public function format($value, $length_class_id, $decimal_point = '.', $thousand_point = ',') {
  33. if (isset($this->lengths[$length_class_id])) {
  34. return number_format($value, 2, $decimal_point, $thousand_point) . $this->lengths[$length_class_id]['unit'];
  35. } else {
  36. return number_format($value, 2, $decimal_point, $thousand_point);
  37. }
  38. }
  39. public function getUnit($length_class_id) {
  40. if (isset($this->lengths[$length_class_id])) {
  41. return $this->lengths[$length_class_id]['unit'];
  42. } else {
  43. return '';
  44. }
  45. }
  46. }