Skip to content
This repository has been archived by the owner on Sep 21, 2022. It is now read-only.

Commit

Permalink
Allow usage of ArrayObject instances as Repository results
Browse files Browse the repository at this point in the history
  • Loading branch information
titouanmathis committed Jul 14, 2021
1 parent a293f82 commit e15869c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,48 +1,71 @@
<?php
/**
* Parent repository class. Provides a very basic, fluent interface to interact
* with query
* Abstract repository class.
*
* @package Studiometa
* @subpackage Repositories
*/

namespace Studiometa\Repositories;

use ArrayObject;

/** Class */
abstract class AbstractRepository {
/**
* List of posts.
* List of results.
*
* @var array
* @var array|ArrayObject<int, mixed>
*/
private $result_set = array();
private $results = array();

/**
* Returns an array of posts.
* Returns the results.
*
* @return array
* @return array|ArrayObject<int, mixed>
*/
public function get() {
return $this->result_set;
return $this->results;
}

/**
* Set the results.
*
* @param array|ArrayObject<int, mixed> $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;
}

/**
* Runs a query.
*
* @param array $params Query params.
*
* @return AbstractRepository
* @return $this
*/
protected function query( array $params ) {
// Clear old result sets.
Expand All @@ -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 );
Expand All @@ -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 );
}

/**
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {

Expand Down Expand Up @@ -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 ) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit e15869c

Please sign in to comment.