Skip to content

Commit

Permalink
Merge pull request #437 from AntonShevchuk/master
Browse files Browse the repository at this point in the history
Added fields filter for `Crud` and `Mapper\Link`
  • Loading branch information
Anton authored Aug 22, 2017
2 parents f5da212 + 38fd8e8 commit ccf0ebe
Show file tree
Hide file tree
Showing 15 changed files with 171 additions and 72 deletions.
17 changes: 13 additions & 4 deletions src/Controller/Mapper/AbstractMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@

use Bluz\Application\Application;
use Bluz\Application\Exception\ForbiddenException;
use Bluz\Application\Exception\NotAcceptableException;
use Bluz\Application\Exception\NotAllowedException;
use Bluz\Application\Exception\NotImplementedException;
use Bluz\Common\Exception\CommonException;
use Bluz\Common\Exception\ComponentException;
use Bluz\Controller\Controller;
use Bluz\Controller\ControllerException;
use Bluz\Crud\AbstractCrud;
Expand Down Expand Up @@ -80,6 +84,7 @@ abstract class AbstractMapper
* 'module' => 'module',
* 'controller' => 'controller',
* 'acl' => 'privilege',
* 'fields' => ['id', ... ]
* },
* ]
*
Expand Down Expand Up @@ -110,7 +115,7 @@ public function __construct(AbstractCrud $crud)
* @param string $controller
* @return Link
*/
public function addMap($method, $module, $controller)
public function addMap($method, $module, $controller) : Link
{
return $this->map[strtoupper($method)] = new Link($module, $controller);
}
Expand Down Expand Up @@ -250,7 +255,12 @@ protected function prepareRequest()
* Dispatch REST or CRUD controller
*
* @return mixed
* @throws ComponentException
* @throws CommonException
* @throws ControllerException
* @throws ForbiddenException
* @throws NotAllowedException
* @throws NotAcceptableException
* @throws NotImplementedException
*/
protected function dispatch()
Expand All @@ -267,14 +277,13 @@ protected function dispatch()
throw new ForbiddenException;
}

// setup params
$link->setParams($this->prepareParams());
$this->crud->setFields($link->getFields());

// dispatch controller
$result = Application::getInstance()->dispatch(
$link->getModule(),
$link->getController(),
$link->getParams()
$this->prepareParams()
);

return $result;
Expand Down
39 changes: 9 additions & 30 deletions src/Controller/Mapper/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ class Link
*/
protected $controller;

/**
* @var array
*/
protected $params;

/**
* @var string
*/
Expand All @@ -41,7 +36,7 @@ class Link
/**
* @var array
*/
protected $filters = [];
protected $fields = [];

/**
* Constructor of Link
Expand Down Expand Up @@ -73,13 +68,13 @@ public function acl(string $acl) : Link
/**
* Set filters for data
*
* @param array $filters
* @param array $fields
*
* @return Link
*/
public function filter(array $filters) : Link
public function fields(array $fields) : Link
{
$this->setFilters($filters);
$this->setFields($fields);
return $this;
}

Expand Down Expand Up @@ -115,22 +110,6 @@ protected function setController(string $controller)
$this->controller = $controller;
}

/**
* @return array
*/
public function getParams(): array
{
return $this->params;
}

/**
* @param array $params
*/
public function setParams(array $params)
{
$this->params = $params;
}

/**
* @return string|null
*/
Expand All @@ -150,18 +129,18 @@ protected function setAcl(string $acl)
/**
* @return array
*/
public function getFilters(): array
public function getFields(): array
{
return $this->filters;
return $this->fields;
}

/**
* Setup data filters
*
* @param array $filters
* @param array $fields
*/
protected function setFilters(array $filters)
protected function setFields(array $fields)
{
$this->filters = $filters;
$this->fields = $fields;
}
}
76 changes: 68 additions & 8 deletions src/Crud/AbstractCrud.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Bluz\Crud;

use Bluz\Application\Exception\NotImplementedException;
use Bluz\Db\Row;

/**
* Crud
Expand All @@ -26,6 +27,11 @@ abstract class AbstractCrud
*/
const DEFAULT_LIMIT = 10;

/**
* @var array Fields for action
*/
protected $fields = [];

/**
* Get CRUD Instance
*
Expand Down Expand Up @@ -58,7 +64,7 @@ abstract public function getPrimaryKey();
*/
public function readOne($primary)
{
throw new NotImplementedException();
throw new NotImplementedException;
}

/**
Expand All @@ -74,7 +80,7 @@ public function readOne($primary)
*/
public function readSet($offset = 0, $limit = self::DEFAULT_LIMIT, $params = [], &$total = null)
{
throw new NotImplementedException();
throw new NotImplementedException;
}

/**
Expand All @@ -87,7 +93,7 @@ public function readSet($offset = 0, $limit = self::DEFAULT_LIMIT, $params = [],
*/
public function createOne($data)
{
throw new NotImplementedException();
throw new NotImplementedException;
}

/**
Expand All @@ -100,7 +106,7 @@ public function createOne($data)
*/
public function createSet($data)
{
throw new NotImplementedException();
throw new NotImplementedException;
}

/**
Expand All @@ -114,7 +120,7 @@ public function createSet($data)
*/
public function updateOne($primary, $data)
{
throw new NotImplementedException();
throw new NotImplementedException;
}

/**
Expand All @@ -127,7 +133,7 @@ public function updateOne($primary, $data)
*/
public function updateSet($data)
{
throw new NotImplementedException();
throw new NotImplementedException;
}

/**
Expand All @@ -140,7 +146,7 @@ public function updateSet($data)
*/
public function deleteOne($primary)
{
throw new NotImplementedException();
throw new NotImplementedException;
}

/**
Expand All @@ -153,6 +159,60 @@ public function deleteOne($primary)
*/
public function deleteSet($data)
{
throw new NotImplementedException();
throw new NotImplementedException;
}

/**
* @return array
*/
public function getFields(): array
{
return $this->fields;
}

/**
* Setup data filters
*
* @param array $fields
*/
public function setFields(array $fields)
{
$this->fields = $fields;
}

/**
* Filter input Fields
*
* @param array $data Request
*
* @return array
*/
protected function filterData($data) : array
{
if (empty($this->getFields())) {
return $data;
}
return array_intersect_key($data, array_flip($this->getFields()));
}

/**
* Filter output Row
*
* @param Row $row from database
*
* @return Row
*/
protected function filterRow($row)
{
if (empty($this->getFields())) {
return $row;
}
$fields = array_keys($row->toArray());
$toDelete = array_diff($fields, $this->getFields());

foreach ($toDelete as $field) {
unset($row->$field);
}
return $row;
}
}
20 changes: 20 additions & 0 deletions src/Crud/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ public function readOne($primary)

$row = $this->getTable()::findRow($primary);

$row = $this->filterRow($row);

if (!$row) {
throw new NotFoundException('Record not found');
}
Expand All @@ -125,6 +127,19 @@ public function readSet($offset = 0, $limit = 10, $params = [], &$total = null)
{
$select = $this->getTable()::select();

// select only required fields
if (count($this->getFields())) {
$fields = $this->getFields();
$name = $this->getTable()->getName();
$fields = array_map(
function ($field) use ($name) {
return $name .'.'. $field;
},
$fields
);
$select->select(implode(', ', $fields));
}

// switch statement for DB type
$type = Proxy\Db::getOption('connect', 'type');
switch ($type) {
Expand Down Expand Up @@ -170,6 +185,9 @@ function () use (&$result, &$total, $select, $totalSQL) {
public function createOne($data)
{
$row = $this->getTable()::create();

$data = $this->filterData($data);

$row->setFromArray($data);
return $row->save();
}
Expand All @@ -191,6 +209,8 @@ public function updateOne($primary, $data)
throw new NotFoundException('Record not found');
}

$data = $this->filterData($data);

$row->setFromArray($data);
return $row->save();
}
Expand Down
17 changes: 8 additions & 9 deletions src/Db/Query/AbstractBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function execute()
*
* @return string The SQL query string
*/
abstract public function getSql();
abstract public function getSql() : string;

/**
* Return the complete SQL string formed for use
Expand All @@ -97,12 +97,11 @@ abstract public function getSql();
*
* @return string
*/
public function getQuery()
public function getQuery() : string
{
$sql = $this->getSql();

$sql = str_replace('%', '%%', $sql);
$sql = str_replace('?', '"%s"', $sql);
$sql = str_replace(['%', '?'], ['%%', '"%s"'], $sql);

// replace mask by data
return vsprintf($sql, $this->getParameters());
Expand All @@ -121,15 +120,15 @@ public function getQuery()
* ->setParameter(':user_id', 1);
* </code>
*
* @param string|int $key The parameter position or name
* @param string|int|null $key The parameter position or name
* @param mixed $value The parameter value
* @param integer $type PDO::PARAM_*
*
* @return $this
*/
public function setParameter($key, $value, $type = \PDO::PARAM_STR)
{
if (null == $key) {
if (null === $key) {
$key = count($this->params);
}

Expand Down Expand Up @@ -185,7 +184,7 @@ public function getParameter($key)
*
* @return array The currently defined query parameters
*/
public function getParameters()
public function getParameters() : array
{
return $this->params;
}
Expand All @@ -212,8 +211,8 @@ protected function addQueryPart($sqlPartName, $sqlPart, $append = false)
}

if ($append) {
if ($sqlPartName == "orderBy" || $sqlPartName == "groupBy"
|| $sqlPartName == "select" || $sqlPartName == "set"
if ($sqlPartName === 'orderBy' || $sqlPartName === 'groupBy'
|| $sqlPartName === 'select' || $sqlPartName === 'set'
) {
foreach ((array)$sqlPart as $part) {
$this->sqlParts[$sqlPartName][] = $part;
Expand Down
Loading

0 comments on commit ccf0ebe

Please sign in to comment.