Source of file AbstractCrud.php
Size: 3,446 Bytes - Last Modified: 2017-11-08T13:54:24+00:00
/home/travis/build/bluzphp/framework/src/Crud/AbstractCrud.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 | <?php /** * Bluz Framework Component * * @copyright Bluz PHP Team * @link https://github.com/bluzphp/framework */ declare(strict_types=1); namespace Bluz\Crud; use Bluz\Application\Exception\NotImplementedException; use Bluz\Db\Row; /** * Crud * * @package Bluz\Crud * @author Anton Shevchuk * @link https://github.com/bluzphp/framework/wiki/Crud */ abstract class AbstractCrud implements CrudInterface { /** * @var array Fields for action * @todo should be different for Create, Read and Update */ protected $fields = []; /** * Return primary key signature * * @return array */ abstract public function getPrimaryKey(); /** * Get CRUD Instance * * @return static */ public static function getInstance() { static $instance; if (null === $instance) { $instance = new static(); } return $instance; } /** * @inheritdoc * * @throws NotImplementedException */ public function readOne($primary) { throw new NotImplementedException; } /** * @inheritdoc * * @throws NotImplementedException */ public function readSet($offset = 0, $limit = self::DEFAULT_LIMIT, $params = []) { throw new NotImplementedException; } /** * @inheritdoc * * @throws NotImplementedException */ public function createOne($data) { throw new NotImplementedException; } /** * @inheritdoc * * @throws NotImplementedException */ public function createSet($data) { throw new NotImplementedException; } /** * @inheritdoc * * @throws NotImplementedException */ public function updateOne($primary, $data) { throw new NotImplementedException; } /** * @inheritdoc * * @throws NotImplementedException */ public function updateSet($data) { throw new NotImplementedException; } /** * @inheritdoc * * @throws NotImplementedException */ public function deleteOne($primary) { throw new NotImplementedException; } /** * @inheritdoc * * @throws NotImplementedException */ public function deleteSet($data) { throw new NotImplementedException; } /** * @return array */ public function getFields(): array { return $this->fields; } /** * @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; } } |