Skip to content

Commit df48378

Browse files
author
David Nahodyl
committed
Added custom fmguardsattributes as alternative - this should be deleted if we're using the schema builder
1 parent d0a04fc commit df48378

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
}

0 commit comments

Comments
 (0)