Completed
Push — master ( 6f39c4...0208de )
by Vincent
02:02 queued 11s
created

ValidateArrays::mustBeArrayOfObjects()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 2
c 0
b 0
f 0
nc 2
nop 3
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 12
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
    public function isArrayOfObjects($json): bool
22
    {
23
        $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
            return true;
27 180
        }
28
29 177
        return !$this->arrayIsAssociative($json);
30 3
    }
31
32
    /**
33 174
     * Validate that an array is an array of objects.
34
     *
35 174
     * @param array       $json
36 21
     * @param string|null $message     An optional message to explain why the test failed
37
     * @param mixed       $code
38
     *
39 156
     * @return void
40
     * @throws \VGirol\JsonApiStructure\Exception\ValidationException
41
     */
42
    public function mustBeArrayOfObjects($json, ?string $message = '', $code = 403): void
43
    {
44
        if (!$this->isArrayOfObjects($json)) {
45
            $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
    }
48
49
    /**
50
     * Check if an array is not an array of objects.
51
     *
52
     * @param array  $json
53 180
     *
54
     * @return bool
55 180
     */
56
    public function isNotArrayOfObjects($json): bool
57 174
    {
58 3
        $this->isValidArgument(1, 'array', $json);
59
60
        if (\count($json) == 0) {
61 171
            return true;
62
        }
63 171
64 18
        return $this->arrayIsAssociative($json);
65
    }
66
67 153
    /**
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 276
    public function mustNotBeArrayOfObjects($json, string $message = '', $code = 403): void
78
    {
79 276
        if (!$this->isNotArrayOfObjects($json)) {
80
            $this->throw($message ?: Messages::MUST_NOT_BE_ARRAY_OF_OBJECTS, $code);
81
        }
82
    }
83
84
    /**
85
     * Checks if the given array is an associative array.
86
     *
87
     * @param array $arr
88
     *
89
     * @return boolean
90
     */
91
    private function arrayIsAssociative(array $arr): bool
92
    {
93
        return (\array_keys($arr) !== \range(0, \count($arr) - 1));
94
    }
95
}
96