|
| 1 | +<?php |
| 2 | + |
| 3 | +class DurationMetadataProvider implements MetadataProvider { |
| 4 | + |
| 5 | + public $capabilities = array('duration', 'movingDuration', 'stopDuration'); |
| 6 | + |
| 7 | + public function provides($key) { |
| 8 | + if (in_array($key, $this->capabilities)) {return TRUE;}; |
| 9 | + return FALSE; |
| 10 | + } |
| 11 | + |
| 12 | + public function has($target, $key) { |
| 13 | + return isset($target->metadata['metadatas'][__CLASS__]) && isset($target->metadata['metadatas'][__CLASS__][$key]) && !is_null($target->metadata['metadatas'][__CLASS__][$key]) && (in_array($key, $this->capabilities)) && ($target instanceof Collection); |
| 14 | + } |
| 15 | + |
| 16 | + public function get($target, $key, $options) { |
| 17 | + |
| 18 | + if ($target instanceof MultiLineString) { |
| 19 | + |
| 20 | + if ($key == 'duration') { |
| 21 | + $duration = 0; |
| 22 | + foreach ($target->components as $component) { |
| 23 | + $duration += $component->getMetadata('duration'); |
| 24 | + } |
| 25 | + return $duration; |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + |
| 30 | + if ($target instanceof LineString) { |
| 31 | + if ($key == 'duration') { |
| 32 | + $point_a = $target->startPoint(); |
| 33 | + $point_b = $target->endPoint(); |
| 34 | + |
| 35 | + if (!is_null($point_a->getMetadata('time')) && !is_null($point_b->getMetadata('time'))) { |
| 36 | + $time = strtotime($point_b->getMetadata('time')) - strtotime($point_a->getMetadata('time')); |
| 37 | + } |
| 38 | + return $time; |
| 39 | + } |
| 40 | + |
| 41 | + if ($key == 'stopDuration') { |
| 42 | + $duration = 0; |
| 43 | + foreach ($target->explode() as $LineString) { |
| 44 | + $point_a = $LineString->startPoint(); |
| 45 | + $point_b = $LineString->endPoint(); |
| 46 | + if (!is_null($point_a->getMetadata('time')) && !is_null($point_b->getMetadata('time'))) { |
| 47 | + $time = strtotime($point_b->getMetadata('time')) - strtotime($point_a->getMetadata('time')); |
| 48 | + } else { |
| 49 | + $time = 0; |
| 50 | + } |
| 51 | + |
| 52 | + $length = $LineString->greatCircleLength(); |
| 53 | + |
| 54 | + if ($length >= 0 && $length <= $options['threshold']) { |
| 55 | + $duration += $time; |
| 56 | + } |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | + return $duration; |
| 61 | + } |
| 62 | + |
| 63 | + if ($key == 'movingDuration') { |
| 64 | + return $this->get($target, 'duration', $options) - $this->get($target, 'stopDuration', $options); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | + public function set($target, $key, $value) { |
| 71 | + if ($key === 'duration') { |
| 72 | + $target->metadata['metadatas'][__CLASS__][$key] = $value; |
| 73 | + return TRUE; |
| 74 | + } |
| 75 | + return FALSE; |
| 76 | + } |
| 77 | + |
| 78 | + public function id() { |
| 79 | + return __CLASS__; |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments