InKeys   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 0
dl 0
loc 28
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 11 3
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/bsd-license.php BSD
7
 *
8
 */
9
namespace Aura\Filter\Rule\Validate;
10
11
/**
12
 *
13
 * Validates that the value is a key in a given array.
14
 *
15
 * @package Aura.Filter
16
 *
17
 */
18
class InKeys
19
{
20
    /**
21
     *
22
     * Validates that the value is a key in a given array.
23
     *
24
     * @param object $subject The subject to be filtered.
25
     *
26
     * @param string $field The subject field name.
27
     *
28
     * @param array $array An array of key-value pairs; the value must match
29
     * one of the keys in this array.
30
     *
31
     * @return bool True if valid, false if not.
32
     *
33
     */
34 14
    public function __invoke($subject, $field, array $array)
35
    {
36 14
        $value = $subject->$field;
37 14
        if (! is_string($value) && ! is_int($value)) {
38
            // array_key_exists errors on non-string non-int keys.
39 3
            return false;
40
        }
41
        // using array_keys() converts string numeric keys to integers, which
42
        // is *not* the behavior we want.
43 11
        return array_key_exists($subject->$field, $array);
44
    }
45
}
46