Completed
Push — master ( c61969...df1bf5 )
by Marcus
02:35
created

StringVal::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Mbright\Validation\Rule\Validate;
4
5
class StringVal
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