Skip to content

Commit

Permalink
Getting start for query builder
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafabarmshory committed Jul 7, 2020
1 parent ea6c616 commit 6ce3e7b
Show file tree
Hide file tree
Showing 33 changed files with 1,104 additions and 2,011 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,43 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
Pluf::loadClass('\Pluf\Data\Model');
namespace Pluf\NoteBook;

class Pluf_Paginator_MyGeoModel extends \Pluf\Data\Model
use Pluf\Data\Model;
use Pluf\Data\Schema;

/**
* Saves library location
*
* @author maso
*
*/
class Library extends Model
{

function init()
{
$this->_a['table'] = 'testforgeomodels';
$this->_a['table'] = 'notebook_library';
$this->_a['cols'] = array(
'id' => array(
'type' => 'Sequence',
'type' => Schema::SEQUENCE,
'blank' => true
), // It is automatically added.
'title' => array(
'type' => 'Varchar',
'type' => Schema::VARCHAR,
'blank' => false,
'size' => 100,
'verbose' => 'Title of the item'
),
'location' => array(
'type' => 'Geometry',
'type' => Schema::GEOMETRY,
'blank' => true,
'is_null' => false,
'editable' => false,
'readable' => true
)
);
}


}


3 changes: 2 additions & 1 deletion examples/NoteBook/module.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"model": [
"\\Pluf\\NoteBook\\Book",
"\\Pluf\\NoteBook\\Item",
"\\Pluf\\NoteBook\\Tag"
"\\Pluf\\NoteBook\\Tag",
"\\Pluf\\NoteBook\\Library"
],
"init": {
"\\Pluf\\NoteBook\\Book": [
Expand Down
36 changes: 24 additions & 12 deletions src/Data/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,20 @@ class Query

private bool $count = false;

private ?string $select = null;

/**
* Creates new instance of the Query
*
* To build a query:
*
* - view
* - filter
* - order
* - start
* - limit
* - select
* - count
* - view: a view to apply
* - filter: list of where clouse
* - order: list of order by
* - start: start index of results
* - limit: Limit of resoult count
* - select: a query to search
* - count: returns counts of items
*
* @param array $param
*/
Expand All @@ -117,6 +119,16 @@ function __construct(?array $param = [])
$this->setDefaults($param);
}

/**
* Gets select query
*
* @return string
*/
public function getSelect(): string
{
return $this->select;
}

/**
* Get current view of the query
*
Expand All @@ -131,7 +143,7 @@ public function getView()
*
* @return array of filters
*/
public function getFilter()
public function getFilter(): array
{
return $this->filter;
}
Expand All @@ -140,7 +152,7 @@ public function getFilter()
*
* @return array order
*/
public function getOrder()
public function getOrder(): array
{
return $this->order;
}
Expand All @@ -149,7 +161,7 @@ public function getOrder()
*
* @return number
*/
public function getStart()
public function getStart(): int
{
return $this->start;
}
Expand All @@ -158,7 +170,7 @@ public function getStart()
*
* @return number
*/
public function getLimit()
public function getLimit(): int
{
if ($this->limit < 1) {
return Query::MAX_RESULT_SIZE;
Expand All @@ -170,7 +182,7 @@ public function getLimit()
*
* @return number of item
*/
public function getCount()
public function getCount(): bool
{
return $this->count;
}
Expand Down
88 changes: 86 additions & 2 deletions src/Data/QueryBuilder.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php
namespace Pluf\Data;

use Pluf\HTTP\Error500;
use Pluf\HTTP\Request;

/**
* Crates a new query
*
Expand All @@ -10,6 +13,26 @@
class QueryBuilder
{

private $view;

private ?bool $count = false;

private ?string $select = null;

private int $start = 0;

private int $limit = - 1;

private array $filters = [];

private array $orders = [];

/**
* Creates new instance of the builder
*
* @param Request|array $config
* @return QueryBuilder
*/
public static function getInstance($config): QueryBuilder
{
if ($config instanceof \Pluf\HTTP\Request) {
Expand All @@ -18,34 +41,95 @@ public static function getInstance($config): QueryBuilder
return new QueryBuilder();
}

/**
* Sets query view
*
* @param array $view
* @return QueryBuilder
*/
public function setView($view): QueryBuilder
{
$this->view = $view;
return $this;
}

public function setStart($start): QueryBuilder
/**
* Start index of the result
*
* @param int $start
* @return QueryBuilder
*/
public function setStart(int $start): QueryBuilder
{
if ($start < 0) {
throw new Error500('Start must be positive.');
}
$this->start = $start;
return $this;
}

public function setLimit($limit): QueryBuilder
{
if ($limit < 0) {
throw new Error500('Query limit must be positive.');
}
$this->limit = $limit;
return $this;
}

public function setOrder($key, $order): QueryBuilder
{
foreach ($this->orders as $order) {
if ($order[0] === $key) {
throw new Error500('Dublicated order key');
}
}
$this->orders[] = [
$key,
$order
];
return $this;
}

public function addFilter($key, $value): QueryBuilder
{
$this->filters[] = [
$key,
'=',
$value
];
return $this;
}

/**
* Sets select query
*
* @param string $select
* A query to perform on object.
* @return QueryBuilder current builder
*/
public function setSelect(string $select): QueryBuilder
{
$this->select = $select;
return $this;
}

/**
* Builds a query based on configurations.
*
* @return Query
*/
public function build(): Query
{
return null;
return new Query([
'view' => $this->view,
'filter' => $this->filters,
'order' => $this->orders,
'select' => $this->select,
'start' => $this->start,
'limit' => $this->limit,
'count' => $this->count
]);
}
}

19 changes: 8 additions & 11 deletions src/Data/QueryBuilder/RequestQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,10 @@ class RequestQueryBuilder extends \Pluf\Data\QueryBuilder
*/
function __construct(Request $request)
{
// load query
if (isset($request->REQUEST[self::SEARCH_QUERY_KEY])) {
$this->setQueryString($request->REQUEST[self::SEARCH_QUERY_KEY]);
}

$this->loadPage($request)
->loadSorts($request)
->loadFilters($request)
->loadQuery($request);

// Load options
$this->loadSortOptions($request);
$this->loadFilterOptions($request);
}

/*
Expand Down Expand Up @@ -89,7 +80,7 @@ private function loadSorts(Request $request): RequestQueryBuilder
{
if (! isset($request->REQUEST[self::SORT_KEY_KEY])) {
$this->sort_order = [];
return;
return $this;
}
// Sort orders
$keys = $request->REQUEST[self::SORT_KEY_KEY];
Expand Down Expand Up @@ -126,7 +117,7 @@ private function loadFilters(Request $request): RequestQueryBuilder

// check filter option
if (! array_key_exists(self::FILTER_KEY_KEY, $request->REQUEST)) {
return;
return $this;
}

$keys = $request->REQUEST[self::FILTER_KEY_KEY];
Expand Down Expand Up @@ -157,6 +148,12 @@ private function loadFilters(Request $request): RequestQueryBuilder
*/
private function loadQuery(Request $request): RequestQueryBuilder
{
// load query
$query = null;
if (isset($request->REQUEST[self::SEARCH_QUERY_KEY])) {
$query = $request->REQUEST[self::SEARCH_QUERY_KEY];
}
$this->setSelect((string) $query);
return $this;
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/ObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

use Pluf\Data\ModelDescription;
use Pluf\HTTP\Error500;
use Pluf\HTTP\RequestMapper;
use Pluf\ObjectMapper\ArrayMapper;
use Pluf\ObjectMapper\RequestMapper;

abstract class ObjectMapper
{
Expand Down Expand Up @@ -39,6 +40,9 @@ public static function getInstance($input): ObjectMapper
if ($input instanceof HTTP\Request) {
return new RequestMapper($input);
}
if (is_array($input)) {
return new ArrayMapper($input);
}
throw new Error500('No suitable object mapper fount for input: ' . get_class($input));
}

Expand Down
Loading

0 comments on commit 6ce3e7b

Please sign in to comment.