Skip to content

Commit 67c1faa

Browse files
Merge branch 'feature/add-code-styling' into dev
2 parents d8093cb + 019f45d commit 67c1faa

File tree

20 files changed

+244
-254
lines changed

20 files changed

+244
-254
lines changed

.github/workflows/php.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: PHP Composer
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
- dev
11+
12+
permissions:
13+
contents: read
14+
15+
jobs:
16+
build:
17+
18+
runs-on: ubuntu-latest
19+
20+
steps:
21+
- uses: actions/checkout@v3
22+
23+
- name: Validate composer.json and composer.lock
24+
run: composer validate --strict
25+
26+
- name: Cache Composer packages
27+
id: composer-cache
28+
uses: actions/cache@v3
29+
with:
30+
path: vendor
31+
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
32+
restore-keys: |
33+
${{ runner.os }}-php-
34+
35+
- name: Install dependencies
36+
run: composer install --prefer-dist --no-progress
37+
38+
- name: Check Code Style
39+
run: ./vendor/bin/pint --test
40+
41+
# Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit"
42+
# Docs: https://getcomposer.org/doc/articles/scripts.md
43+
44+
# - name: Run test suite
45+
# run: composer run-script test

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
},
3333
"require-dev": {
3434
"orchestra/testbench": "^7.0",
35-
"mockery/mockery": "^1.5.1"
35+
"mockery/mockery": "^1.5.1",
36+
"laravel/pint": "^1.1"
3637
},
3738
"extra": {
3839
"laravel": {

pint.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"presets": "laravel",
3+
"rules": {
4+
"concat_space": {
5+
"spacing": "one"
6+
}
7+
}
8+
}

src/Database/Eloquent/Concerns/FMGuardsAttributes.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66

77
trait FMGuardsAttributes
88
{
9-
109
/**
1110
* Determine if the given key is guarded.
1211
*
13-
* @param string $key
12+
* @param string $key
1413
* @return bool
1514
*/
1615
public function isGuarded($key)
@@ -20,21 +19,21 @@ public function isGuarded($key)
2019
}
2120

2221
return $this->getGuarded() == ['*'] ||
23-
!empty(preg_grep('/^' . preg_quote($key) . '$/i', $this->getGuarded())) ||
24-
!$this->isGuardableColumn($key);
22+
! empty(preg_grep('/^' . preg_quote($key) . '$/i', $this->getGuarded())) ||
23+
! $this->isGuardableColumn($key);
2524
}
2625

2726
/**
2827
* Determine if the given column is a valid, guardable column.
2928
*
30-
* @param string $key
29+
* @param string $key
3130
* @return bool
3231
*/
3332
protected function isGuardableColumn($key)
3433
{
3534
$this->primeGuardableColumns();
3635

37-
if(in_array($key, static::$guardableColumns[get_class($this)])) {
36+
if (in_array($key, static::$guardableColumns[get_class($this)])) {
3837
return true;
3938
}
4039

@@ -45,7 +44,7 @@ protected function isGuardableColumn($key)
4544

4645
protected function primeGuardableColumns($forceRefresh = false)
4746
{
48-
if (!isset(static::$guardableColumns[get_class($this)])) {
47+
if (! isset(static::$guardableColumns[get_class($this)])) {
4948
$columns = $this->getColumns($forceRefresh);
5049

5150
if (empty($columns)) {
@@ -55,23 +54,21 @@ protected function primeGuardableColumns($forceRefresh = false)
5554
}
5655
}
5756

58-
5957
protected function getColumns($forceRefresh = false): array
6058
{
6159
$cacheKey = "eloquent-filemaker-{$this->table}-columns";
62-
$refreshCallback = function() {
60+
$refreshCallback = function () {
6361
$layoutMetaData = $this->getConnection()->getLayoutMetadata($this->table);
6462

6563
return array_column($layoutMetaData['fieldMetaData'], 'name');
6664
};
6765

68-
if($forceRefresh) {
66+
if ($forceRefresh) {
6967
Cache::forever($cacheKey, $columns = $refreshCallback());
7068

7169
return $columns;
7270
}
7371

7472
return Cache::rememberForever($cacheKey, $refreshCallback);
7573
}
76-
7774
}

src/Database/Eloquent/Concerns/FMHasAttributes.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@
33
namespace GearboxSolutions\EloquentFileMaker\Database\Eloquent\Concerns;
44

55
use DateTime;
6-
use Illuminate\Database\Eloquent\Concerns\HasAttributes;
76
use Illuminate\Support\Arr;
87

98
trait FMHasAttributes
109
{
11-
1210
/**
1311
* Set a given attribute on the model.
1412
*
15-
* @param string $key
16-
* @param mixed $value
13+
* @param string $key
14+
* @param mixed $value
1715
* @return mixed
1816
*/
1917
public function setAttribute($key, $value)
@@ -36,7 +34,7 @@ public function setAttribute($key, $value)
3634
// When writing dates the regular datetime format won't work, so we have to get JUST the date value
3735
// check the key's cast to see if it is cast to a date or custom date:format
3836
$castType = $this->getCasts()[$key] ?? '';
39-
$isDate = $castType == "date" || str_starts_with($castType, 'date:');
37+
$isDate = $castType == 'date' || str_starts_with($castType, 'date:');
4038
if ($isDate) {
4139
$value = Arr::first(explode(' ', $value));
4240
}
@@ -55,5 +53,4 @@ public function setAttribute($key, $value)
5553

5654
return $this;
5755
}
58-
5956
}

src/Database/Eloquent/Concerns/FMHasRelationships.php

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
43
namespace GearboxSolutions\EloquentFileMaker\Database\Eloquent\Concerns;
54

65
use GearboxSolutions\EloquentFileMaker\Database\Eloquent\Relations\BelongsTo;
@@ -11,13 +10,12 @@
1110

1211
trait FMHasRelationships
1312
{
14-
1513
/**
1614
* Define a one-to-one relationship.
1715
*
18-
* @param string $related
19-
* @param string|null $foreignKey
20-
* @param string|null $localKey
16+
* @param string $related
17+
* @param string|null $foreignKey
18+
* @param string|null $localKey
2119
*/
2220
public function hasOne($related, $foreignKey = null, $localKey = null)
2321
{
@@ -30,13 +28,12 @@ public function hasOne($related, $foreignKey = null, $localKey = null)
3028
return $this->newHasOne($instance->newQuery(), $this, $foreignKey, $localKey);
3129
}
3230

33-
3431
/**
3532
* Define a one-to-many relationship.
3633
*
37-
* @param string $related
38-
* @param string|null $foreignKey
39-
* @param string|null $localKey
34+
* @param string $related
35+
* @param string|null $foreignKey
36+
* @param string|null $localKey
4037
* @return \Illuminate\Database\Eloquent\Relations\HasMany
4138
*/
4239
public function hasMany($related, $foreignKey = null, $localKey = null)
@@ -55,11 +52,9 @@ public function hasMany($related, $foreignKey = null, $localKey = null)
5552
/**
5653
* Instantiate a new BelongsTo relationship.
5754
*
58-
* @param \Illuminate\Database\Eloquent\Builder $query
59-
* @param \Illuminate\Database\Eloquent\Model $child
60-
* @param string $foreignKey
61-
* @param string $ownerKey
62-
* @param string $relation
55+
* @param string $foreignKey
56+
* @param string $ownerKey
57+
* @param string $relation
6358
* @return BelongsTo
6459
*/
6560
protected function newBelongsTo(Builder $query, Model $child, $foreignKey, $ownerKey, $relation)
@@ -71,10 +66,8 @@ protected function newBelongsTo(Builder $query, Model $child, $foreignKey, $owne
7166
/**
7267
* Instantiate a new HasMany relationship.
7368
*
74-
* @param \Illuminate\Database\Eloquent\Builder $query
75-
* @param \Illuminate\Database\Eloquent\Model $parent
76-
* @param string $foreignKey
77-
* @param string $localKey
69+
* @param string $foreignKey
70+
* @param string $localKey
7871
* @return HasMany
7972
*/
8073
protected function newHasMany(Builder $query, Model $parent, $foreignKey, $localKey)
@@ -112,17 +105,13 @@ public function morphMany($related, $name, $type = null, $id = null, $localKey =
112105
/**
113106
* Instantiate a new HasOne relationship.
114107
*
115-
* @param \Illuminate\Database\Eloquent\Builder $query
116-
* @param \Illuminate\Database\Eloquent\Model $parent
117-
* @param string $foreignKey
118-
* @param string $localKey
108+
* @param string $foreignKey
109+
* @param string $localKey
119110
* @return HasOne
120111
*/
121112
protected function newHasOne(Builder $query, Model $parent, $foreignKey, $localKey)
122113
{
123114
// custom version of this so we can return our own BelongsTo class with a custom constraint for FM
124115
return new HasOne($query, $parent, $foreignKey, $localKey);
125116
}
126-
127-
128117
}

src/Database/Eloquent/FMEloquentBuilder.php

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
43
namespace GearboxSolutions\EloquentFileMaker\Database\Eloquent;
54

65
use GearboxSolutions\EloquentFileMaker\Database\Eloquent\Concerns\FMHasRelationships;
@@ -18,6 +17,7 @@ class FMEloquentBuilder extends Builder
1817

1918
/**
2019
* @return Collection
20+
*
2121
* @throws FileMakerDataApiException
2222
*/
2323
public function get($columns = ['*'])
@@ -40,8 +40,8 @@ public function get($columns = ['*'])
4040
/**
4141
* Set the affected Eloquent model and instance ids.
4242
*
43-
* @param FMModel $model
44-
* @param int|array $ids
43+
* @param FMModel $model
44+
* @param int|array $ids
4545
* @return $this
4646
*/
4747
public function setModel($model, $ids = [])
@@ -55,7 +55,6 @@ public function setModel($model, $ids = [])
5555
return $this;
5656
}
5757

58-
5958
/**
6059
* Determine if any rows exist for the current query.
6160
* This actually runs the full query, so it doesn't save you any data, just an error capture
@@ -86,13 +85,13 @@ public function exists()
8685
*/
8786
public function doesntExist()
8887
{
89-
return !$this->exists();
88+
return ! $this->exists();
9089
}
9190

9291
/**
9392
* Add a where clause on the primary key to the query.
9493
*
95-
* @param mixed $id
94+
* @param mixed $id
9695
* @return $this
9796
*/
9897
public function whereKeyNot($id)
@@ -108,7 +107,7 @@ public function whereKeyNot($id)
108107
}
109108

110109
// If this is our first where clause we can add the omit directly
111-
if (count($this->wheres) === 0){
110+
if (count($this->wheres) === 0) {
112111
return $this->where($this->model->getKeyName(), '==', $id)->omit();
113112
}
114113

@@ -121,13 +120,14 @@ public function findByRecordId($recordId)
121120
$response = $this->query->findByRecordId($recordId);
122121
$newRecord = $response['response']['data'][0];
123122
$newModel = $this->model::createFromRecord($newRecord);
123+
124124
return $newModel;
125125
}
126126

127127
/**
128128
* Get a single column's value from the first result of a query.
129129
*
130-
* @param string $column
130+
* @param string $column
131131
* @return mixed
132132
*/
133133
public function value($column)
@@ -162,15 +162,15 @@ public function editRecord()
162162
$fieldsToWrite = $this->model->getAttributesForFileMakerWrite();
163163

164164
$modifiedPortals = null;
165-
foreach($fieldsToWrite as $key => $value) {
165+
foreach ($fieldsToWrite as $key => $value) {
166166
// Check if the field is a portal (it should be an array if it is)
167167
if (is_array($value)) {
168168
$modifiedPortals[$key] = $this->getOnlyModifiedPortalFields($fieldsToWrite[$key], $this->model->getOriginal($key));
169169
$fieldsToWrite->forget($key);
170170
}
171171
}
172172

173-
if ($fieldsToWrite->count() > 0 || sizeof($modifiedPortals ?? []) > 0) {
173+
if ($fieldsToWrite->count() > 0 || count($modifiedPortals ?? []) > 0) {
174174
// we have some regular text fields to update
175175
// forward this request to a base query builder to execute the edit record request
176176
$response = $this->query->fieldData($fieldsToWrite->toArray())->portalData($modifiedPortals)->recordId($model->getRecordId())->editRecord();
@@ -228,10 +228,10 @@ public function duplicate()
228228
/**
229229
* Paginate the given query.
230230
*
231-
* @param int|null $perPage
232-
* @param array $columns
233-
* @param string $pageName
234-
* @param int|null $page
231+
* @param int|null $perPage
232+
* @param array $columns
233+
* @param string $pageName
234+
* @param int|null $page
235235
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
236236
*
237237
* @throws \InvalidArgumentException
@@ -260,21 +260,20 @@ public function paginate($perPage = null, $columns = ['*'], $pageName = 'page',
260260
*
261261
* @param $array1 array The modified portal data
262262
* @param $array2 array The model's original portal data
263-
* @return array
264263
*/
265264
protected function getOnlyModifiedPortalFields($array1, $array2): array
266265
{
267266
$result = [];
268-
foreach($array1 as $key => $val) {
269-
if($array2[$key] != $val){
267+
foreach ($array1 as $key => $val) {
268+
if ($array2[$key] != $val) {
270269
// go recursive if we're comparing two arrays
271-
if(is_array($val) && is_array($array2[$key])){
270+
if (is_array($val) && is_array($array2[$key])) {
272271
$result[$key] = $this->getOnlyModifiedPortalFields($val, $array2[$key]);
273-
} else{
272+
} else {
274273
// These are normal values, so compare directly
275274
$result[$key] = $val;
276275
// at least one field is modified, so also set the recordID if it isn't set yet
277-
if(!isset($result['recordId'])){
276+
if (! isset($result['recordId'])) {
278277
$result['recordId'] = $array1['recordId'];
279278
}
280279
}

0 commit comments

Comments
 (0)