Formattable::getFormatter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Reports\Core;
4
5
use AlecRabbit\Formatters\Contracts\FormatterInterface;
6
7
abstract class Formattable
8
{
9
    /** @var null|FormatterInterface */
10
    protected $formatter;
11
12 4
    public function __construct(FormatterInterface $formatter = null)
13
    {
14 4
        $this->formatter = $formatter;
15 4
    }
16
17
    /**
18
     * @return string
19
     */
20 3
    public function __toString(): string
21
    {
22 3
        if ($this->formatter instanceof FormatterInterface) {
23 3
            return $this->formatter->format($this);
24
        }
25 1
        return get_class($this) . ' ERROR: no formatter';
26
    }
27
28
    /**
29
     * @return null|FormatterInterface
30
     */
31 4
    public function getFormatter(): ?FormatterInterface
32
    {
33 4
        return $this->formatter;
34
    }
35
36
    /**
37
     * @param null|FormatterInterface $formatter
38
     * @return Formattable
39
     */
40 1
    public function setFormatter(?FormatterInterface $formatter): Formattable
41
    {
42 1
        $this->formatter = $formatter;
43 1
        return $this;
44
    }
45
}
46