Version 5.0.0 beta 1
This commit is contained in:
1 parent
25ddeb65d6
commit
15c7beabc5
6736 files changed
+627902
-497943
No files matched your search
@@ -11,79 +11,86 @@
|
||||
namespace IPS\Api;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use IPS\Db;
|
||||
use IPS\Db\Select;
|
||||
use IPS\Member;
|
||||
use IPS\Patterns\ActiveRecordIterator;
|
||||
use Iterator;
|
||||
use function defined;
|
||||
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* API Response
|
||||
*/
|
||||
class _PaginatedResponse extends Response
|
||||
class PaginatedResponse extends Response
|
||||
{
|
||||
/**
|
||||
* @brief HTTP Response Code
|
||||
*/
|
||||
public $httpCode;
|
||||
public int $httpCode = 200;
|
||||
|
||||
/**
|
||||
* @brief Iterator (usually a select query)
|
||||
*/
|
||||
protected $iterator;
|
||||
protected ?Iterator $iterator = null;
|
||||
|
||||
/**
|
||||
* @brief Current page
|
||||
*/
|
||||
protected $page = 1;
|
||||
protected int $page = 1;
|
||||
|
||||
/**
|
||||
* @brief Results per page
|
||||
*/
|
||||
protected $resultsPerPage = 25;
|
||||
protected int $resultsPerPage = 25;
|
||||
|
||||
/**
|
||||
* @brief ActiveRecord class
|
||||
*/
|
||||
protected $activeRecordClass;
|
||||
protected string $activeRecordClass = '';
|
||||
|
||||
/**
|
||||
* @brief Total Count
|
||||
*/
|
||||
protected $count;
|
||||
protected int $count = 0;
|
||||
|
||||
/**
|
||||
* @brief The member making the API request or NULL for API Key / client_credentials
|
||||
*/
|
||||
protected $authorizedMember;
|
||||
protected ?Member $authorizedMember = null;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param int $httpCode HTTP Response code
|
||||
* @param \Iterator $iterator Select query or \IPS\Patterns\ActiveRecordIterator instance
|
||||
* @param Iterator|array $iterator Select query or \IPS\Patterns\ActiveRecordIterator instance
|
||||
* @param int $page Current page
|
||||
* @param string $activeRecordClass ActiveRecord class
|
||||
* @param int|NULL $count Total Count
|
||||
* @param \IPS\Member|NULL $authorizedMember The member making the API request or NULL for API Key / client_credentials
|
||||
* @param int $perPage Number of results per page
|
||||
* @param Member|NULL $authorizedMember The member making the API request or NULL for API Key / client_credentials
|
||||
* @param int|null $perPage Number of results per page
|
||||
* @return void
|
||||
*/
|
||||
public function __construct( $httpCode, $iterator, $page, $activeRecordClass, $count, \IPS\Member $authorizedMember = NULL, $perPage=NULL )
|
||||
public function __construct( int $httpCode, Iterator $iterator, int $page, string $activeRecordClass, ?int $count, ?Member $authorizedMember = NULL, ?int $perPage=NULL )
|
||||
{
|
||||
$this->httpCode = $httpCode;
|
||||
$page = (int) $page;
|
||||
$this->page = $page > 0 ? $page : 1;
|
||||
$this->iterator = $iterator;
|
||||
$this->activeRecordClass = $activeRecordClass;
|
||||
$this->count = (int) $count;
|
||||
$this->resultsPerPage = $perPage ? (int) $perPage : 25;
|
||||
$this->resultsPerPage = $perPage ?: 25;
|
||||
$this->authorizedMember = $authorizedMember;
|
||||
|
||||
if( $this->iterator instanceof \IPS\Db\Select )
|
||||
if( $this->iterator instanceof Select )
|
||||
{
|
||||
/* Limit the query before calling count(), as this runs the query so it cannot be modified after */
|
||||
$this->iterator->query .= \IPS\Db::i()->compileLimitClause( array( ( $this->page - 1 ) * $this->resultsPerPage, $this->resultsPerPage ) );
|
||||
$this->iterator->query .= Db::i()->compileLimitClause( array( ( $this->page - 1 ) * $this->resultsPerPage, $this->resultsPerPage ) );
|
||||
|
||||
if ( $count === NULL )
|
||||
{
|
||||
@@ -97,13 +104,13 @@ class _PaginatedResponse extends Response
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getOutput()
|
||||
public function getOutput() : array
|
||||
{
|
||||
$results = array();
|
||||
|
||||
if ( $this->activeRecordClass )
|
||||
{
|
||||
if( $this->iterator instanceof \IPS\Patterns\ActiveRecordIterator )
|
||||
if( $this->iterator instanceof ActiveRecordIterator )
|
||||
{
|
||||
foreach ( $this->iterator as $result )
|
||||
{
|
||||
@@ -112,7 +119,7 @@ class _PaginatedResponse extends Response
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach ( new \IPS\Patterns\ActiveRecordIterator( $this->iterator, $this->activeRecordClass ) as $result )
|
||||
foreach ( new ActiveRecordIterator( $this->iterator, $this->activeRecordClass ) as $result )
|
||||
{
|
||||
$results[] = $result->apiOutput( $this->authorizedMember );
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user