|
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
|
|
|
|