44namespace MixerApi \Core \Model ;
55
66use Cake \Database \Schema \TableSchema ;
7+ use Cake \Datasource \EntityInterface ;
78use Cake \ORM \Table ;
89use Cake \Validation \Validator ;
910
@@ -18,9 +19,14 @@ class ModelPropertyFactory
1819 * @param \Cake\Database\Schema\TableSchema $schema cake TableSchema instance
1920 * @param \Cake\ORM\Table $table cake Table instance
2021 * @param string $columnName the tables column name
22+ * @param \Cake\Datasource\EntityInterface $entity EntityInterface
2123 */
22- public function __construct (private TableSchema $ schema , private Table $ table , private string $ columnName )
23- {
24+ public function __construct (
25+ private TableSchema $ schema ,
26+ private Table $ table ,
27+ private string $ columnName ,
28+ private EntityInterface $ entity
29+ ) {
2430 }
2531
2632 /**
@@ -36,6 +42,8 @@ public function create()
3642 ->setType ($ this ->schema ->getColumnType ($ this ->columnName ))
3743 ->setDefault ((string )$ default )
3844 ->setIsPrimaryKey ($ this ->isPrimaryKey ($ vars , $ this ->columnName ))
45+ ->setIsHidden (in_array ($ this ->columnName , $ this ->entity ->getHidden ()))
46+ ->setIsAccessible ($ this ->isAccessible ())
3947 ->setValidationSet ($ this ->table ->validationDefault (new Validator ())->field ($ this ->columnName ));
4048 }
4149
@@ -55,4 +63,24 @@ private function isPrimaryKey(array $schemaDebugInfo, string $columnName): bool
5563
5664 return in_array ($ columnName , $ schemaDebugInfo ['constraints ' ]['primary ' ]['columns ' ]);
5765 }
66+
67+ /**
68+ * Returns accessibility of the property.
69+ *
70+ * @link https://book.cakephp.org/4/en/orm/entities.html#mass-assignment
71+ * @return bool
72+ */
73+ private function isAccessible (): bool
74+ {
75+ $ accessible = $ this ->entity ->getAccessible ();
76+ if (isset ($ accessible [$ this ->columnName ]) && is_bool ($ accessible [$ this ->columnName ])) {
77+ return $ accessible [$ this ->columnName ];
78+ }
79+
80+ if (isset ($ accessible ['* ' ]) && is_bool ($ accessible ['* ' ])) {
81+ return $ accessible ['* ' ];
82+ }
83+
84+ return false ;
85+ }
5886}
0 commit comments