| 1 | <?php |
||||||
| 2 | |||||||
| 3 | declare(strict_types=1); |
||||||
| 4 | |||||||
| 5 | namespace VGirol\JsonApiStructure\Concern; |
||||||
| 6 | |||||||
| 7 | use VGirol\JsonApiConstant\Members; |
||||||
| 8 | use VGirol\JsonApiStructure\Messages; |
||||||
| 9 | |||||||
| 10 | /** |
||||||
| 11 | * Validations relating to the jsonapi object |
||||||
| 12 | */ |
||||||
| 13 | trait ValidateJsonapiObject |
||||||
| 14 | { |
||||||
| 15 | /** |
||||||
| 16 | * Asserts that a json fragment is a valid jsonapi object. |
||||||
| 17 | * |
||||||
| 18 | * It will do the following checks : |
||||||
| 19 | * 1) asserts that the jsonapi object is not an array of objects (@see mustNotBeArrayOfObjects). |
||||||
| 20 | * 2) asserts that the jsonapi object contains only the following allowed members : "version" and "meta" |
||||||
| 21 | * (@see containsOnlyAllowedMembers). |
||||||
| 22 | * |
||||||
| 23 | * Optionaly, if presents, it will : |
||||||
| 24 | * 3) asserts that the version member is a string. |
||||||
| 25 | * 4) asserts that meta member is valid (@see validateMetaObject). |
||||||
| 26 | * |
||||||
| 27 | * @param array $json |
||||||
| 28 | * @param boolean $strict If true, unsafe characters are not allowed when checking members name. |
||||||
| 29 | * |
||||||
| 30 | * @return void |
||||||
| 31 | */ |
||||||
| 32 | 24 | public function validateJsonapiObject($json, bool $strict): void |
|||||
| 33 | { |
||||||
| 34 | 24 | $this->mustNotBeArrayOfObjects($json); |
|||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 35 | |||||||
| 36 | 21 | $this->containsOnlyAllowedMembers( |
|||||
|
0 ignored issues
–
show
It seems like
containsOnlyAllowedMembers() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 37 | 21 | $this->getRule('JsonapiObject.Allowed'), |
|||||
|
0 ignored issues
–
show
It seems like
getRule() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 38 | $json |
||||||
| 39 | ); |
||||||
| 40 | |||||||
| 41 | 18 | if (\array_key_exists(Members::JSONAPI_VERSION, $json)) { |
|||||
| 42 | 18 | if (!\is_string($json[Members::JSONAPI_VERSION])) { |
|||||
| 43 | 6 | $this->throw(Messages::JSONAPI_OBJECT_VERSION_MEMBER_MUST_BE_STRING, 403); |
|||||
|
0 ignored issues
–
show
It seems like
throw() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 44 | } |
||||||
| 45 | } |
||||||
| 46 | |||||||
| 47 | 12 | if (\array_key_exists(Members::META, $json)) { |
|||||
| 48 | 9 | $this->validateMetaObject($json[Members::META], $strict); |
|||||
|
0 ignored issues
–
show
It seems like
validateMetaObject() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 49 | } |
||||||
| 50 | 6 | } |
|||||
| 51 | } |
||||||
| 52 |