ValidateArrays::isNotArrayOfObjects()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VGirol\JsonApiStructure\Concern;
6
7
use VGirol\JsonApiStructure\Messages;
8
9
/**
10
 * Validations relating to the arrays
11
 */
12
trait ValidateArrays
13
{
14
    /**
15
     * Check if an array is an array of objects.
16
     *
17
     * @param array $json
18
     *
19
     * @return bool
20
     */
21 183
    public function isArrayOfObjects($json): bool
22
    {
23 183
        $this->isValidArgument(1, 'array', $json);
0 ignored issues
show
Bug introduced by
It seems like isValidArgument() 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 ignore-call  annotation

23
        $this->/** @scrutinizer ignore-call */ 
24
               isValidArgument(1, 'array', $json);
Loading history...
24
25 180
        if (\count($json) == 0) {
26 3
            return true;
27
        }
28
29 177
        return !$this->arrayIsAssociative($json);
30
    }
31
32
    /**
33
     * Validate that an array is an array of objects.
34
     *
35
     * @param array       $json
36
     * @param string|null $message     An optional message to explain why the test failed
37
     * @param mixed       $code
38
     *
39
     * @return void
40
     * @throws \VGirol\JsonApiStructure\Exception\ValidationException
41
     */
42 54
    public function mustBeArrayOfObjects($json, ?string $message = '', $code = 403): void
43
    {
44 54
        if (!$this->isArrayOfObjects($json)) {
45 21
            $this->throw($message ?: Messages::MUST_BE_ARRAY_OF_OBJECTS, $code);
0 ignored issues
show
Bug introduced by
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 ignore-call  annotation

45
            $this->/** @scrutinizer ignore-call */ 
46
                   throw($message ?: Messages::MUST_BE_ARRAY_OF_OBJECTS, $code);
Loading history...
46
        }
47 33
    }
48
49
    /**
50
     * Check if an array is not an array of objects.
51
     *
52
     * @param array  $json
53
     *
54
     * @return bool
55
     */
56 186
    public function isNotArrayOfObjects($json): bool
57
    {
58 186
        $this->isValidArgument(1, 'array', $json);
59
60 180
        if (\count($json) == 0) {
61 3
            return true;
62
        }
63
64 177
        return $this->arrayIsAssociative($json);
65
    }
66
67
    /**
68
     * Validate that an array is not an array of objects.
69
     *
70
     * @param array  $json
71
     * @param string $message     An optional message to explain why the test failed
72
     * @param mixed  $code
73
     *
74
     * @return void
75
     * @throws \VGirol\JsonApiStructure\Exception\ValidationException
76
     */
77 174
    public function mustNotBeArrayOfObjects($json, string $message = '', $code = 403): void
78
    {
79 174
        if (!$this->isNotArrayOfObjects($json)) {
80 21
            $this->throw($message ?: Messages::MUST_NOT_BE_ARRAY_OF_OBJECTS, $code);
81
        }
82 150
    }
83
84
    /**
85
     * Checks if the given array is an associative array.
86
     *
87
     * @param array $arr
88
     *
89
     * @return boolean
90
     */
91 285
    private function arrayIsAssociative(array $arr): bool
92
    {
93 285
        return (\array_keys($arr) !== \range(0, \count($arr) - 1));
94
    }
95
}
96