16 Commits

Author SHA1 Message Date
43f545516d Merge branch 'develop' 2020-08-04 01:33:09 -04:00
b757ed19b2 Merge branch 'develop' 2020-08-03 23:54:16 -04:00
9dc71e4d77 Merge branch 'develop' 2020-08-03 23:51:05 -04:00
c6806a1c62 Merge branch 'develop' 2020-08-03 23:42:11 -04:00
6fd19a11be Merge branch 'develop' 2020-08-03 23:38:41 -04:00
7c727d93e9 Merge branch 'develop' 2020-08-03 23:27:45 -04:00
e02b8c4063 Merge branch 'develop' 2020-08-03 23:17:38 -04:00
89d1db7a7e Merge branch 'develop' 2020-08-03 23:10:57 -04:00
8dc0a27fd9 Merge branch 'develop' 2020-08-03 22:07:24 -04:00
ae172b902c Merge branch 'develop' 2020-08-03 16:25:19 -04:00
7f81b987c9 Merge branch 'develop' 2020-07-24 12:06:16 -04:00
af801e769f Merge branch 'develop' 2020-07-24 11:58:16 -04:00
c40baaad3f Merge branch 'develop' 2020-07-24 11:47:39 -04:00
a82fdce64b Merge branch 'develop' 2020-07-22 23:08:52 -04:00
8126b1f67d Merge branch 'develop' 2020-07-22 22:57:48 -04:00
2177cb4652 Merge branch 'develop' 2020-07-22 14:08:09 -04:00
15 changed files with 545 additions and 502 deletions

7
.gitignore vendored
View File

@ -1,6 +1,3 @@
# Composer
**/vendor/
**/*.lock
# PHPStorm
**/.idea/
/vendor/
composer.lock

14
common/Alias/Model.php Normal file
View File

@ -0,0 +1,14 @@
<?php
namespace ProVM\Common\Alias;
use ProVM\Common\Factory\Model as ModelFactory;
interface Model {
public function getTable(): string;
public function setFactory(ModelFactory $factory): Model;
public function parentOf(string $child_model_class, array $relation_definitions): ?array;
public function childOf(string $parent_model_class, array $relation_definitions): ?Model;
public function siblingOf(string $sibling_model_class, string $connecting_table, array $relation_definitions): ?array;
public function toArray(): array;
}

125
common/Define/Model.php Normal file
View File

@ -0,0 +1,125 @@
<?php
namespace ProVM\Common\Define;
use \Model as BaseModel;
use ProVM\Common\Alias\Model as ModelInterface;
use ProVM\Common\Factory\Model as ModelFactory;
abstract class Model extends BaseModel implements ModelInterface {
const SELF_KEY = 'self_key';
const PARENT_KEY = 'parent_key';
const CHILD_KEY = 'child_key';
const SIBLING_KEY = 'sibling_key';
const SELF_CONNECT_KEY = 'self_connect_key';
const SIBLING_CONNECT_KEY = 'sibling_connect_key';
public function getTable(): string {
return static::$_table;
}
protected $factory;
public function setFactory(ModelFactory $factory): ModelInterface {
$this->factory = $factory;
return $this;
}
protected function checkDefinitions(array $definitions, array $required, array $default) {
foreach ($default as $key => $value) {
if (!isset($definitions[$key])) {
$definitions[$key] = $value;
}
}
foreach ($required as $definition) {
if (!isset($definitions[$definition])) {
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
throw new \InvalidArgumentException($definition . ' is required for ' . $trace[1]['function'] . ' in ' . get_called_class());
}
}
return $definitions;
}
public function parentOf(string $child_model_class, array $relation_definitions): ?array {
$relation_definitions = $this->checkDefinitions($relation_definitions, [
Model::SELF_KEY,
Model::CHILD_KEY
], [
Model::SELF_KEY => 'id'
]);
return $this->factory
->find($child_model_class)
->where([
[
$relation_definitions[Model::CHILD_KEY],
$this->{$relation_definitions[Model::SELF_KEY]}
]
])
->many();
}
public function childOf(string $parent_model_class, array $relation_definitions): ?ModelInterface {
$relation_definitions = $this->checkDefinitions($relation_definitions, [
Model::SELF_KEY,
Model::PARENT_KEY
], [
Model::PARENT_KEY => 'id'
]);
$parent_table = (new $parent_model_class())->getTable();
return $this->factory
->find($parent_model_class)
->where([
[
$relation_definitions[Model::PARENT_KEY],
$this->{$relation_definitions[Model::SELF_KEY]}
]
])
->one();
}
public function siblingOf(string $sibling_model_class, string $connecting_table, array $relation_definitions): ?array {
$relation_definitions = $this->checkDefinitions($relation_definitions, [
Model::SELF_KEY,
Model::SIBLING_KEY,
Model::SELF_CONNECT_KEY,
Model::SIBLING_CONNECT_KEY
], [
Model::SELF_KEY => 'id',
Model::SIBLING_KEY => 'id'
]);
$sibling_table = (new $sibling_model_class())->getTable();
return $this->find($sibling_model_class)
->select([
[
$sibling_table,
'*'
],
[
$connecting_table,
'*'
]
])
->join([
[
$connecting_table,
implode('.', [
$connecting_table,
$relation_definitions[Model::SIBLING_CONNECT_KEY]
]),
implode('.', [
$sibling_table,
$relation_definitions[Model::SIBLING_KEY]
])
]
])
->where([
[
implode('.', [
$connecting_table,
$relation_definitions[Model::SELF_CONNECT_KEY]
]),
$this->{$relation_definitions[Model::SELF_KEY]}
]
])
->many();
}
public function toArray(): array {
return $this->asArray();
}
}

390
common/Factory/Model.php Normal file
View File

@ -0,0 +1,390 @@
<?php
namespace ProVM\Common\Factory;
use ORM;
use Model as BaseModel;
use ProVM\Common\Alias\Model as ModelInterface;
class Model {
public function reset(): Model {
$reset = [
'class',
'columns',
'joins',
'conditions',
'grouping',
'ordering',
'limit',
'offset'
];
foreach ($reset as $p) {
$this->{$p} = null;
}
return $this;
}
public function create(string $model_class, array $data = null): ModelInterface {
if ($data !== null) {
$model = $this->find($model_class);
foreach ($data as $f => $v) {
$model = $model->where([[$f, $v]]);
}
$model = $model->one();
if ($model !== null) {
return $model;
}
}
$model = BaseModel::factory($model_class)->create($data);
$model->setFactory($this);
return $model;
}
protected $class;
public function find(string $model_class): Model {
if (!class_exists($model_class)) {
throw new \InvalidArgumentException('El modelo ' . $model_class . ' no existe.');
}
$this->reset();
$this->class = $model_class;
return $this;
}
protected $columns;
public function select($columns): Model {
if ($this->columns === null) {
$this->columns = [];
}
if (!is_array($columns)) {
$columns = [$columns];
}
foreach ($columns as $c) {
$col = (object) [
'table' => '',
'column' => $c
];
if (is_array($c)) {
$col = (object) [
'table' => $c['table'] ?? $c[0],
'column' => $c['column'] ?? $c[1]
];
}
$this->columns []= $col;
}
return $this;
}
protected $joins;
public function join(array $joins): Model {
if ($this->joins === null) {
$this->joins = [];
}
foreach ($joins as $j) {
$join = (object) [
'table' => $j['table'] ?? $j[0],
'from' => $j['from'] ?? $j[1],
'to' => $j['to'] ?? $j[2],
'sym' => $j['sym'] ?? ($j[3] ?? '='),
'alias' => $j['alias'] ?? '',
'type' => strtolower($j['type']) ?? '',
'params' => $j['params'] ?? ''
];
$this->joins []= $join;
}
return $this;
}
protected $conditions;
public function where(array $conditions): Model {
if ($this->conditions === null) {
$this->conditions = [];
}
foreach ($conditions as $c) {
$cond = (object) [
'column' => $c['column'] ?? $c[0],
'value' => $c['value'] ?? $c[1],
'sym' => strtolower($c['sym'] ?? ($c[2] ?? '=')),
'type' => strtolower($c['type']) ?? ''
];
$this->conditions []= $cond;
}
return $this;
}
protected $grouping;
public function group($groups): Model {
if ($this->grouping === null) {
$this->grouping = [];
}
if (!is_array($groups)) {
$groups = [$groups];
}
foreach ($groups as $g) {
$this->grouping []= $g;
}
return $this;
}
protected $ordering;
public function order($orders): Model {
if ($this->ordering === null) {
$this->ordering = [];
}
if (!is_array($orders)) {
$orders = [$orders];
}
foreach ($orders as $o) {
$order = (object) [
'column' => $o,
'direction' => 'asc'
];
if (is_array($o)) {
$order = (object) [
'column' => $o['column'] ?? $o[0],
'direction' => strtolower($o['direction']) ?? $o[1]
];
}
$this->ordering []= $order;
}
return $this;
}
protected $limit;
public function limit(int $limit): Model {
$this->limit = $limit;
}
protected $offset;
public function offset(int $offset): Model {
$this->offset = $offset;
}
protected function parseSelect(ORM $orm): ORM {
if ($this->columns === null) {
return $orm;
}
foreach ($this->columns as $col) {
$orm = $orm->select(trim(implode('.', $col), '.'));
}
return $orm;
}
protected function parseJoin(ORM $orm): ORM {
if ($this->joins === null) {
return $orm;
}
foreach ($this->joins as $join) {
$method = 'join';
switch ($join->type) {
case 'raw':
$method = 'rawJoin';
break;
case 'inner':
$method = 'innerJoin';
break;
case 'left':
case 'left outer':
case 'left_outer':
case 'leftouter':
$method = 'leftOuterJoin';
break;
case 'right':
case 'right outer':
case 'right_outer':
case 'rightouter':
$method = 'rightOuterJoin';
break;
case 'full':
case 'full outer':
case 'full_outer':
case 'fullouter':
$method = 'fullOuterJoin';
break;
}
if ($join->type == 'raw') {
$orm = $orm->{$method}($join->table, [$join->from, $join->symb, $join->to], $join->alias, $join->params);
} elseif ($join->alias === '') {
$orm = $orm->{$method}($join->table, [$join->from, $join->symb, $join->to], $join->alias);
} else {
$orm = $orm->{$method}($join->table, [$join->from, $join->symb, $join->to]);
}
}
return $orm;
}
protected function parseWhere(ORM $orm): ORM {
if ($this->conditions === null) {
return $orm;
}
foreach ($this->conditions as $cond) {
$method = 'where';
switch ($cond->sym) {
case '<':
$method = 'whereLt';
break;
case '<=':
$method = 'whereLte';
break;
case '>':
$method = 'whereGt';
break;
case '>=':
$method = 'whereGte';
break;
case '!=':
$method = 'whereNotEqual';
break;
case 'like':
$method = 'whereLike';
break;
case 'not like':
case 'not_like':
case 'notlike':
$method = 'whereNotLike';
break;
}
switch ($cond->type) {
case 'equal':
$method = 'whereEqual';
break;
case 'not equal':
case 'not_equal':
case 'notequal':
$method = 'whereNotEqual';
break;
case 'less':
case 'less than':
case 'less_than':
case 'lessthan':
case 'lt':
$method = 'whereLt';
break;
case 'less equal':
case 'less_equal':
case 'lessequal':
case 'less than equal':
case 'less_than_equal':
case 'lessthanequal':
case 'lte':
$method = 'whereLte';
break;
case 'greater':
case 'greater than':
case 'greater_than':
case 'greaterthan':
case 'gt':
$method = 'whereGt';
break;
case 'greater equal':
case 'greater_equal':
case 'greaterequal':
case 'greater than equal':
case 'greater_than_equal':
case 'greaterthanequal':
case 'gte':
$method = 'whereGte';
break;
case 'like':
$method = 'whereLike';
break;
case 'not like':
case 'not_like':
case 'notlike':
$method = 'whereNotLike';
break;
case 'in':
$method = 'whereIn';
break;
case 'not in':
case 'not_in':
case 'notin':
$method = 'whereNotIn';
break;
case 'raw':
$method = 'rawWhere';
break;
}
$orm = $orm->{$method}($cond->column, $cond->value);
}
return $orm;
}
protected function parseGroup(ORM $orm): ORM {
if ($this->grouping === null) {
return $orm;
}
foreach ($this->grouping as $group) {
if (strpos($group, '(') !== false) {
$orm = $orm->groupByExpr($group);
} else {
$orm = $orm->groupBy($group);
}
}
return $orm;
}
protected function parseOrder(ORM $orm): ORM {
if ($this->ordering === null) {
return $orm;
}
foreach ($this->ordering as $order) {
if (strpos($order->column, '(') !== false) {
$orm = $orm->orderByExpr($order->column);
continue;
}
switch ($order->direction) {
case 'asc':
case 'ascending':
$orm = $orm->orderByAsc($order->column);
break;
case 'desc':
case 'descending':
$orm = $orm->orderByDesc($order->column);
break;
}
}
return $orm;
}
protected function parseLimit(ORM $orm): ORM {
if ($this->limit === null) {
return $orm;
}
return $orm->limit($this->limit);
}
protected function parseOffset(ORM $orm): ORM {
if ($this->offset === null) {
return $orm;
}
return $orm->offset($this->offset);
}
public function one($id = null): ?ModelInterface {
$result = $this->build()->findOne($id);
if (!$result) {
return null;
}
$result->setFactory($this);
return $result;
}
public function many(): ?array {
$results = $this->build()->findMany();
if (!$results) {
return null;
}
foreach ($results as &$r) {
$r->setFactory($this);
}
return $results;
}
public function array(): ?array {
$results = $this->build()->findArray();
if (!$results) {
return null;
}
return $results;
}
protected function build(): ORM {
$orm = BaseModel::factory($this->class);
$methods = [
'select',
'join',
'where',
'group',
'order',
'limit',
'offset'
];
foreach ($methods as $m) {
$method = 'parse' . ucfirst($m);
$orm = $this->{$method}($orm);
}
return $orm;
}
}

6
common/Form/Model.php Normal file
View File

@ -0,0 +1,6 @@
<?php
namespace ProVM\Common\Form;
use ProVM\Common\Define\Model as BaseModel;
abstract class Model extends BaseModel {}

View File

@ -1,14 +1,13 @@
{
"name": "provm/models",
"description": "Model with Repository for Database Mapping",
"type": "project",
"description": "Model handling using j4mie/paris",
"type": "library",
"require": {
"provm/database": "^2",
"provm/query_builder": "^1"
"j4mie/paris": "^1.5"
},
"require-dev": {
"phpunit/phpunit": "^9",
"kint-php/kint": "^4"
"phpunit/phpunit": "^9.2",
"kint-php/kint": "^3.3"
},
"license": "prioprietary",
"authors": [
@ -18,26 +17,8 @@
}
],
"autoload": {
"psr-4": {
"ProVM\\": "src/"
}
},
"repositories": [
{
"type": "git",
"url": "https://git.provm.cl/ProVM/database.git"
},
{
"type": "git",
"url": "https://git.provm.cl/ProVM/query_builder.git"
}
],
"config": {
"http-basic": {
"git.provm.cl": {
"username": "aldarien",
"password": "$&At'GQmd@Ul;=j@'2d#@N&H"
}
}
"psr-4": {
"ProVM\\Common\\": "common"
}
}
}

View File

@ -1,19 +0,0 @@
<?php
namespace ProVM\Alias;
use ProVM\Concept\Model\Repository;
use ProVM\Concept\Model as ModelInterface;
abstract class Model implements ModelInterface
{
protected int $id;
public function setId(int $id): ModelInterface
{
$this->id = $id;
return $this;
}
public function getId(): int
{
return $this->id;
}
}

View File

@ -1,163 +0,0 @@
<?php
namespace ProVM\Alias\Model;
use PDOException;
use ProVM\Concept\Database\Connection;
use ProVM\Concept\Database\QueryBuilder;
use ProVM\Concept\Model;
use ProVM\Concept\Model\Factory;
use ProVM\Concept\Model\Repository as RepositoryInterface;
use ProVM\Exception\BlankResult;
abstract class Repository implements RepositoryInterface
{
public function __construct(protected Connection $connection, protected QueryBuilder $builder, protected Factory $factory)
{}
protected string $model;
public function getConnection(): Connection
{
return $this->connection;
}
public function getQueryBuilder(): QueryBuilder
{
return $this->builder;
}
public function getFactory(): Factory
{
return $this->factory;
}
public function save(Model &$model): void
{
try {
$old = $this->defaultFind($model);
$this->update($model, $old);
} catch (BlankResult $e) {
$this->insert($model);
$model->setId($this->getConnection()->getPDO()->lastInsertId());
}
}
public function update(Model $model, Model $old): void
{
$mapper = $this->getMapper();
$model_values = $mapper->mapModelToTable($model);
$old_values = $mapper->mapModelToTable($old);
$differences = array_diff_assoc($old_values, $model_values);
$columns = array_map(function($key) {
return "`{$key}` = ?";
}, array_keys($differences));
$values = array_values($differences);
if (count($columns) === 0) {
return;
}
$values []= $old->{$this->idProperty()}();
$query = $this->getQueryBuilder()->update($this->getTable())->set($columns)->where(["{$this->idField()}} = ?"]);
$this->getConnection()->execute($query, $values);
}
public function create(array $data): Model
{
try {
return $this->defaultSearch($data);
} catch (PDOException | BlankResult $e) {
$data[$this->idField()] = 0;
return $this->load($data);
}
}
public function delete(Model $model): void
{
$query = $this->getQueryBuilder()->delete($this->getTable())->where(["{$this->idField()} = ?"]);
$this->getConnection()->execute($query, [$model->{$this->idProperty()}()]);
$this->resetIndex();
$this->optimize();
}
protected function idProperty(): string
{
return 'getId';
}
protected function idField(): string
{
return 'id';
}
protected function insert(Model $model): void
{
$mapper = $this->getMapper();
$fields = $mapper->getColumns();
$fields_string = array_map(function($field) {
return "`{$field}`";
}, $fields);
$fields_questions = array_fill(0, count($fields), '?');
$query = $this->getQueryBuilder()->insert($this->getTable())->columns($fields_string)->values($fields_questions);
$values = array_values($mapper->mapModelToTable($model));
$this->getConnection()->execute($query, $values);
}
protected function resetIndex(): void
{
$query = "ALTER TABLE `{$this->getTable()}` AUTO_INCREMENT = 1";
$this->getConnection()->query($query);
}
protected function optimize(): void
{
$query = "OPTIMIZE TABLE `{$this->getTable()}`";
$this->getConnection()->query($query);
}
protected function fetchOne(string $query, ?array $values = null): Model
{
if ($values !== null) {
$rs = $this->getConnection()->prepare($query);
$rs->execute($values);
} else {
$rs = $this->getConnection()->query($query);
}
$row = $rs->getFirstAsArray();
if (!$row) {
throw new BlankResult();
}
return $this->load($row);
}
protected function fetchMany(string $query, ?array $values = null): array
{
if ($values !== null) {
$rs = $this->getConnection()->prepare($query);
$rs->execute($values);
} else {
$rs = $this->getConnection()->query($query);
}
$rows = $rs->getAsArray();
if (!$rows) {
throw new BlankResult();
}
return array_map([$this, 'load'], $rows);
}
public function fetchById(int $id): Model
{
$query = $this->getQueryBuilder()
->select()
->from($this->getTable())
->where(['id = ?'])
->limit(1);
return $this->fetchOne($query, [$id]);
}
public function fetchAll(): array
{
$query = $this->getQueryBuilder()
->select()
->from($this->getTable());
return $this->fetchMany($query);
}
abstract protected function getMapper(): Model\Mapper;
abstract protected function fieldsForUpdate(): array;
abstract protected function fieldsForInsert(): array;
abstract protected function valuesForUpdate(Model $model): array;
abstract protected function valuesForInsert(Model $model): array;
abstract protected function defaultFind(Model $model): Model;
abstract protected function defaultSearch(array $data): Model;
}

View File

@ -1,10 +0,0 @@
<?php
namespace ProVM\Concept;
use JsonSerializable;
interface Model extends JsonSerializable
{
public function setId(int $id): Model;
public function getId(): int;
}

View File

@ -1,48 +0,0 @@
<?php
namespace ProVM\Concept\Model;
use ProVM\Concept\Model;
use Psr\Container\ContainerInterface;
interface Factory
{
/**
* @param ContainerInterface $container
* @return Factory
*/
public function setContainer(ContainerInterface $container): Factory;
/**
* @return ContainerInterface
*/
public function getContainer(): ContainerInterface;
/**
* Get Repository by class
* @param string $repository_class
* @return Repository
*/
public function get(string $repository_class): Repository;
/**
* Get Repository by Model class
* @param string $model_class
* @return Repository
*/
public function getByModel(string $model_class): Repository;
/**
* Fetch Model by Id
* @param string $model_class
* @param int $id
* @return Model
*/
public function fetchById(string $model_class, int $id): Model;
/**
* Fetch all Models
* @param string $model_class
* @return array
*/
public function fetchAll(string $model_class): array;
}

View File

@ -1,11 +0,0 @@
<?php
namespace ProVM\Concept\Model;
use ProVM\Concept;
interface Mapper
{
public function getColumns(): array;
public function mapModelToTable(Concept\Model $model): array;
public function mapTableToModel(array $data, Concept\Model $model): Concept\Model;
}

View File

@ -1,73 +0,0 @@
<?php
namespace ProVM\Concept\Model;
use ProVM\Concept\Database\Connection;
use ProVM\Concept\Database\QueryBuilder;
use ProVM\Concept\Model;
interface Repository
{
/**
* @return Connection
*/
public function getConnection(): Connection;
/**
* @return QueryBuilder
*/
public function getQueryBuilder(): QueryBuilder;
/**
* @return Factory
*/
public function getFactory(): Factory;
/**
* @return string
*/
public function getModel(): string;
/**
* @return string
*/
public function getTable(): string;
/**
* Transform result array to Model
* @param array $data
* @return Model
*/
public function load(array $data): Model;
/**
* Save Model to table
* @param Model $model
* @return void
*/
public function save(Model &$model): void;
/**
* Update table value with Model
* @param Model $model
* @return void
*/
public function update(Model $model, Model $old): void;
/**
* Create new Model with data
* @param array $data
* @return Model
*/
public function create(array $data): Model;
/**
* Delete Model from table
* @param Model $model
* @return void
*/
public function delete(Model $model): void;
/**
* Fetch Model by Id
* @param int $id
* @return Model
*/
public function fetchById(int $id): Model;
/**
* Fetch all Models
* @return array
*/
public function fetchAll(): array;
}

View File

@ -1,44 +0,0 @@
<?php
namespace ProVM\Implement\Model;
use Psr\Container\ContainerInterface;
use ProVM\Concept\Model;
use ProVM\Concept\Model\Factory as FactoryInterface;
use ProVM\Concept\Model\Repository;
class Factory implements FactoryInterface
{
public function __construct(ContainerInterface $container)
{
$this->setContainer($container);
}
protected ContainerInterface $container;
public function setContainer(ContainerInterface $container): FactoryInterface
{
$this->container = $container;
return $this;
}
public function getContainer(): ContainerInterface
{
return $this->container;
}
public function get(string $repository_class): Repository
{
return $this->getContainer()->get($repository_class);
}
public function getByModel(string $model_class): Repository
{
$class = str_replace('Model', 'Repository', $model_class);
return $this->get($class);
}
public function fetchById(string $model_class, int $id): Model
{
return $this->getByModel($model_class)->fetchById($id);
}
public function fetchAll(string $model_class): array
{
return $this->getByModel($model_class)->fetchAll();
}
}

View File

@ -1,39 +0,0 @@
<?php
namespace ProVM\Implement\Model;
use ProVM\Concept;
use ProVM\Implement\Model\Mapper\PropertyMap;
class Mapper implements Concept\Model\Mapper
{
protected array $map;
public function getColumns(): array
{
return array_keys($this->map);
}
public function registerColumn(string $columnName, ?PropertyMap $propertyMap = null): Mapper
{
if ($propertyMap == null) {
$this->map[$columnName] = (new PropertyMap())->setColumn($columnName);
return $this;
}
$this->map[$columnName] = $propertyMap;
return $this;
}
public function mapModelToTable(Concept\Model $model): array
{
$data = [];
foreach ($this->map as $columnName => $propertyMap) {
$data[$columnName] = $propertyMap->mapToTable($model);
}
return $data;
}
public function mapTableToModel(array $data, Concept\Model $model): Concept\Model
{
foreach ($this->map as $propertyMap) {
$propertyMap->map($model, $data);
}
return $model;
}
}

View File

@ -1,63 +0,0 @@
<?php
namespace ProVM\Implement\Model\Mapper;
use Closure;
use Exception;
use ProVM\Concept\Model;
class PropertyMap
{
protected string $column;
protected string $property;
protected Closure $callback;
protected mixed $defaultValue;
public function setProperty(string $property): PropertyMap
{
$this->property = $property;
return $this;
}
public function setColumn(string $column): PropertyMap
{
$this->column = $column;
return $this;
}
public function setCallback(Closure $callback): PropertyMap
{
$this->callback = $callback;
return $this;
}
public function setDefaultValue(mixed $defaultValue): PropertyMap
{
$this->defaultValue = $defaultValue;
return $this;
}
public function map(Model $model, array $data): Model
{
$property = $this->property ?? $this->column;
if (isset($this->callback)) {
try {
$model->{$property} = call_user_func_array($this->callback, $data);
} catch (Exception) {
if (!isset($data[$this->column])) {
$model->{$property} = $this->defaultValue;
} else {
$model->{$property} = $data[$this->column];
}
}
return $model;
}
if (!isset($data[$this->column])) {
$model->{$property} = $this->defaultValue;
} else {
$model->{$property} = $data[$this->column];
}
return $model;
}
public function mapToTable(Model $model): mixed
{
$property = $this->property ?? $this->column;
return $model->{$property} ?? $this->defaultValue;
}
}