1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: wechsler |
5
|
|
|
* Date: 03/03/2017 |
6
|
|
|
* Time: 18:11 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Phase\TakeATicket\SongLoader; |
10
|
|
|
|
11
|
|
|
use Doctrine\DBAL\Exception\InvalidArgumentException; |
12
|
|
|
use Phase\TakeATicket\DataSource\AbstractSql; |
13
|
|
|
use Phase\TakeATicket\Model\Instrument; |
14
|
|
|
use Phase\TakeATicket\Model\Platform; |
15
|
|
|
use Phase\TakeATicket\Model\Song; |
16
|
|
|
use Phase\TakeATicket\Model\Source; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Imports an XLS file with Column A: Artist, B: Song Title, C: Duration (min:sec) |
20
|
|
|
*/ |
21
|
|
|
class SimpleKaraokeRowMapper implements RowMapperInterface |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
const INPUT_FIELD_ARTIST = 'artist'; |
25
|
|
|
const INPUT_FIELD_TITLE = 'title'; |
26
|
|
|
const INPUT_FIELD_DURATION_MMSS = 'duration_mmss'; |
27
|
|
|
const INPUT_FIELD_DURATION = 'duration'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Column map for input file |
31
|
|
|
* |
32
|
|
|
* @var string[] |
33
|
|
|
*/ |
34
|
|
|
protected $fileFields = [ |
35
|
|
|
'A' => self::INPUT_FIELD_ARTIST, |
36
|
|
|
'B' => self::INPUT_FIELD_TITLE, |
37
|
|
|
'C' => self::INPUT_FIELD_DURATION_MMSS, |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
protected $fieldLookup = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var AbstractSql |
44
|
|
|
*/ |
45
|
|
|
protected $dataStore; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var Instrument |
49
|
|
|
*/ |
50
|
|
|
protected $vocals; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var Platform |
54
|
|
|
*/ |
55
|
|
|
protected $platform; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var Platform |
59
|
|
|
*/ |
60
|
|
|
protected $source; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* RclRowMapper constructor. |
64
|
|
|
* @param $dataStore |
65
|
|
|
*/ |
66
|
|
|
public function __construct(AbstractSql $dataStore) |
67
|
|
|
{ |
68
|
|
|
$this->dataStore = $dataStore; |
69
|
|
|
$this->fieldLookup = array_flip($this->fileFields); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get formatter name for interface / selection |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
public function getFormatterName() |
78
|
|
|
{ |
79
|
|
|
return 'Simple Karaoke formatter'; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
protected function getDefaultPlatformName() |
83
|
|
|
{ |
84
|
|
|
return 'Karaoke'; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Initialise the RowMapper |
89
|
|
|
* |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
|
|
public function init() |
93
|
|
|
{ |
94
|
|
|
$vocals = (new Instrument())->setId(1)->setName('Vocals')->setAbbreviation('V'); |
95
|
|
|
$this->dataStore->storeInstrument($vocals); |
96
|
|
|
$this->vocals = $vocals; |
97
|
|
|
|
98
|
|
|
$platform = new Platform($this->getDefaultPlatformName()); |
99
|
|
|
$this->dataStore->storePlatform($platform); |
100
|
|
|
$this->platform = $platform; |
101
|
|
|
|
102
|
|
|
$source = new Source('Karaoke'); |
103
|
|
|
$this->dataStore->storeSource($source); |
104
|
|
|
$this->source = $source; |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Store a single spreadsheet row |
109
|
|
|
* |
110
|
|
|
* @param array $row Character-indexed flattened DB row |
111
|
|
|
* @return bool True on success |
112
|
|
|
* @throws \Doctrine\DBAL\Exception\InvalidArgumentException |
113
|
|
|
*/ |
114
|
|
|
public function storeRawRow(array $row) |
115
|
|
|
{ |
116
|
|
|
$song = new Song(); |
117
|
|
|
$song |
118
|
|
|
->setArtist($row[$this->fieldLookup[self::INPUT_FIELD_ARTIST]]) |
119
|
|
|
->setTitle($row[$this->fieldLookup[self::INPUT_FIELD_TITLE]]) |
120
|
|
|
->setSourceId($this->source->getId()); |
121
|
|
|
|
122
|
|
|
$durationMS = trim($row[$this->fieldLookup[self::INPUT_FIELD_DURATION_MMSS]]); |
123
|
|
View Code Duplication |
if ($durationMS && preg_match('/^\s*(\d+):(\d+)\s*$/', $durationMS, $matches)) { |
|
|
|
|
124
|
|
|
$song->setDuration(($matches[1] * 60) + $matches[2]); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$this->dataStore->storeSong($song); // need id below |
128
|
|
|
|
129
|
|
|
try { |
130
|
|
|
$this->dataStore->storeSongPlatformLinks($song->getId(), [$this->platform->getId()]); |
131
|
|
|
$this->dataStore->storeSongInstrumentLinks($song->getId(), [$this->vocals->getId()]); |
132
|
|
|
} catch (InvalidArgumentException $e) { |
133
|
|
|
trigger_error($e->getMessage(), E_USER_WARNING); |
134
|
|
|
return false; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return true; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get short name for form keys, CLI etc |
142
|
|
|
* |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
|
|
public function getShortName() |
146
|
|
|
{ |
147
|
|
|
return 'Karaoke'; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..