Skip to content

Commit

Permalink
Merge pull request #103 from turbo124/main
Browse files Browse the repository at this point in the history
Updated dependencies
  • Loading branch information
turbo124 authored Mar 30, 2023
2 parents dac1b77 + 0196c9a commit 4488f21
Show file tree
Hide file tree
Showing 11 changed files with 939 additions and 5 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
"psr-4": {"InvoiceNinja\\Sdk\\": "src/"}
},
"require": {
"php": "^7.4|^8.0|^8.1",
"guzzlehttp/guzzle": "^7.3"
"php": "^8.1|^8.2",
"guzzlehttp/guzzle": "^7.5",
"nesbot/carbon": "^2.66"
},
"require-dev": {
"fakerphp/faker": "^1.16",
Expand Down
5 changes: 3 additions & 2 deletions src/Exceptions/ApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ protected static function parseResponseBody($response)
$body = (string) $response->getBody();
$error = "";
$object = @json_decode($body);
$error_array = [];

if(property_exists($object, "message"))
$error = $object->message;
Expand All @@ -40,10 +41,10 @@ protected static function parseResponseBody($response)
{
$properties = array_keys(get_object_vars($object->errors));

if(is_array($properties))
if(is_array($properties) && count($properties) >=1)
$error_array = $object->errors->{$properties[0]};

if(is_array($error_array))
if(is_array($error_array) && count($error_array) >=1)
$error = $error_array[0];

}
Expand Down
16 changes: 16 additions & 0 deletions src/Exceptions/ModelValidationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/sdk-php source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/MIT
*/

namespace InvoiceNinja\Sdk\Exceptions;

class ModelValidationException extends \Exception
{
}
87 changes: 87 additions & 0 deletions src/Models/BaseModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/sdk-php source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/MIT
*/

namespace InvoiceNinja\Sdk\Models;

use InvoiceNinja\Sdk\Exceptions\ModelValidationException;

class BaseModel
{
private array $validation_errors = [];

public function validate(): bool | ModelValidationException
{
$this->resetValidationErrors();

foreach($this->rules as $key => $rule)
{

switch($rule)
{
case 'required':
isset($this->{$key}) ?: $this->setValidationErrors(["error" => "{$this->model} - `{$key}` must be set", "code" => 422]);
break;
case 'date':
isset($this->{$key}) ? $this->checkDate($key) : true;
break;
default:
$this->setValidationErrors(["error" => "{$this->model} - Unknown validation rule `{$rule}` for property `{$key}`", "code" => 500]);

}

}

if(count($this->getValidationErrors()) == 0)
return true;

throw new ModelValidationException($this->getValidationErrors()[0]["error"] ,$this->getValidationErrors()[0]["code"]);
}

private function resetValidationErrors()
{
$this->validation_errors = [];

return $this;
}

private function setValidationErrors($error): self
{
$this->validation_errors[] = $error;

return $this;
}

public function getValidationErrors(): array
{
return $this->validation_errors;
}

public function checkDate(string $date_field): bool
{

try {

$parsed_date = \Carbon\Carbon::parse($this->{$date_field});
$this->{$date_field} = $parsed_date->format('Y-m-d');

return true;
}
catch(\Exception $e) {
$this->setValidationErrors([$date_field => "Invalid date format for field {$date_field} - '{$this->{$date_field}}'"]);
return false;
}
}





}
Loading

0 comments on commit 4488f21

Please sign in to comment.