Skip to content

Commit 30910bc

Browse files
author
David Nahodyl
committed
Merge branch 'main' into add-eager-loading
2 parents 68a6a2b + 626246c commit 30910bc

File tree

5 files changed

+132
-3
lines changed

5 files changed

+132
-3
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace BlueFeather\EloquentFileMaker\Database\Eloquent\Concerns;
4+
5+
use Illuminate\Support\Facades\Cache;
6+
7+
trait FMGuardsAttributes
8+
{
9+
10+
/**
11+
* Determine if the given key is guarded.
12+
*
13+
* @param string $key
14+
* @return bool
15+
*/
16+
public function isGuarded($key)
17+
{
18+
if (empty($this->getGuarded())) {
19+
return false;
20+
}
21+
22+
return $this->getGuarded() == ['*'] ||
23+
!empty(preg_grep('/^' . preg_quote($key) . '$/i', $this->getGuarded())) ||
24+
!$this->isGuardableColumn($key);
25+
}
26+
27+
/**
28+
* Determine if the given column is a valid, guardable column.
29+
*
30+
* @param string $key
31+
* @return bool
32+
*/
33+
protected function isGuardableColumn($key)
34+
{
35+
$this->primeGuardableColumns();
36+
37+
if(in_array($key, static::$guardableColumns[get_class($this)])) {
38+
return true;
39+
}
40+
41+
$this->primeGuardableColumns(true);
42+
43+
return in_array($key, static::$guardableColumns[get_class($this)]);
44+
}
45+
46+
protected function primeGuardableColumns($forceRefresh = false)
47+
{
48+
if (!isset(static::$guardableColumns[get_class($this)])) {
49+
$columns = $this->getColumns($forceRefresh);
50+
51+
if (empty($columns)) {
52+
return true;
53+
}
54+
static::$guardableColumns[get_class($this)] = $columns;
55+
}
56+
}
57+
58+
59+
protected function getColumns($forceRefresh = false): array
60+
{
61+
$cacheKey = "eloquent-filemaker-{$this->table}-columns";
62+
$refreshCallback = function() {
63+
$layoutMetaData = $this->getConnection()->getLayoutMetadata($this->table);
64+
65+
return array_column($layoutMetaData['fieldMetaData'], 'name');
66+
};
67+
68+
if($forceRefresh) {
69+
Cache::forever($cacheKey, $columns = $refreshCallback());
70+
71+
return $columns;
72+
}
73+
74+
return Cache::rememberForever($cacheKey, $refreshCallback);
75+
}
76+
77+
}

src/Database/Eloquent/FMModel.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace BlueFeather\EloquentFileMaker\Database\Eloquent;
44

5+
use BlueFeather\EloquentFileMaker\Database\Eloquent\Concerns\FMGuardsAttributes;
56
use BlueFeather\EloquentFileMaker\Database\Eloquent\Concerns\FMHasRelationships;
67
use BlueFeather\EloquentFileMaker\Database\Query\FMBaseBuilder;
78
use BlueFeather\EloquentFileMaker\Exceptions\FileMakerDataApiException;
@@ -18,6 +19,7 @@ abstract class FMModel extends Model
1819
{
1920

2021
use FMHasRelationships;
22+
use FMGuardsAttributes;
2123

2224
/**
2325
* Indicates if the model should be timestamped.
@@ -62,6 +64,13 @@ abstract class FMModel extends Model
6264
*/
6365
protected $modId;
6466

67+
/**
68+
* The "type" of the primary key ID. FileMaker uses UUID strings by default.
69+
*
70+
* @var string
71+
*/
72+
protected $keyType = 'string';
73+
6574

6675
public function __construct(array $attributes = [])
6776
{

src/Database/Query/FMBaseBuilder.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -845,9 +845,13 @@ public function count($columns = '*')
845845

846846
public function whereDate($column, $operator, $value = null, $boolean = 'and')
847847
{
848+
if (is_null($value)) {
849+
$value = $operator;
850+
$operator = '=';
851+
}
848852

849-
if ($operator instanceof DateTimeInterface) {
850-
$operator = $operator->format('n/j/Y');
853+
if ($value instanceof DateTimeInterface) {
854+
$value = $value->format('n/j/Y');
851855
}
852856

853857
return $this->where($column, $operator, $value, $boolean);

src/Database/Schema/FMBuilder.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace BlueFeather\EloquentFileMaker\Database\Schema;
4+
5+
class FMBuilder extends \Illuminate\Database\Schema\Builder
6+
{
7+
/**
8+
* Get the column listing for a given table.
9+
*
10+
* @param string $table
11+
* @return array
12+
*/
13+
public function getColumnListing($table)
14+
{
15+
$layoutMetaData = $this->connection->getLayoutMetadata($table);
16+
$fieldMetaData = $layoutMetaData['fieldMetaData'];
17+
$columns = array_column($fieldMetaData, 'name');
18+
19+
return $columns;
20+
}
21+
22+
}

src/Services/FileMakerConnection.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use BlueFeather\EloquentFileMaker\Database\Eloquent\FMEloquentBuilder;
88
use BlueFeather\EloquentFileMaker\Database\Query\FMBaseBuilder;
99
use BlueFeather\EloquentFileMaker\Database\Query\Grammars\FMGrammar;
10+
use BlueFeather\EloquentFileMaker\Database\Schema\FMBuilder;
1011
use BlueFeather\EloquentFileMaker\Exceptions\FileMakerDataApiException;
1112
use GuzzleHttp\Exception\ConnectException;
1213
use GuzzleHttp\Exception\RequestException;
@@ -100,8 +101,12 @@ protected function getRecordUrl()
100101
return $this->getLayoutUrl() . '/records/';
101102
}
102103

103-
protected function getLayoutUrl()
104+
protected function getLayoutUrl($layout = null)
104105
{
106+
// Set the connection layout as the layout parameter, otherwise get the layout from the connection
107+
if($layout){
108+
$this->setLayout($layout);
109+
}
105110
return $this->getDatabaseUrl() . '/layouts/' . $this->getLayout();
106111
}
107112

@@ -637,4 +642,16 @@ protected function getDefaultQueryGrammar()
637642
{
638643
return new FMGrammar();
639644
}
645+
646+
public function getLayoutMetadata($layout = null){
647+
$response = $this->makeRequest('get', $this->getLayoutUrl($layout));
648+
return $response['response'];
649+
}
650+
651+
public function getSchemaBuilder()
652+
{
653+
parent::getSchemaBuilder();
654+
655+
return new FMBuilder($this);
656+
}
640657
}

0 commit comments

Comments
 (0)