Completed
Push — master ( a7afc7...f7865d )
by Karsten
08:02
created

ToString::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Created by gerk on 20.11.17 17:34
4
 */
5
6
namespace PeekAndPoke\Component\Psi\Psi\Map;
7
8
use PeekAndPoke\Component\Psi\UnaryFunction;
9
10
/**
11
 *
12
 *
13
 * @author Karsten J. Gerber <[email protected]>
14
 */
15
class ToString implements UnaryFunction
16
{
17
    /** @var string */
18
    private $default;
19
20
    /**
21
     * @param string $default
22
     */
23 32
    public function __construct($default = '')
24
    {
25 32
        $this->default = (string) $default;
26 32
    }
27
28
    /**
29
     * @param mixed $input
30
     *
31
     * @return string
32
     */
33 34
    public function __invoke($input)
34
    {
35 34
        if (is_scalar($input)) {
36 21
            return (string) $input;
37
        }
38
39 13
        if (is_object($input) && method_exists($input, '__toString')) {
40 3
            return (string) $input;
41
        }
42
43 10
        return $this->default;
44
    }
45
}
46