Skip to content

Commit

Permalink
Merge pull request #117 from fleetbase/dev-v1.5.14
Browse files Browse the repository at this point in the history
v1.5.14
  • Loading branch information
roncodes authored Oct 15, 2024
2 parents 2db4b5b + d652c7a commit 8dd4932
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 6 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fleetbase/core-api",
"version": "1.5.13",
"version": "1.5.14",
"description": "Core Framework and Resources for Fleetbase API",
"keywords": [
"fleetbase",
Expand Down
70 changes: 70 additions & 0 deletions src/Support/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -2492,4 +2492,74 @@ public static function clearCacheByPattern(string $pattern): void
}
}
}

/**
* Converts the given input into an array.
*
* This method handles different types of inputs:
* - **Array:** Returns the array as is.
* - **String:** Splits the string into an array using common delimiters (comma or pipe).
* - **Object:** Converts the object into an associative array. If the object is traversable,
* it uses the iterator; otherwise, it extracts public properties.
*
* @param array|string|object $target the input to convert to an array
*
* @return array the converted array
*
* @example
* // Example with an array:
* $result = arrayFrom([1, 2, 3]);
* // Result: [1, 2, 3]
* @example
* // Example with a string containing delimiters:
* $result = arrayFrom('apple, banana, cherry');
* // Result: ['apple', 'banana', 'cherry']
* @example
* // Example with a traversable object:
* $iterator = new ArrayIterator(['x' => 1, 'y' => 2]);
* $result = arrayFrom($iterator);
* // Result: ['x' => 1, 'y' => 2]
* @example
* // Example with a regular object:
* class Person {
* public $name = 'John';
* public $age = 30;
* }
* $person = new Person();
* $result = arrayFrom($person);
* // Result: ['name' => 'John', 'age' => 30]
*/
public static function arrayFrom(array|string|object $target): array
{
if (is_array($target)) {
// If the target is already an array, return it as is.
return $target;
}

if (is_string($target)) {
// Define possible delimiters.
foreach ([',', '|'] as $delim) {
if (strpos($target, $delim) !== false) {
// Split the string by the delimiter and trim each element.
return array_map('trim', explode($delim, $target));
}
}

// If no delimiter is found, return the string as a single-element array.
return [$target];
}

if (is_object($target)) {
if ($target instanceof \Traversable) {
// If the object is traversable (like an iterator), convert it to an array.
return iterator_to_array($target);
} else {
// Get an associative array of the object's public properties.
return get_object_vars($target);
}
}

// If $target is none of the above types, return it as a single-element array.
return [$target];
}
}
38 changes: 34 additions & 4 deletions src/Traits/HasApiControllerBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Fleetbase\Traits;

use Closure;
use Fleetbase\Exceptions\FleetbaseRequestValidationException;
use Fleetbase\Http\Requests\Internal\BulkDeleteRequest;
use Fleetbase\Support\Http;
Expand Down Expand Up @@ -303,8 +304,9 @@ public function validateRequest(Request $request)
*/
public function queryRecord(Request $request)
{
$single = $request->boolean('single');
$data = $this->model->queryFromRequest($request);
$single = $request->boolean('single');
$queryCallback = $this->getControllerCallback('onQueryRecord');
$data = $this->model->queryFromRequest($request, $queryCallback);

if ($single) {
$data = Arr::first($data);
Expand Down Expand Up @@ -391,8 +393,11 @@ public function findRecord(Request $request, $id)
public function createRecord(Request $request)
{
try {
$onBeforeCallback = $this->getControllerCallback('onBeforeCreate');
$onAfterCallback = $this->getControllerCallback('onAfterCreate');

$this->validateRequest($request);
$record = $this->model->createRecordFromRequest($request);
$record = $this->model->createRecordFromRequest($request, $onBeforeCallback, $onAfterCallback);

if (Http::isInternalRequest($request)) {
$this->resource::wrap($this->resourceSingularlName);
Expand Down Expand Up @@ -441,8 +446,11 @@ public function createRecord(Request $request)
public function updateRecord(Request $request, string $id)
{
try {
$onBeforeCallback = $this->getControllerCallback('onBeforeUpdate');
$onAfterCallback = $this->getControllerCallback('onAfterUpdate');

$this->validateRequest($request);
$record = $this->model->updateRecordFromRequest($request, $id);
$record = $this->model->updateRecordFromRequest($request, $id, $onBeforeCallback, $onAfterCallback);

if (Http::isInternalRequest($request)) {
$this->resource::wrap($this->resourceSingularlName);
Expand Down Expand Up @@ -611,4 +619,26 @@ public function count(Request $request)

return response()->json(['count' => $results]);
}

/**
* Retrieves a Closure for a specified method of the controller if it exists.
*
* This method checks if a method with the given name exists in the current controller instance.
* If the method exists, it returns a Closure that, when invoked, will call the specified method
* with any provided arguments. This allows for dynamic method invocation while ensuring the method's existence.
*
* @param string $name the name of the controller method to retrieve as a Closure
*
* @return \Closure|null a Closure that calls the specified method, or null if the method does not exist
*/
private function getControllerCallback(string $name): ?\Closure
{
if (method_exists($this, $name)) {
return function (...$args) use ($name) {
return $this->{$name}(...$args);
};
}

return null;
}
}
9 changes: 8 additions & 1 deletion src/Traits/HasApiModelBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,14 @@ public function updateRecordFromRequest(Request $request, $id, ?callable $onBefo
}
}

$record->update($input);
// Remove ID's and timestamps from input
$input = Arr::except($input, ['uuid', 'public_id', 'deleted_at', 'updated_at', 'created_at']);

try {
$record->update($input);
} catch (\Exception $e) {
throw new \Exception('Failed to update ' . $this->getApiHumanReadableName());
}

if (isset($options['return_object']) && $options['return_object'] === true) {
return $record;
Expand Down
9 changes: 9 additions & 0 deletions src/Types/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,15 @@ class Currency implements \JsonSerializable
'decimalSeparator' => ',',
'symbolPlacement' => 'before',
],
'GEL' => [
'code' => 'GEL',
'title' => 'Georgian lari',
'symbol' => '',
'precision' => 2,
'thousandSeparator' => ',',
'decimalSeparator' => '.',
'symbolPlacement' => 'before',
],
'GHC' => [
'code' => 'GHC',
'title' => 'Ghana, Cedi',
Expand Down

0 comments on commit 8dd4932

Please sign in to comment.