-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSongCollection.php
More file actions
72 lines (52 loc) · 1.17 KB
/
SongCollection.php
File metadata and controls
72 lines (52 loc) · 1.17 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
66
67
68
69
70
71
72
<?php
/**
* Summary goes here and ends in a full stop plus blank line.
*
* Description goes here and goes across
* Two or more lines
*
* @author: tim
* Date: 18/05/2017 23:27
* @license:
*/
namespace MusicApp;
class SongCollection extends BaseCollection {
protected $items;
/**
* @var \DateTime
*/
private $totalDuration;
public function __construct(Song ...$songs) {
$this->items = $songs;
}
public function addItem(Song $song) {
$this->items[] = $song;
}
/**
* calculate duration from individual song durations
*/
public function setDuration()
{
$this->totalDuration = new \DateTime('@0');
foreach ($this->items as $song) {
$seconds = $this->timeToSeconds($song->getDuration());;
$this->totalDuration->add( date_interval_create_from_date_string($seconds.'seconds'));
}
}
/**
* @return \DateTime
*/
public function getTotalDuration() {
if (empty($this->totalDuration)) {
$this->setDuration();
}
return $this->totalDuration;
}
private function timeToSeconds($his)
{
$time = explode(':',$his);
$seconds = $time[ count($time) - 1 ];
$minutes = $time[ count($time) - 2 ];
return $seconds + ($minutes * 60);
}
}