77use BlueFeather \EloquentFileMaker \Exceptions \FileMakerDataApiException ;
88use BlueFeather \EloquentFileMaker \Services \FileMakerConnection ;
99use Illuminate \Database \Query \Builder ;
10- use Illuminate \Database \Query \Grammars \Grammar ;
11- use Illuminate \Database \Query \Processors \Processor ;
1210use Illuminate \Http \File ;
1311use Illuminate \Support \Collection ;
1412use InvalidArgumentException ;
@@ -156,21 +154,6 @@ class FMBaseBuilder extends Builder
156154 public $ containerFile ;
157155
158156
159- /**
160- * Create a new query builder instance.
161- *
162- * @param FileMakerConnection $connection
163- * @param \Illuminate\Database\Query\Grammars\Grammar|null $grammar
164- * @param \Illuminate\Database\Query\Processors\Processor|null $processor
165- * @return void
166- */
167- public function __construct (FileMakerConnection $ connection ,
168- Grammar $ grammar = null ,
169- Processor $ processor = null )
170- {
171- $ this ->connection = $ connection ;
172- }
173-
174157
175158 /**
176159 * Add a basic where clause to the query.
@@ -219,28 +202,33 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
219202 return $ this ;
220203 }
221204
222- // convenience method for getting the first result of a find
223- public function first ($ columns = ['* ' ])
224- {
225- // set the limit so we don't query more than we need to
226- $ result = $ this ->limit (1 )->get ()->first ();
227- return $ result ;
228- }
229-
230205 /**
231206 * Delete records from the database.
232207 *
233208 * @return int
209+ * @throws FileMakerDataApiException
234210 */
235211 public function delete ($ recordId = null )
236212 {
237- $ this ->recordId ($ recordId ?? $ this ->getRecordId ());
213+ // If an ID is passed to the method we will delete the record with this internal FileMaker record ID
214+ if (! is_null ($ recordId )) {
215+ $ this ->recordId ($ recordId );
216+ }
238217
239- // return 0 if there's no record ID, as we can't delete anything
240- // deleting nothing is supported normally, but should return a 0
241- if ($ this ->getRecordId () === null ) return 0 ;
218+ $ this ->applyBeforeQueryCallbacks ();
242219
243- return $ this ->connection ->deleteRecord ($ this );
220+ try {
221+ $ this ->connection ->deleteRecord ($ this );
222+ } catch (FileMakerDataApiException $ e ){
223+ if ($ e ->getCode () === 101 ){
224+ // no record was found to be deleted, return modified count of 0
225+ return 0 ;
226+ } else {
227+ throw $ e ;
228+ }
229+ }
230+ // we deleted the record, return modified count of 1
231+ return 1 ;
244232 }
245233
246234 /**
@@ -253,7 +241,8 @@ public function getRecordId()
253241 }
254242
255243 /**
256- * @param mixed $recordId
244+ * Set the internal FileMaker Record ID to be used for these queries
245+ * @param int $recordId
257246 */
258247 public function recordId ($ recordId )
259248 {
@@ -275,7 +264,8 @@ public function orderBy($column, $direction = self::ASCEND): FMBaseBuilder
275264 * @param string $direction
276265 * @return $this
277266 */
278- public function sort ($ column , $ direction = self ::ASCEND ){
267+ public function sort ($ column , string $ direction = self ::ASCEND ): FMBaseBuilder
268+ {
279269 return $ this ->orderBy ($ column , $ direction );
280270 }
281271
@@ -289,7 +279,7 @@ public function orderByDesc($column): FMBaseBuilder
289279
290280 protected function appendSortOrder ($ column , $ direction )
291281 {
292- array_push ( $ this ->sorts , ['fieldName ' => $ this ->getMappedFieldName ($ column ), 'sortOrder ' => $ direction ]) ;
282+ $ this ->sorts [] = ['fieldName ' => $ this ->getMappedFieldName ($ column ), 'sortOrder ' => $ direction ];
293283 }
294284
295285 /**
@@ -305,9 +295,9 @@ public function limit($value): FMBaseBuilder
305295 }
306296
307297
308- public function offset ($ offset ): FMBaseBuilder
298+ public function offset ($ value ): FMBaseBuilder
309299 {
310- $ this ->offset = $ offset ;
300+ $ this ->offset = $ value ;
311301 return $ this ;
312302 }
313303
@@ -388,26 +378,22 @@ public function get($columns = ['*'])
388378 }
389379 }
390380 $ records = collect ($ response ['response ' ]['data ' ]);
381+
382+ // filter to only requested columns
383+ if ($ columns !== ['* ' ]){
384+ $ records = $ records ->intersectByKeys (array_flip ($ columns ));
385+ }
391386 return $ records ;
392387 }
393388
394- /**
395- * Get the database connection instance.
396- *
397- * @return FileMakerConnection
398- */
399- public function getConnection ()
400- {
401- return $ this ->connection ;
402- }
403389
404390 /**
405391 * Gets the gets the name of the mapped FileMaker field for a particular column
406392 *
407- * @param $column
408- * @return mixed
393+ * @param string $column
394+ * @return string
409395 */
410- protected function getMappedFieldName ($ column )
396+ protected function getMappedFieldName (string $ column )
411397 {
412398 // remap the field name if the dev specified a mapping
413399 return array_flip ($ this ->getFieldMapping ())[$ column ] ?? $ column ;
@@ -419,7 +405,7 @@ protected function getMappedFieldName($column)
419405 * @param $array array An array of columns and their values
420406 * @return array
421407 */
422- protected function mapFieldNamesForArray ($ array )
408+ protected function mapFieldNamesForArray (array $ array ): array
423409 {
424410
425411 $ mappedArray = [];
@@ -441,17 +427,18 @@ public function getFieldMapping(): array
441427 /**
442428 * @param array $fieldMapping
443429 */
444- public function setFieldMapping (array $ fieldMapping ): void
430+ public function setFieldMapping ($ fieldMapping ): void
445431 {
446432 $ this ->fieldMapping = $ fieldMapping ;
447433 }
448434
449435 /**
450436 * Sets the current find request as an omit.
451437 * Optionally may pass false as a parameter to make a request NOT an omit if it was already set
452- *
438+ * @param bool $boolean
439+ * @return FMBaseBuilder
453440 */
454- public function omit ($ boolean = true )
441+ public function omit ($ boolean = true ): FMBaseBuilder
455442 {
456443
457444 $ count = sizeof ($ this ->wheres );
@@ -473,6 +460,7 @@ public function omit($boolean = true)
473460 * Retrieve the minimum value of a given column.
474461 *
475462 * @param string $column
463+ * @param string $direction
476464 * @return mixed
477465 */
478466 public function min ($ column , $ direction = self ::ASCEND )
@@ -494,12 +482,23 @@ public function max($column)
494482 return $ this ->min ($ column , self ::DESCEND );
495483 }
496484
485+ /**
486+ * Edit the record and get the raw FileMaker Data API Response
487+ *
488+ * @throws FileMakerDataApiException
489+ */
497490 public function editRecord ()
498491 {
499492 $ response = $ this ->connection ->editRecord ($ this );
500493 return $ response ;
501494 }
502495
496+ /**
497+ * Create a record and get the raw FileMaker Data API Response
498+ *
499+ * @return bool
500+ * @throws FileMakerDataApiException
501+ */
503502 public function createRecord (){
504503 $ response = $ this ->connection ->createRecord ($ this );
505504 return $ response ;
@@ -580,7 +579,7 @@ public function update(array $values)
580579
581580 $ this ->fieldData ($ values );
582581
583- return $ this ->connection ->update ($ this , null );
582+ return $ this ->connection ->update ($ this );
584583 }
585584
586585 public function layout ($ layoutName )
0 commit comments