Mistralys /
application-utils
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * File containing the {@see AppUtils\ConvertHelper_IntervalConverter} class. |
||
| 4 | * |
||
| 5 | * @package Application Utils |
||
| 6 | * @subpackage ConvertHelper |
||
| 7 | * @see AppUtils\ConvertHelper_IntervalConverter |
||
| 8 | */ |
||
| 9 | |||
| 10 | declare(strict_types=1); |
||
| 11 | |||
| 12 | namespace AppUtils; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Converts date intervals to human-readable strings. |
||
| 16 | * |
||
| 17 | * @package Application Utils |
||
| 18 | * @subpackage ConvertHelper |
||
| 19 | * @author Sebastian Mordziol <[email protected]> |
||
| 20 | |||
| 21 | * @see ConvertHelper::interval2string() |
||
| 22 | */ |
||
| 23 | class ConvertHelper_IntervalConverter |
||
| 24 | { |
||
| 25 | public const ERROR_MISSING_TRANSLATION = 43501; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var array<string,string>|NULL |
||
| 29 | */ |
||
| 30 | protected static $texts = null; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string[] |
||
| 34 | */ |
||
| 35 | protected $tokens = array('y', 'm', 'd', 'h', 'i', 's'); |
||
| 36 | |||
| 37 | public function __construct() |
||
| 38 | { |
||
| 39 | if(class_exists('\AppLocalize\Localization')) { |
||
| 40 | \AppLocalize\Localization::onLocaleChanged(array($this, 'handle_localeChanged')); |
||
|
0 ignored issues
–
show
|
|||
| 41 | } |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Called whenever the application locale has changed, |
||
| 46 | * to reset the internal translation cache. |
||
| 47 | */ |
||
| 48 | public function handle_localeChanged() : void |
||
| 49 | { |
||
| 50 | self::$texts = null; |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Converts the specified interval to a human-readable |
||
| 55 | * string, e.g. "2 hours and 4 minutes". |
||
| 56 | * |
||
| 57 | * @param \DateInterval $interval |
||
| 58 | * @return string |
||
| 59 | * @throws ConvertHelper_Exception |
||
| 60 | * |
||
| 61 | * @see ConvertHelper_IntervalConverter::ERROR_MISSING_TRANSLATION |
||
| 62 | */ |
||
| 63 | public function toString(\DateInterval $interval) : string |
||
| 64 | { |
||
| 65 | $this->initTexts(); |
||
| 66 | |||
| 67 | $interval = parseInterval($interval); |
||
| 68 | |||
| 69 | $keep = $this->resolveTokens($interval); |
||
| 70 | |||
| 71 | $parts = array(); |
||
| 72 | foreach($keep as $token) |
||
| 73 | { |
||
| 74 | $value = $interval->getToken($token); |
||
| 75 | if($value <= 0) { |
||
| 76 | continue; |
||
| 77 | } |
||
| 78 | |||
| 79 | $parts[] = $this->translateToken($token, $interval); |
||
| 80 | } |
||
| 81 | |||
| 82 | if(count($parts) == 1) { |
||
| 83 | return $parts[0]; |
||
| 84 | } |
||
| 85 | |||
| 86 | $last = array_pop($parts); |
||
| 87 | |||
| 88 | return t('%1$s and %2$s', implode(', ', $parts), $last); |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Translates the selected time token, e.g. "y" (for year). |
||
| 93 | * |
||
| 94 | * @param string $token |
||
| 95 | * @param ConvertHelper_DateInterval $interval |
||
| 96 | * @throws ConvertHelper_Exception |
||
| 97 | * @return string |
||
| 98 | */ |
||
| 99 | protected function translateToken(string $token, ConvertHelper_DateInterval $interval) : string |
||
| 100 | { |
||
| 101 | $value = $interval->getToken($token); |
||
| 102 | |||
| 103 | $suffix = 'p'; |
||
| 104 | if($value == 1) { $suffix = 's'; } |
||
| 105 | $token .= $suffix; |
||
| 106 | |||
| 107 | if(!isset(self::$texts[$token])) |
||
| 108 | { |
||
| 109 | throw new ConvertHelper_Exception( |
||
| 110 | 'Missing interval translation', |
||
| 111 | sprintf( |
||
| 112 | 'The format [%s] does not exist in the texts.', |
||
| 113 | $token |
||
| 114 | ), |
||
| 115 | self::ERROR_MISSING_TRANSLATION |
||
| 116 | ); |
||
| 117 | } |
||
| 118 | |||
| 119 | return str_replace( |
||
| 120 | '$value', |
||
| 121 | (string)$value, |
||
| 122 | self::$texts[$token] |
||
| 123 | ); |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Resolves all time tokens that need to be translated in |
||
| 128 | * the subject interval, depending on its length. |
||
| 129 | * |
||
| 130 | * @param ConvertHelper_DateInterval $interval |
||
| 131 | * @return string[] |
||
| 132 | */ |
||
| 133 | protected function resolveTokens(ConvertHelper_DateInterval $interval) : array |
||
| 134 | { |
||
| 135 | $offset = 0; |
||
| 136 | |||
| 137 | foreach($this->tokens as $token) |
||
| 138 | { |
||
| 139 | if($interval->getToken($token) > 0) |
||
| 140 | { |
||
| 141 | return array_slice($this->tokens, $offset); |
||
| 142 | } |
||
| 143 | |||
| 144 | $offset++; |
||
| 145 | } |
||
| 146 | |||
| 147 | return array(); |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Initializes the translateable strings. |
||
| 152 | */ |
||
| 153 | protected function initTexts() : void |
||
| 154 | { |
||
| 155 | if(isset(self::$texts)) { |
||
| 156 | return; |
||
| 157 | } |
||
| 158 | |||
| 159 | self::$texts = array( |
||
| 160 | 'ys' => t('1 year'), |
||
| 161 | 'yp' => t('%1$s years', '$value'), |
||
| 162 | 'ms' => t('1 month'), |
||
| 163 | 'mp' => t('%1$s months', '$value'), |
||
| 164 | 'ds' => t('1 day'), |
||
| 165 | 'dp' => t('%1$s days', '$value'), |
||
| 166 | 'hs' => t('1 hour'), |
||
| 167 | 'hp' => t('%1$s hours', '$value'), |
||
| 168 | 'is' => t('1 minute'), |
||
| 169 | 'ip' => t('%1$s minutes', '$value'), |
||
| 170 | 'ss' => t('1 second'), |
||
| 171 | 'sp' => t('%1$s seconds', '$value'), |
||
| 172 | ); |
||
| 173 | } |
||
| 174 | } |
||
| 175 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths