Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions config/apihandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@

'prefix' => '_',

/*
|--------------------------------------------------------------------------
| Max Limit for query
|--------------------------------------------------------------------------
|
| Defines the max limit of resutls that can be obtained, if null is defined
| results are limited as that of "limit" query parameter
|
*/

'max_limit' => null,

/*
|--------------------------------------------------------------------------
| Envelope
Expand Down Expand Up @@ -62,7 +74,7 @@

'errors' => [
'ResourceNotFound' => ['http_code' => 404, 'message' => 'The requested resource could not be found but may be available again in the future.'],
'InternalError' => ['http_code' => 500, 'message' => 'Internal server error'],
'InternalError' => ['http_code' => 500, 'message' => 'Internal server error'],
'Unauthorized' => ['http_code' => 401, 'message' => 'Authentication is required and has failed or has not yet been provided'],
'Forbidden' => ['http_code' => 403, 'message' => 'You don\'t have enough permissions to access this resource'],
'ToManyRequests' => ['http_code' => 429, 'message' => 'You have sent too many requests in a specific timespan'],
Expand All @@ -88,4 +100,4 @@
'UnsupportedQueryParameter' => 'UnsupportedQueryParameter',
'InvalidQueryParameter' => 'InvalidQueryParameter',
],
];
];
22 changes: 20 additions & 2 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ class Parser
*/
public $envelope;

/**
* Maximum limit for response
*
* @var int
*/
public $maxLimit;

/**
* The base query builder instance.
*
Expand Down Expand Up @@ -132,6 +139,7 @@ public function __construct($builder, $params)

$this->prefix = Config::get('apihandler.prefix');
$this->envelope = Config::get('apihandler.envelope');
$this->maxLimit = Config::get('apihandler.max_limit');

$isEloquentModel = is_subclass_of($builder, '\Illuminate\Database\Eloquent\Model');
$isEloquentRelation = is_subclass_of($builder, '\Illuminate\Database\Eloquent\Relations\Relation');
Expand Down Expand Up @@ -186,7 +194,17 @@ public function parse($options, $multiple = false)
//Parse and apply limit using the laravel "limit" function
if ($limit = $this->getParam('limit')) {
$limit = intval($limit);
$this->query->limit($limit);

if($limit <= $this->maxLimit) {
$this->query->limit($limit);
}
elseif($this->maxLimit === null) {
$this->query->limit($limit);
}
else {
$limit = intval($this->maxLimit);
$this->query->limit($limit);
}
}

//Parse and apply the filters using the different laravel "where" functions
Expand Down Expand Up @@ -709,4 +727,4 @@ protected function isRelation($model, $relationName)
return false;
}
}
}
}