Skip to content

Commit 4488f21

Browse files
authored
Merge pull request #103 from turbo124/main
Updated dependencies
2 parents dac1b77 + 0196c9a commit 4488f21

File tree

11 files changed

+939
-5
lines changed

11 files changed

+939
-5
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
"psr-4": {"InvoiceNinja\\Sdk\\": "src/"}
1414
},
1515
"require": {
16-
"php": "^7.4|^8.0|^8.1",
17-
"guzzlehttp/guzzle": "^7.3"
16+
"php": "^8.1|^8.2",
17+
"guzzlehttp/guzzle": "^7.5",
18+
"nesbot/carbon": "^2.66"
1819
},
1920
"require-dev": {
2021
"fakerphp/faker": "^1.16",

src/Exceptions/ApiException.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ protected static function parseResponseBody($response)
3232
$body = (string) $response->getBody();
3333
$error = "";
3434
$object = @json_decode($body);
35+
$error_array = [];
3536

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

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

46-
if(is_array($error_array))
47+
if(is_array($error_array) && count($error_array) >=1)
4748
$error = $error_array[0];
4849

4950
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
* Invoice Ninja (https://invoiceninja.com).
4+
*
5+
* @link https://github.com/invoiceninja/sdk-php source repository
6+
*
7+
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
8+
*
9+
* @license https://opensource.org/licenses/MIT
10+
*/
11+
12+
namespace InvoiceNinja\Sdk\Exceptions;
13+
14+
class ModelValidationException extends \Exception
15+
{
16+
}

src/Models/BaseModel.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
/**
3+
* Invoice Ninja (https://invoiceninja.com).
4+
*
5+
* @link https://github.com/invoiceninja/sdk-php source repository
6+
*
7+
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
8+
*
9+
* @license https://opensource.org/licenses/MIT
10+
*/
11+
12+
namespace InvoiceNinja\Sdk\Models;
13+
14+
use InvoiceNinja\Sdk\Exceptions\ModelValidationException;
15+
16+
class BaseModel
17+
{
18+
private array $validation_errors = [];
19+
20+
public function validate(): bool | ModelValidationException
21+
{
22+
$this->resetValidationErrors();
23+
24+
foreach($this->rules as $key => $rule)
25+
{
26+
27+
switch($rule)
28+
{
29+
case 'required':
30+
isset($this->{$key}) ?: $this->setValidationErrors(["error" => "{$this->model} - `{$key}` must be set", "code" => 422]);
31+
break;
32+
case 'date':
33+
isset($this->{$key}) ? $this->checkDate($key) : true;
34+
break;
35+
default:
36+
$this->setValidationErrors(["error" => "{$this->model} - Unknown validation rule `{$rule}` for property `{$key}`", "code" => 500]);
37+
38+
}
39+
40+
}
41+
42+
if(count($this->getValidationErrors()) == 0)
43+
return true;
44+
45+
throw new ModelValidationException($this->getValidationErrors()[0]["error"] ,$this->getValidationErrors()[0]["code"]);
46+
}
47+
48+
private function resetValidationErrors()
49+
{
50+
$this->validation_errors = [];
51+
52+
return $this;
53+
}
54+
55+
private function setValidationErrors($error): self
56+
{
57+
$this->validation_errors[] = $error;
58+
59+
return $this;
60+
}
61+
62+
public function getValidationErrors(): array
63+
{
64+
return $this->validation_errors;
65+
}
66+
67+
public function checkDate(string $date_field): bool
68+
{
69+
70+
try {
71+
72+
$parsed_date = \Carbon\Carbon::parse($this->{$date_field});
73+
$this->{$date_field} = $parsed_date->format('Y-m-d');
74+
75+
return true;
76+
}
77+
catch(\Exception $e) {
78+
$this->setValidationErrors([$date_field => "Invalid date format for field {$date_field} - '{$this->{$date_field}}'"]);
79+
return false;
80+
}
81+
}
82+
83+
84+
85+
86+
87+
}

0 commit comments

Comments
 (0)