From e15869c8086fdf175023789185926a7b5b1c1b90 Mon Sep 17 00:00:00 2001 From: Titouan Mathis Date: Wed, 14 Jul 2021 23:17:00 +0200 Subject: [PATCH] Allow usage of ArrayObject instances as Repository results --- .../app/Repositories/AbstractRepository.php | 68 +++++++++++-------- .../app/Repositories/PostRepository.php | 4 +- .../app/Repositories/TermRepository.php | 2 +- 3 files changed, 42 insertions(+), 32 deletions(-) diff --git a/template/web/wp-content/themes/<%= slug %>/app/Repositories/AbstractRepository.php b/template/web/wp-content/themes/<%= slug %>/app/Repositories/AbstractRepository.php index 161dd4a..a244a0a 100644 --- a/template/web/wp-content/themes/<%= slug %>/app/Repositories/AbstractRepository.php +++ b/template/web/wp-content/themes/<%= slug %>/app/Repositories/AbstractRepository.php @@ -1,40 +1,63 @@ */ - private $result_set = array(); + private $results = array(); /** - * Returns an array of posts. + * Returns the results. * - * @return array + * @return array|ArrayObject */ public function get() { - return $this->result_set; + return $this->results; + } + + /** + * Set the results. + * + * @param array|ArrayObject $results The results to set. + * @return $this + */ + private function set( $results ) { + $this->results = $results; + return $this; } /** * Returns the first item in a collection. Returns null if there are 0 items in * the collection. * - * @return mixed + * @return mixed|null */ public function first() { - $local_array = $this->get(); - return isset( $local_array[0] ) ? $local_array[0] : null; + $array = (array) $this->get(); + return $array[ array_key_first( $array ) ] ?? null; + } + + /** + * Get the last item in a collection, null if the collection is empty. + * + * @return mixed|null + */ + public function last() { + $array = (array) $this->get(); + return $array[ array_key_last( $array ) ] ?? null; } /** @@ -42,7 +65,7 @@ public function first() { * * @param array $params Query params. * - * @return AbstractRepository + * @return $this */ protected function query( array $params ) { // Clear old result sets. @@ -53,7 +76,7 @@ protected function query( array $params ) { if ( false !== $cached_results && count( $cached_results ) > 0 ) { // Use cached results. - return $this->result_set( $cached_results ); + return $this->set( $cached_results ); } $results = $this->do_query( $params ); @@ -63,7 +86,7 @@ protected function query( array $params ) { wp_cache_set( $cache_key, $results, __CLASS__ ); } - return $this->result_set( $results ); + return $this->set( $results ); } /** @@ -81,23 +104,10 @@ abstract protected function do_query( $params ); /** * Clears the current result set. * - * @return AbstractRepository + * @return $this */ protected function reset() { - $this->result_set = array(); - return $this; - } - - /** - * Returns current result set - * - * @param array $result_set Result set. - * - * @return AbstractRepository - */ - protected function result_set( $result_set = array() ) { - $this->result_set = $result_set; + $this->set( array() ); return $this; } - } diff --git a/template/web/wp-content/themes/<%= slug %>/app/Repositories/PostRepository.php b/template/web/wp-content/themes/<%= slug %>/app/Repositories/PostRepository.php index 181950e..4ad782e 100644 --- a/template/web/wp-content/themes/<%= slug %>/app/Repositories/PostRepository.php +++ b/template/web/wp-content/themes/<%= slug %>/app/Repositories/PostRepository.php @@ -33,7 +33,7 @@ public function do_query( $params ) { * @param array $exclude Posts to exclude (optional). * @param integer $paged Enable pagination (optional). * - * @return AbstractRepository + * @return $this */ public function posts_by_category_slug( $slug, $limit = 10, $exclude = array(), $paged = 0 ) { @@ -77,7 +77,7 @@ public function posts_by_category_slug( $slug, $limit = 10, $exclude = array(), * @param array $exclude IDs of posts to exclude. * @param integer $paged Enable pagination. * - * @return AbstractRepository + * @return $this */ public function latest_posts( $limit = 10, array $exclude = array(), $paged = 0 ) { diff --git a/template/web/wp-content/themes/<%= slug %>/app/Repositories/TermRepository.php b/template/web/wp-content/themes/<%= slug %>/app/Repositories/TermRepository.php index 42604f4..650e7c3 100644 --- a/template/web/wp-content/themes/<%= slug %>/app/Repositories/TermRepository.php +++ b/template/web/wp-content/themes/<%= slug %>/app/Repositories/TermRepository.php @@ -30,7 +30,7 @@ public function do_query( $params ) { * @param array $exclude IDs of posts to exclude. * @param int $limit Number of maximum results. * - * @return AbstractRepository + * @return $this */ public function top_level_terms( $taxonomy, $exclude = array(), $limit = 100 ) { // Set sane defaults so we don't do full table scans.