File tree Expand file tree Collapse file tree 1 file changed +55
-0
lines changed
src/Database/Eloquent/Concerns Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace BlueFeather \EloquentFileMaker \Database \Eloquent \Concerns ;
4+
5+ trait FMGuardsAttributes
6+ {
7+
8+ /**
9+ * Determine if the given key is guarded.
10+ *
11+ * @param string $key
12+ * @return bool
13+ */
14+ public function isGuarded ($ key )
15+ {
16+ if (empty ($ this ->getGuarded ())) {
17+ return false ;
18+ }
19+
20+ return $ this ->getGuarded () == ['* ' ] ||
21+ ! empty (preg_grep ('/^ ' .preg_quote ($ key ).'$/i ' , $ this ->getGuarded ())) ||
22+ ! $ this ->isGuardableColumn ($ key );
23+ }
24+
25+ /**
26+ * Determine if the given column is a valid, guardable column.
27+ *
28+ * @param string $key
29+ * @return bool
30+ */
31+ protected function isGuardableColumn ($ key )
32+ {
33+ if (! isset (static ::$ guardableColumns [get_class ($ this )])) {
34+ $ columns = $ this ->getColumns ();
35+
36+ if (empty ($ columns )) {
37+ return true ;
38+ }
39+ static ::$ guardableColumns [get_class ($ this )] = $ columns ;
40+ }
41+
42+ return in_array ($ key , static ::$ guardableColumns [get_class ($ this )]);
43+ }
44+
45+
46+
47+ protected function getColumns (): array {
48+ $ layoutMetaData = $ this ->getConnection ()->getLayoutMetadata ($ this ->table );
49+ $ fieldMetaData = $ layoutMetaData ['fieldMetaData ' ];
50+ $ columns = array_column ($ fieldMetaData , 'name ' );
51+
52+ return $ columns ;
53+ }
54+
55+ }
You can’t perform that action at this time.
0 commit comments