forked from mixerapi/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelPropertyFactory.php
More file actions
79 lines (70 loc) · 2.22 KB
/
ModelPropertyFactory.php
File metadata and controls
79 lines (70 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
declare(strict_types=1);
namespace MixerApi\Core\Model;
use Cake\Database\Schema\TableSchema;
use Cake\Datasource\EntityInterface;
use Cake\ORM\Table;
use Cake\Validation\Validator;
/**
* Builds ModelProperty
*
* @see ModelProperty
*/
class ModelPropertyFactory
{
/**
* @param \Cake\Database\Schema\TableSchema $schema cake TableSchema instance
* @param \Cake\ORM\Table $table cake Table instance
* @param string $columnName the tables column name
* @param \Cake\Datasource\EntityInterface $entity EntityInterface
*/
public function __construct(
private TableSchema $schema,
private Table $table,
private string $columnName,
private EntityInterface $entity,
) {
}
/**
* @return \MixerApi\Core\Model\ModelProperty
*/
public function create(): ModelProperty
{
$column = $this->schema->getColumn($this->columnName);
$default = $column['default'] ?? '';
return (new ModelProperty())
->setName($this->columnName)
->setType($this->schema->getColumnType($this->columnName))
->setDefault((string)$default)
->setIsPrimaryKey($this->isPrimaryKey())
->setIsHidden(in_array($this->columnName, $this->entity->getHidden()))
->setIsAccessible($this->isAccessible())
->setValidationSet($this->table->validationDefault(new Validator())->field($this->columnName));
}
/**
* Checks if this column is part of the primary key.
*
* @return bool
*/
private function isPrimaryKey(): bool
{
return in_array($this->columnName, $this->schema->getPrimaryKey());
}
/**
* Returns accessibility of the property.
*
* @link https://book.cakephp.org/4/en/orm/entities.html#mass-assignment
* @return bool
*/
private function isAccessible(): bool
{
$accessible = $this->entity->getAccessible();
if (isset($accessible[$this->columnName]) && is_bool($accessible[$this->columnName])) {
return $accessible[$this->columnName];
}
if (isset($accessible['*']) && is_bool($accessible['*'])) {
return $accessible['*'];
}
return false;
}
}