Passed
Push — master ( b78bc9...3bbd58 )
by Bruno
15:37
created

Renderable::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Formularium;
4
5
use Formularium\Exception\ClassNotFoundException;
6
use Formularium\Exception\Exception;
7
use Formularium\HTMLElement;
8
9
/**
10
 * Abstract base classe to render datatypes. This class should be extended by frontends
11
 * for each datatype.
12
 */
13
abstract class Renderable implements RenderableParameter
14
{
15
    /**
16
     * Factory.
17
     *
18
     * @param string|Datatype $datatype
19
     * @param Framework $framework
20
     * @return Renderable
21
     */
22 3
    public static function factory($datatype, Framework $framework): Renderable
23
    {
24 3
        if ($datatype instanceof Datatype) {
25 1
            $datatypeName = $datatype->getName();
26
        } else {
27 2
            $datatypeName = $datatype;
28 2
            $datatype = DatatypeFactory::factory($datatypeName);
29
        }
30
31 2
        $frameworkClassname = get_class($framework);
32 2
        $lastpos = strrpos($frameworkClassname, '\\');
33 2
        if ($lastpos === false) {
34
            $ns = '';
35
        } else {
36 2
            $ns = '\\' . substr($frameworkClassname, 0, $lastpos);
37
        }
38 2
        $class = "$ns\\Renderable\\Renderable_$datatypeName";
39 2
        if (!class_exists($class)) {
40
            $basetype = $datatype->getBasetype();
41
            $class = "$ns\\Renderable\\Renderable_$basetype";
42
        }
43 2
        if (!class_exists($class)) {
44
            throw new ClassNotFoundException("Invalid datatype '$datatypeName' for {$framework->getName()}");
45
        }
46 2
        return new $class($framework);
47
    }
48
49
    /**
50
     * @var Framework
51
     */
52
    protected $framework;
53
54
    public function __construct(Framework $framework)
55
    {
56
        $this->framework = $framework;
57
    }
58
    /**
59
     * Renders a view-only version of this renderable.
60
     *
61
     * @param mixed $value The value to render.
62
     * @param Field $field The field.
63
     * @param HTMLElement $previous The HTML coming from the previous composer.
64
     * @return HTMLElement The HTML rendered.
65
     */
66
    abstract public function viewable($value, Field $field, HTMLElement $previous): HTMLElement;
67
68
    /**
69
     * Renders a form editable version of this renderable
70
     *
71
     * @param mixed $value The value to render.
72
     * @param Field $field The field.
73
     * @param HTMLElement $previous The HTML coming from the previous composer.
74
     * @return HTMLElement The HTML rendered.
75
     */
76
    abstract public function editable($value, Field $field, HTMLElement $previous): HTMLElement;
77
}
78