Skip to content

Commit e19afd7

Browse files
committed
low minute addition
1 parent 1aaff52 commit e19afd7

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

README.md

+24-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ And if you have multiple hour letters you can set these in the constructor, see
1414
* 2t20 = 140
1515
* 3x84s = 264
1616

17+
##### New in 2.0
18+
19+
* You can now add a low minute, meaning if set it to 4, then if you write 3, it will become 3 hours, if you write 5 it will still be 5 minutes.
20+
* By default the `low minute` variable is NULL, meaning its not used
21+
1722
### Install
1823

1924
`composer require lsv/timestringparser`
@@ -33,7 +38,13 @@ to your `composer.json`
3338
### Usage
3439

3540
By standard hour letter is `h` and minute letter is `m` but these can be customized in the constructor `new TimestringParser(['h','t','u'], ['m','x','y']);` now `h`, `t` and `u` can be used to parse the hour part of the time string, and the letters `m`, `x`, `y` can be used to parse the minute part of the time string
36-
41+
42+
##### New in 2.0
43+
44+
You can set the `low minute` variable in the constructor `new TimestringParser(['h','t','u'], ['m','x','y'], <low minute>);`
45+
46+
It needs to be a integer
47+
3748
### Examples
3849

3950
```php
@@ -47,6 +58,18 @@ foreach($timestrings as $string) {
4758
}
4859
```
4960

61+
##### Low minute example
62+
63+
```
64+
$timestrings = [1, 2, '3', '4', 5];
65+
$parser = new TimestringParser(['h'], ['m'], 3);
66+
echo $parser->parseTimeString(1); // Returns 60
67+
echo $parser->parseTimeString(2); // Returns 120
68+
echo $parser->parseTimeString('3'); // Returns 180
69+
echo $parser->parseTimeString('4'); // Returns 4
70+
echo $parser->parseTimeString(5); // Returns 5
71+
```
72+
5073
### License
5174

5275
The MIT License (MIT)

src/TimestringParser.php

+16-1
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,19 @@ class TimestringParser
4545
*/
4646
protected $hourLetters;
4747

48-
public function __construct(array $hourLetters = ['h'], array $minuteLetters = ['m'])
48+
/**
49+
* Set this to a number, and if this value is 5,
50+
* then everything you write equals or lower than this, will become hours
51+
*
52+
* @var int
53+
*/
54+
public $lowMinutes;
55+
56+
public function __construct(array $hourLetters = ['h'], array $minuteLetters = ['m'], int $lowMinutes = null)
4957
{
5058
$this->hourLetters = $hourLetters;
5159
$this->minuteLetters = $minuteLetters;
60+
$this->lowMinutes = $lowMinutes;
5261
}
5362

5463
/**
@@ -103,6 +112,12 @@ private function parseTime(string $time) : int
103112
return $minutes;
104113
}
105114

115+
if ($this->lowMinutes !== null) {
116+
if (((int)$time) <= $this->lowMinutes) {
117+
return ((int)$time) * 60;
118+
}
119+
}
120+
106121
return (int) $time;
107122
}
108123

tests/TimestringParserTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class TimestringParserTest extends TestCase
1212
public function dataProvider()
1313
{
1414
return [
15+
['1', 1],
1516
['3:20', (3 * 60) + 20],
1617
['4h48', (4 * 60) + 48],
1718
['3h 20m', (3 * 60) + 20],
@@ -80,4 +81,27 @@ public function test_readme_examples(string $string, int $expected) : void
8081
$this->assertEquals($expected, $parser->parseTimeString($string));
8182
}
8283

84+
public function dataProviderLowMinutes() : array
85+
{
86+
return [
87+
[3, (3 * 60), 3],
88+
[3, 3, 2],
89+
[3, (3 * 60), 4],
90+
[10, 10 * 60, 10],
91+
];
92+
}
93+
94+
/**
95+
* @dataProvider dataProviderLowMinutes
96+
*
97+
* @param int $number
98+
* @param int $expected
99+
* @param int $lowinutes
100+
*/
101+
public function test_low_minutes(int $number, int $expected, int $lowinutes) : void
102+
{
103+
$parser = new TimestringParser(['h'], ['m'], $lowinutes);
104+
$this->assertEquals($expected, $parser->parseTimeString($number));
105+
}
106+
83107
}

0 commit comments

Comments
 (0)