1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* You may not change or alter any portion of this comment or credits |
4
|
|
|
* of supporting developers from this source code or any supporting source code |
5
|
|
|
* which is considered copyrighted (c) material of the original comment or credit authors. |
6
|
|
|
* This program is distributed in the hope that it will be useful, |
7
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
8
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Xoops\Form; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Factory to build form elements |
15
|
|
|
* |
16
|
|
|
* @category ElementFactory |
17
|
|
|
* @package Xoops\Form |
18
|
|
|
* @author Richard Griffith <[email protected]> |
19
|
|
|
* @copyright 2015 XOOPS Project (http://xoops.org) |
20
|
|
|
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html) |
21
|
|
|
* @link http://xoops.org |
22
|
|
|
*/ |
23
|
|
|
class ElementFactory |
24
|
|
|
{ |
25
|
|
|
const CLASS_KEY = ':class'; |
26
|
|
|
const FORM_KEY = ':form'; |
27
|
|
|
|
28
|
|
|
/** @var ContainerInterface */ |
29
|
|
|
protected $container = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Create the specified Form\Element |
33
|
|
|
* |
34
|
|
|
* @param array $specification associative array of attributes and controls defining an Element |
35
|
|
|
* |
36
|
|
|
* @return Element |
37
|
|
|
* |
38
|
|
|
* @throws \DomainException |
39
|
|
|
*/ |
40
|
5 |
|
public function create($specification) |
41
|
|
|
{ |
42
|
5 |
|
$this->validateSpec($specification); |
43
|
2 |
|
return new $specification[self::CLASS_KEY]($specification); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Validate the specification, adding container if needed |
48
|
|
|
* |
49
|
|
|
* @param array $specification associative array of attributes and controls defining an Element |
50
|
|
|
* |
51
|
|
|
* @return Element |
52
|
|
|
* |
53
|
|
|
* @throws \DomainException |
54
|
|
|
*/ |
55
|
5 |
|
protected function validateSpec(&$specification) |
56
|
|
|
{ |
57
|
5 |
|
if (!array_key_exists(self::CLASS_KEY, $specification)) { |
58
|
1 |
|
throw new \DomainException('Specification CLASS_KEY required.'); |
59
|
|
|
} |
60
|
4 |
|
$elementClass = $specification[self::CLASS_KEY]; |
61
|
4 |
|
if (false === strpos($elementClass, '\\')) { |
62
|
3 |
|
$elementClass = '\Xoops\Form\\' . $elementClass; |
63
|
|
|
} |
64
|
|
|
|
65
|
4 |
|
if (!class_exists($elementClass)) { |
66
|
1 |
|
throw new \DomainException('Unknown element class: ' . $specification[self::CLASS_KEY]); |
67
|
|
|
} |
68
|
|
|
|
69
|
3 |
|
if (!is_a($elementClass, '\Xoops\Form\Element', true)) { |
70
|
1 |
|
throw new \DomainException('Not an Element subclass: ' . $specification[self::CLASS_KEY]); |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
$specification[self::CLASS_KEY] = $elementClass; |
74
|
2 |
|
if (!(array_key_exists(self::FORM_KEY, $specification)) && isset($this->container)) { |
75
|
1 |
|
$specification[self::FORM_KEY] = $this->container; |
76
|
|
|
} |
77
|
2 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set a the container to be applied to new elements |
81
|
|
|
* |
82
|
|
|
* @param ContainerInterface $container form or tray to contain generated elements |
83
|
|
|
* |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
1 |
|
public function setContainer(ContainerInterface $container) |
87
|
|
|
{ |
88
|
1 |
|
$this->container = $container; |
89
|
1 |
|
} |
90
|
|
|
} |
91
|
|
|
|