forked from jimmiw/php-time-ago
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minor refactor to not use trait, extend readme
- Loading branch information
Zsolt Gál
committed
Jun 22, 2018
1 parent
c802acc
commit e05a9d0
Showing
6 changed files
with
151 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/Technodelight/TimeAgo/Translation/DefaultSecondsDurationMap.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace Technodelight\TimeAgo\Translation; | ||
|
||
class DefaultSecondsDurationMap implements SecondsDurationMap | ||
{ | ||
/** | ||
* Durations mapped to values in seconds | ||
* | ||
* @var array [['duration' => 123]] | ||
*/ | ||
private $secondsDurationMap = array( | ||
'sec' => 1, | ||
'min' => 60, | ||
'hour' => 3600, | ||
'day' => 86400, | ||
'month' => 2592000, | ||
'year' => 31104000, | ||
); | ||
|
||
/** | ||
* Converts a definition like "1day" into seconds | ||
* | ||
* @param string $human | ||
* @return int | ||
*/ | ||
public function inSeconds($human) | ||
{ | ||
$parts = explode(' ', preg_replace('~-\s+~', '-', $human)); | ||
$seconds = 0; | ||
foreach ($parts as $def) { | ||
$operand = '+'; | ||
if (strpos($def, '-') !== false) { | ||
$def = substr($def, strpos($def, '-') + 1); | ||
$operand = '-'; | ||
} | ||
sscanf($def, '%d%s', $amount, $duration); | ||
|
||
$seconds += ($this->durationToSeconds($duration, $amount) * ($operand == '+' ? 1 : -1)); | ||
} | ||
|
||
return $seconds; | ||
} | ||
|
||
/** | ||
* Returns an amount for a duration type | ||
* | ||
* @param string $duration | ||
* @return int | ||
*/ | ||
public function amountForDuration($duration) | ||
{ | ||
if (isset($this->secondsDurationMap[$duration])) { | ||
return $this->secondsDurationMap[$duration]; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
/** | ||
* @param string $duration | ||
* @param int $amount | ||
* @return int | ||
*/ | ||
private function durationToSeconds($duration, $amount) | ||
{ | ||
return $amount * $this->secondsDurationMap[$duration]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters