ValueFormatter::format()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0
cc 6
eloc 16
nc 6
nop 1
1
<?php
2
namespace Workana\AsyncJobs\Formatter;
3
4
/**
5
 * @author Carlos Frutos <[email protected]>
6
 */
7
class ValueFormatter implements FormatterInterface
8
{
9
    /**
10
     * {@inheritdoc}
11
     */
12
    public function format($target)
13
    {
14
        if (is_null($target)) {
15
            return '(null)';
16
        } elseif (is_array($target)) {
17
            return '(Array) {...}';
18
        } elseif (is_scalar($target)) {
19
            $type = gettype($target);
20
21
            if (is_bool($target)) {
22
                $value = $target ? 'true' : 'false';
23
            } else {
24
                $value = (string) $target;
25
            }
26
27
            return strtr('(:type) :value', [
28
                ':type' => $type,
29
                ':value' => $value,
30
            ]);
31
        } else {
32
            return strtr('(:class) {...}', [':class' => get_class($target)]);
33
        }
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function canFormat($target)
40
    {
41
        /**
42
         * Can format always
43
         */
44
        return true;
45
    }
46
}