Fix a bug where contributors array was being encoded as an object.
[rock.divinelegy.git] / Domain / Entities / StepMania / Pack.php
1 <?php
2
3 namespace Domain\Entities\StepMania;
4
5 use Exception;
6 use Domain\Entities\StepMania\ISimfile;
7 use Domain\Entities\IUser;
8 use Domain\Entities\IFile;
9 use Domain\Entities\StepMania\IPack;
10 use Domain\Entities\AbstractEntity;
11
12 class Pack extends AbstractEntity implements IPack
13 {
14 private $_title;
15 private $_uploader;
16 private $_simfiles;
17 private $_banner;
18 private $_file;
19
20 public function __construct(
21 $title,
22 IUser $uploader,
23 array $simfiles,
24 IFile $banner = null,
25 IFile $file = null
26 ) {
27 $this->_title = $title;
28 $this->_uploader = $uploader;
29 $this->_banner = $banner;
30 $this->_file = $file;
31
32 foreach($simfiles as $simfile) {
33 if(!$simfile instanceof ISimfile) {
34 throw new Exception('Invalid Simfile array. All elements must be an instance of ISimfile.');
35 }
36 }
37
38 $this->_simfiles = $simfiles;
39 }
40
41 public function getContributors() {
42 $contributors = array();
43 foreach($this->_simfiles as $simfile)
44 {
45 /* @var $simfile \Domain\Entities\StepMania\Simfile */
46 $contributors = array_unique(
47 array_merge($contributors, $this->getAllStepArtistsFromSimfile($simfile))
48 );
49 }
50
51 //XXX: If there are duplicate contributors, say, index 5 and 6 are the same
52 //then we loose index 5 and have an array like 1,2,3,4,6
53 //
54 //This makes json_encode use the indicies as object keys, so we need to
55 //reshuffle the array into a continuous list
56 return array_values($contributors);
57 }
58
59 public function getFile()
60 {
61 return $this->_file;
62 }
63
64 public function getSimfiles()
65 {
66 return $this->_simfiles;
67 }
68
69 public function getTitle()
70 {
71 return $this->_title;
72 }
73
74 public function getUploader()
75 {
76 return $this->_uploader;
77 }
78
79 public function getBanner()
80 {
81 return $this->_banner;
82 }
83
84 private function getAllStepArtistsFromSimfile(ISimfile $simfile)
85 {
86 $artists = array();
87 foreach($simfile->getSteps() as $steps)
88 {
89 /* @var $steps \Domain\VOs\StepMania\StepChart */
90 if($steps->getArtist()->getTag() && !in_array($steps->getArtist()->getTag(), $artists)) $artists[] = $steps->getArtist()->getTag();
91 }
92
93 return $artists;
94 }
95 }