Completed
Push — master ( 25512e...e66e4c )
by Matt
02:57
created

src/Constraint/DraftFour/AdditionalProperties.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace League\JsonGuard\Constraint\DraftFour;
4
5
use League\JsonGuard;
6
use League\JsonGuard\Assert;
7
use League\JsonGuard\ConstraintInterface;
8
use League\JsonGuard\Validator;
9
use function League\JsonGuard\error;
10
use function League\JsonGuard\pointer_push;
11
12
final class AdditionalProperties implements ConstraintInterface
13
{
14
    const KEYWORD = 'additionalProperties';
15
16
    /**
17
     * {@inheritdoc}
18
     */
19 16
    public function validate($value, $parameter, Validator $validator)
20
    {
21 16
        Assert::type($parameter, ['object', 'boolean'], self::KEYWORD, $validator->getSchemaPath());
22
23 14
        if (!is_object($value)) {
24 4
            return null;
25
        }
26
27 14
        $diff = self::getDiff($value, $validator->getSchema());
28
29 14
        if (count($diff) === 0) {
30 10
            return null;
31
        }
32
33 12
        if ($parameter === false) {
34 8
            return error('The object must not contain additional properties ({cause}).', $validator)
35 8
                ->withCause($diff);
36 6
        } elseif (is_object($parameter)) {
37
            // If additionalProperties is an object it's a schema,
38
            // so validate all additional properties against it.
39 6
            $errors = [];
40 6 View Code Duplication
            foreach ($diff as $property) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41 6
                $subValidator = $validator->makeSubSchemaValidator(
42 6
                    $value->$property,
43 3
                    $parameter,
44 6
                    pointer_push($validator->getDataPath(), $property)
45 3
                );
46 6
                $errors = array_merge($errors, $subValidator->errors());
47 3
            }
48
49 6
            return $errors;
50
        }
51
    }
52
53
    /**
54
     * Get the properties in $value which are not in $schema 'properties' or matching 'patternProperties'.
55
     *
56
     * @param object $value
57
     * @param object $schema
58
     *
59
     * @return array
60
     */
61 14
    private static function getDiff($value, $schema)
62
    {
63 14
        if (property_exists($schema, Properties::KEYWORD)) {
64 10
            $definedProperties = array_keys(get_object_vars($schema->properties));
65 5
        } else {
66 6
            $definedProperties = [];
67
        }
68
69 14
        $actualProperties = array_keys(get_object_vars($value));
70 14
        $diff             = array_diff($actualProperties, $definedProperties);
71
72
        // The diff doesn't account for patternProperties, so lets filter those out too.
73 14
        if (property_exists($schema, PatternProperties::KEYWORD)) {
74 4
            foreach ($schema->patternProperties as $property => $schema) {
75 4
                $matches = JsonGuard\properties_matching_pattern($property, $diff);
76 4
                $diff    = array_diff($diff, $matches);
77 2
            }
78
79 4
            return $diff;
80
        }
81
82 12
        return $diff;
83
    }
84
}
85