forked from kriskbx/gitlab-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimesCollection.php
More file actions
65 lines (55 loc) · 1.99 KB
/
TimesCollection.php
File metadata and controls
65 lines (55 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
namespace kriskbx\gtt\Collection;
use Illuminate\Support\Collection;
use kriskbx\gtt\Time;
/**
* Class TimesCollection
*
* Time should be rendered differently, so we extend the base Collection and implement our own toString method.
* @package kriskbx\gtt\Collection
*/
class TimesCollection extends Collection
{
/**
* Convert the times collection to a string.
*
* @param array $params
*
* @return string
*/
public function toString($params = [])
{
// Make sure things are set
$params['displayUserFunction'] = @$params['displayUserFunction'] ?: false;
$params['beforeHeadline'] = @$params['beforeHeadline'] ?: '';
$params['afterHeadline'] = @$params['afterHeadline'] ?: '';
$params['timesDelimiter'] = @$params['timesDelimiter'] ?: '';
$params['break'] = @$params['break'] ?: '';
$params['delimiter'] = $params['break'];
return $this->map(function (Collection $times, $user) use ($params) {
if ($params['displayUserFunction']) {
$string = call_user_func_array($params['displayUserFunction'], [$times, $user, $params]);
} else {
$string = $this->displayUser($times, $user, $params);
}
$string .= $times->toString($params);
return $string;
})->implode($params['timesDelimiter']);
}
/**
* Default function that "displays" a user.
*
* @param Collection $times
* @param string $user
* @param array $params
*
* @return string
*/
protected function displayUser(Collection $times, $user, array $params)
{
$totalTime = Time::humanReadable($times->reduce(function ($carry, Time $item) {
return $carry + (int)$item->getSeconds();
}), @$params['hoursPerDay'], @$params['timeFormat']);
return "{$params['beforeHeadline']}$user: {$totalTime}{$params['afterHeadline']}{$params['break']}";
}
}