Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated dependencies #103

Merged
merged 4 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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