StringVal::__invoke()   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 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Mbright\Validation\Rule\Validate;
4
5
class StringVal implements ValidateRuleInterface
6
{
7
    /**
8
     * Validates that the value can be represented as a string.
9
     *
10
     * Essentially, this means any scalar value is valid (no arrays, objects, resources, etc). If the $subject is an
11
     * object and has a __tostring() method, this will validate successfully.
12
     *
13
     * @param object $subject The subject to be filtered.
14
     * @param string $field The subject field name.
15
     *
16
     * @return bool True if valid, false if not.
17
     */
18 24
    public function __invoke($subject, string $field): bool
19
    {
20 24
        $value = $subject->$field;
21
22 24
        if (is_object($value)) {
23 3
            return method_exists($value, '__toString');
24
        }
25
26 21
        return is_scalar($value);
27
    }
28
}
29