summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--includes/concert.php37
-rw-r--r--tests/ConcertTest.php14
2 files changed, 29 insertions, 22 deletions
diff --git a/includes/concert.php b/includes/concert.php
index 56021f5..fcb3934 100644
--- a/includes/concert.php
+++ b/includes/concert.php
@@ -173,7 +173,18 @@ if ( !class_exists('GiglogAdmin_Concert') ) {
}
- public static function find_concerts_in(?string $city = null) : array
+ /**
+ * Return an array of concert objects optionally limited by a specified
+ * filter.
+ *
+ * Valid filters are:
+ * - 'venue_id' => int : only include concerts at the given venue
+ * - 'city' => string : only include concerts in the given city
+ *
+ * @param array<string, mixed> $filter
+ * @return array<GiglogAdmin_Concert>
+ */
+ public static function find_concerts(array $filter = []) : array
{
global $wpdb;
@@ -181,23 +192,19 @@ if ( !class_exists('GiglogAdmin_Concert') ) {
. 'FROM wpg_concerts '
. 'INNER JOIN wpg_venues ON wpg_concerts.venue = wpg_venues.id ';
- if ( $city ) {
- $query .= 'WHERE wpg_venues.wpgvenue_city = ' . $wpdb->prepare('%s', $city);
- }
-
- $results = $wpdb->get_results($query);
+ $where = [];
- return array_map(function($c) { return new GiglogAdmin_Concert($c); }, $results);
- }
+ if ( isset( $filter["city"] ) ) {
+ array_push($where, 'wpg_venues.wpgvenue_city = ' . $wpdb->prepare('%s', $filter["city"]));
+ }
- public static function find_concerts_at(GiglogAdmin_Venue $venue) : array
- {
- global $wpdb;
+ if ( isset( $filter["venue_id"] ) ) {
+ array_push($where, 'wpg_venues.id = ' . $wpdb->prepare('%s', $filter["venue_id"]));
+ }
- $query = 'SELECT wpg_concerts.*, wpg_venues.wpgvenue_name, wpg_venues.wpgvenue_city '
- . 'FROM wpg_concerts '
- . 'INNER JOIN wpg_venues ON wpg_concerts.venue = wpg_venues.id '
- . 'WHERE wpg_concerts.venue = ' . $wpdb->prepare('%d', $venue->id());
+ if ( ! empty( $where ) ) {
+ $query .= 'WHERE ' . implode(' and ', $where);
+ }
$results = $wpdb->get_results($query);
diff --git a/tests/ConcertTest.php b/tests/ConcertTest.php
index ec7bc13..2c747b8 100644
--- a/tests/ConcertTest.php
+++ b/tests/ConcertTest.php
@@ -106,7 +106,7 @@ final class ConcertTest extends WP_UnitTestCase
GiglogAdmin_Concert::create('Concert ' . $i, $venue3->id(), '', '', '');
}
- $gigs_in_svene = GiglogAdmin_Concert::find_concerts_in("Svene");
+ $gigs_in_svene = GiglogAdmin_Concert::find_concerts([ "city" => "Svene"]);
$this->assertEquals(4, count($gigs_in_svene));
while ($gig = array_pop($gigs_in_svene)) {
@@ -114,14 +114,14 @@ final class ConcertTest extends WP_UnitTestCase
}
- $gigs_in_oslo = GiglogAdmin_Concert::find_concerts_in("Oslo");
+ $gigs_in_oslo = GiglogAdmin_Concert::find_concerts(["city" => "Oslo"]);
$this->assertEquals(2, count($gigs_in_oslo));
while ($gig = array_pop($gigs_in_oslo)) {
$this->assertEquals("Oslo", $gig->venue()->city());
}
- $gigs_in_sogndal = GiglogAdmin_Concert::find_concerts_in("Sogndal");
+ $gigs_in_sogndal = GiglogAdmin_Concert::find_concerts(["city" => "Sogndal"]);
$this->assertEquals(5, count($gigs_in_sogndal));
while ($gig = array_pop($gigs_in_sogndal)) {
@@ -147,21 +147,21 @@ final class ConcertTest extends WP_UnitTestCase
GiglogAdmin_Concert::create('Concert ' . $i, $venue3->id(), '', '', '');
}
- $gigs_at_ss = GiglogAdmin_Concert::find_concerts_at($venue1);
+ $gigs_at_ss = GiglogAdmin_Concert::find_concerts(["venue_id" => $venue1->id()]);
$this->assertEquals(4, count($gigs_at_ss));
while ($gig = array_pop($gigs_at_ss)) {
$this->assertEquals("Sentrum Scene", $gig->venue()->name());
}
- $gigs_at_rmh = GiglogAdmin_Concert::find_concerts_at($venue2);
+ $gigs_at_rmh = GiglogAdmin_Concert::find_concerts(["venue_id" => $venue2->id()]);
$this->assertEquals(2, count($gigs_at_rmh));
while ($gig = array_pop($gigs_at_rmh)) {
$this->assertEquals("Rockefeller Music Hall", $gig->venue()->name());
}
- $gigs_at_r = GiglogAdmin_Concert::find_concerts_at($venue3);
+ $gigs_at_r = GiglogAdmin_Concert::find_concerts(["venue_id" => $venue3->id()]);
$this->assertEquals(5, count($gigs_at_r));
while ($gig = array_pop($gigs_at_r)) {
@@ -187,7 +187,7 @@ final class ConcertTest extends WP_UnitTestCase
GiglogAdmin_Concert::create('Concert ' . $i, $venue3->id(), '', '', '');
}
- $gigs = GiglogAdmin_Concert::find_concerts_in();
+ $gigs = GiglogAdmin_Concert::find_concerts();
$this->assertEquals(11, count($gigs));
}