Blame view
app/Queries/StageQuery.php
4 KB
e77200db5 Initial commit |
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
<?php namespace FootyRoom\Queries; use Illuminate\Database\Connection; class StageQuery { /** * @var \Illuminate\Database\Connection */ protected $mysql; /** * Constructor. * * @param \Illuminate\Database\Connection $mysql */ public function __construct(Connection $mysql) { $this->mysql = $mysql; } /** * Find seasons of specified competition. * * Only returns seasons that actually have matches with review posts. * * @param int $competitionId * * @return object[] */ public function findSeasons($competitionId) { $matches = $this->mysql->select( 'SELECT DISTINCT stage_tree FROM matches WHERE stage_tree LIKE ? OR stage_tree = ?', ["$competitionId,%", "$competitionId"] ); // Extract seasons. $seasonsStageIds = []; foreach ($matches as $match) { $stages = explode(',', $match->stage_tree); $seasonsStageIds[] = $stages[1]; } $stageIds = implode(',', array_unique($seasonsStageIds)); if (!$stageIds) { return []; } $seasons = $this->mysql->select( "SELECT s.id, s.name, s.type, s.meta, s.parent, s.tree FROM stages AS s WHERE s.id IN ($stageIds) AND s.type = 'season'" ); // Unserialize seasons meta. foreach ($seasons as $season) { $season->meta = json_decode($season->meta); } // Sort by year, descending. @usort($seasons, function ($a, $b) { if ($a->meta->year > $b->meta->year) { return -1; } if ($a->meta->year < $b->meta->year) { return 1; } return 0; }); return $seasons; } /** * Finds season that specified stage belongs to. * * @param int $stageId * * @return object|null */ public function findSeasonOf($stageId) { $stages = $this->mysql->select( 'SELECT season.id, season.name, season.type, season.meta, season.parent, season.tree FROM stages as stage JOIN stages as season ON (stage.tree LIKE CONCAT(season.tree, ",%") OR stage.tree = season.tree) AND season.type = "season" WHERE stage.id = ?', [$stageId] ); if (!$stages) { return null; } $season = $stages[0]; $season->meta = json_decode($season->meta); return $season; } /** * Find stages of specified tree. * * Only returns stages that have matches in them. * * @param string $tree * * @return object[]|null */ public function findStagesByTree($tree) { $matches = $this->mysql->select( 'SELECT DISTINCT stage_tree FROM matches WHERE stage_tree LIKE ?', ["$tree,%"] ); // Extract stages. $stageIds = []; foreach ($matches as $match) { $stageIds = array_merge($stageIds, explode(',', $match->stage_tree)); } $stageIds = implode(',', array_unique($stageIds)); if (!$stageIds) { return []; } $stages = $this->mysql->select( "SELECT s.id, s.name, s.type, s.meta, s.parent, s.tree FROM stages AS s WHERE s.tree LIKE ? AND s.id IN ($stageIds)", ["$tree,%"] ); if (!$stages) { return null; } foreach ($stages as $stage) { $stage->meta = json_decode($stage->meta); } return $stages; } /** * Finds stage by id. * * @param int $id * * @return object|null */ public function findById($id) { $stage = $this->mysql ->table('stages') ->where('id', '=', $id) ->first(); if ($stage) { $stage->meta = json_decode($stage->meta); } return $stage; } } |