Skip to content

Commit

Permalink
Added SecondsToTime string macro
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbaljet committed Feb 22, 2022
1 parent 8071c87 commit 6602871
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `laravel-mixins` will be documented in this file.

## 3.3.0 - 2022-02-22

- Added `SecondsToTime` string macro.

## 3.2.0 - 2022-02-04

- Added supprt for Laravel 9
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ There's no Service Provider or automatic discovery/registration of anything. All
* [Human Filesize](#human-filesize)
* [Text](#text)
* [URL](#url)
* [Seconds to time][#seconds-to-time]

#### PDF
* [PDF Regeneration](#pdf-regeneration)
Expand Down Expand Up @@ -271,6 +272,21 @@ $url = "protone.media";
Str::url($url);
```

### Seconds to time

Converts seconds to a 'mm:ss' / 'hh:mm:ss' format.

```php
Str::mixin(new ProtoneMedia\LaravelMixins\String\SecondsToTime);

Str::secondsToTime(10); // 00:10
Str::secondsToTime(580); // 09:40
Str::secondsToTime(3610); // 01:00:10

// force 'hh:mm:ss' format, even under an hour:
Str::secondsToTime(580, false); // 00:09:40
```

## PDF Regeneration

*Note: Requires the `symfony/process` package.*
Expand Down
25 changes: 25 additions & 0 deletions src/String/SecondsToTime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace ProtoneMedia\LaravelMixins\String;

class SecondsToTime
{
/**
* Returns a function containing the macro.
*
* @return callable
*/
public function secondsToTime(): callable
{
/**
* Converts seconds to 'mm:ss' / 'hh:mm:ss' format.
*/
return function (int $seconds, bool $omitHours = true): string {
if (!$omitHours || $seconds >= 3600) {
return sprintf('%02d:%02d:%02d', ($seconds / 3600), ($seconds / 60 % 60), $seconds % 60);
}

return sprintf('%02d:%02d', ($seconds / 60 % 60), $seconds % 60);
};
}
}
27 changes: 27 additions & 0 deletions tests/String/SecondsToTimeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace ProtoneMedia\Mixins\Tests\String;

use Illuminate\Support\Str;
use Orchestra\Testbench\TestCase;
use ProtoneMedia\LaravelMixins\String\SecondsToTime;

class SecondsToTimeTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

Str::mixin(new SecondsToTime);
}

/** @test */
public function it_can_format_seconds_as_time()
{
$this->assertEquals('01:00:10', Str::secondsToTime(3610));
$this->assertEquals('09:40', Str::secondsToTime(580));
$this->assertEquals('00:01', Str::secondsToTime(1));
$this->assertEquals('00:00', Str::secondsToTime(0));
$this->assertEquals('00:09:40', Str::secondsToTime(580, false));
}
}

0 comments on commit 6602871

Please sign in to comment.