sauls /
helpers
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * This file is part of the sauls/helpers package. |
||||
| 4 | * |
||||
| 5 | * @author Saulius Vaičeliūnas <[email protected]> |
||||
| 6 | * @link http://saulius.vaiceliunas.lt |
||||
| 7 | * @copyright 2018 |
||||
| 8 | * |
||||
| 9 | * For the full copyright and license information, please view the LICENSE |
||||
| 10 | * file that was distributed with this source code. |
||||
| 11 | */ |
||||
| 12 | |||||
| 13 | namespace Sauls\Component\Helper\Operation\DateTimeOperation; |
||||
| 14 | |||||
| 15 | use Sauls\Component\Helper\Operation\ArrayOperation; |
||||
| 16 | |||||
| 17 | class ElapsedTime extends AbstractOperation implements ElapsedTimeInterface |
||||
| 18 | { |
||||
| 19 | private $labels = [ |
||||
| 20 | 'singular' => [ |
||||
| 21 | '{year}' => 'yr', |
||||
| 22 | '{month}' => 'mo', |
||||
| 23 | '{week}' => 'w', |
||||
| 24 | '{day}' => 'd', |
||||
| 25 | '{hour}' => 'h', |
||||
| 26 | '{minute}' => 'm', |
||||
| 27 | '{second}' => 's', |
||||
| 28 | ], |
||||
| 29 | 'plural' => [ |
||||
| 30 | '{years}' => 'yr', |
||||
| 31 | '{months}' => 'mo', |
||||
| 32 | '{weeks}' => 'w', |
||||
| 33 | '{days}' => 'd', |
||||
| 34 | '{hours}' => 'h', |
||||
| 35 | '{minutes}' => 'm', |
||||
| 36 | '{seconds}' => 's', |
||||
| 37 | ], |
||||
| 38 | ]; |
||||
| 39 | |||||
| 40 | private $timeOffsets = [ |
||||
| 41 | ['{year}', '{years}', 31557600], |
||||
| 42 | ['{month}', '{months}', 2592000], |
||||
| 43 | ['{week}', '{weeks}', 604800], |
||||
| 44 | ['{day}', '{days}', 86400], |
||||
| 45 | ['{hour}', '{hours}', 3600], |
||||
| 46 | ['{minute}', '{minutes}', 60], |
||||
| 47 | ['{second}', '{seconds}', 1], |
||||
| 48 | ]; |
||||
| 49 | |||||
| 50 | |||||
| 51 | /** |
||||
| 52 | * @var ArrayOperation\MergeInterface |
||||
| 53 | */ |
||||
| 54 | private $arrayMergeOperation; |
||||
| 55 | |||||
| 56 | /** |
||||
| 57 | * @var ArrayOperation\GetValue |
||||
| 58 | */ |
||||
| 59 | private $arrayGetValueOperation; |
||||
| 60 | |||||
| 61 | 1 | public function __construct( |
|||
| 62 | ArrayOperation\MergeInterface $arrayMergeOperation, |
||||
| 63 | ArrayOperation\GetValue $arrayGetValueOperation |
||||
| 64 | ) { |
||||
| 65 | 1 | $this->arrayMergeOperation = $arrayMergeOperation; |
|||
| 66 | 1 | $this->arrayGetValueOperation = $arrayGetValueOperation; |
|||
| 67 | 1 | } |
|||
| 68 | |||||
| 69 | /** |
||||
| 70 | * @throws \Sauls\Component\Helper\Exception\PropertyNotAccessibleException |
||||
| 71 | */ |
||||
| 72 | 15 | public function execute($date, array $labels, string $format = self::ELAPSED_TIME_FORMAT_FULL): string |
|||
| 73 | { |
||||
| 74 | 15 | $elapsedTimeLabels = $this->arrayMergeOperation->execute($this->labels, $labels); |
|||
| 75 | |||||
| 76 | 15 | $date = $this->createDateObject($date); |
|||
| 77 | |||||
| 78 | 15 | $time = $date->getTimestamp(); |
|||
| 79 | 15 | $timeDifference = time() - $time; |
|||
| 80 | 15 | $timeLeftValues = []; |
|||
| 81 | |||||
| 82 | 15 | foreach ($this->timeOffsets as [$timeSingle, $timePlural, $offset]) { |
|||
| 83 | 15 | if ($timeDifference >= $offset) { |
|||
| 84 | |||||
| 85 | 15 | $timeLeft = floor($timeDifference / $offset); |
|||
| 86 | 15 | $timeDifference -= ($timeLeft * $offset); |
|||
| 87 | 15 | $timeLeftValues[] = $this->formatLabel((int)$timeLeft, $elapsedTimeLabels, [ |
|||
| 88 | 15 | 'singular' => $timeSingle, |
|||
| 89 | 15 | 'plural' => $timePlural, |
|||
| 90 | ]); |
||||
| 91 | } |
||||
| 92 | } |
||||
| 93 | |||||
| 94 | 15 | return $this->outputElapsedTime($timeLeftValues, $format); |
|||
| 95 | } |
||||
| 96 | |||||
| 97 | /** |
||||
| 98 | * @throws \Sauls\Component\Helper\Exception\PropertyNotAccessibleException |
||||
| 99 | */ |
||||
| 100 | 15 | private function formatLabel(int $timeLeft, array $labels, array $timeStrings): string |
|||
| 101 | { |
||||
| 102 | 15 | return \sprintf( |
|||
| 103 | 15 | '%s%s', $timeLeft, |
|||
| 104 | 15 | (1 === $timeLeft |
|||
| 105 | 9 | ? $this->formatSingularLabel($labels, $timeStrings) |
|||
| 106 | 15 | : $this->formatPlurarLabel($labels, $timeStrings) |
|||
| 107 | ) |
||||
| 108 | ); |
||||
| 109 | } |
||||
| 110 | |||||
| 111 | /** |
||||
| 112 | * @throws \Sauls\Component\Helper\Exception\PropertyNotAccessibleException |
||||
| 113 | */ |
||||
| 114 | 9 | private function formatSingularLabel(array $labels, array $timeStrings): string |
|||
| 115 | { |
||||
| 116 | 9 | return \strtr( |
|||
| 117 | 9 | $this->arrayGetValueOperation->execute($timeStrings, 'singular', ''), |
|||
| 118 | 9 | $this->arrayGetValueOperation->execute($labels, 'singular', '') |
|||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 119 | ); |
||||
| 120 | } |
||||
| 121 | |||||
| 122 | /** |
||||
| 123 | * @throws \Sauls\Component\Helper\Exception\PropertyNotAccessibleException |
||||
| 124 | */ |
||||
| 125 | 15 | private function formatPlurarLabel(array $labels, array $timeStrings): string |
|||
| 126 | { |
||||
| 127 | 15 | return \strtr( |
|||
| 128 | 15 | $this->arrayGetValueOperation->execute($timeStrings, 'plural', ''), |
|||
| 129 | 15 | $this->arrayGetValueOperation->execute($labels, 'plural', '') |
|||
|
0 ignored issues
–
show
It seems like
$this->arrayGetValueOper...($labels, 'plural', '') can also be of type string; however, parameter $replace_pairs of strtr() does only seem to accept array, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 130 | ); |
||||
| 131 | } |
||||
| 132 | |||||
| 133 | /** |
||||
| 134 | * @throws \Sauls\Component\Helper\Exception\PropertyNotAccessibleException |
||||
| 135 | */ |
||||
| 136 | 15 | private function outputElapsedTime(array $timeLeftValues, string $format): string |
|||
| 137 | { |
||||
| 138 | 15 | if (self::ELAPSED_TIME_FORMAT_SHORT === $format) { |
|||
| 139 | 10 | return $this->arrayGetValueOperation->execute($timeLeftValues, 0, ''); |
|||
| 140 | } |
||||
| 141 | |||||
| 142 | 5 | return \implode(' ', $timeLeftValues); |
|||
| 143 | } |
||||
| 144 | } |
||||
| 145 |