1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SKien\Formgenerator; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* |
8
|
|
|
* |
9
|
|
|
* @package Formgenerator |
10
|
|
|
* @author Stefanius <[email protected]> |
11
|
|
|
* @copyright MIT License - see the LICENSE file for details |
12
|
|
|
*/ |
13
|
|
|
class XMLForm extends FormGenerator |
14
|
|
|
{ |
15
|
|
|
const E_OK = 0; |
16
|
|
|
const E_FILE_NOT_EXIST = 1; |
17
|
|
|
const E_INVALID_FORM = 2; |
18
|
|
|
const E_INVALID_FORM_ELEMENT = 3; |
19
|
|
|
|
20
|
|
|
/** @var \DOMDocument the DOM Doc containing the form definition */ |
21
|
|
|
protected ?\DOMDocument $oXMLForm = null; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* no constructor - always use the constructor of the parent! |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Load form from the given XML file. |
29
|
|
|
* @param string $strXMLFile |
30
|
|
|
* @return int |
31
|
|
|
*/ |
32
|
|
|
public function loadXML(string $strXMLFile) : int |
33
|
|
|
{ |
34
|
|
|
$iResult = self::E_OK; |
35
|
|
|
if (!file_exists($strXMLFile)) { |
36
|
|
|
return self::E_FILE_NOT_EXIST; |
37
|
|
|
} |
38
|
|
|
$this->oXMLForm = new \DOMDocument(); |
39
|
|
|
if ($this->oXMLForm->load($strXMLFile)) { |
40
|
|
|
$oRoot = $this->oXMLForm->documentElement; |
41
|
|
|
if ($oRoot->nodeName != 'Form') { |
42
|
|
|
return self::E_INVALID_FORM; |
43
|
|
|
} |
44
|
|
|
// First we read some general infos for the form |
45
|
|
|
$this->readAdditionalXML($oRoot); |
46
|
|
|
|
47
|
|
|
// and iterate recursive through the child elements |
48
|
|
|
$this->createChildElements($oRoot, $this); |
49
|
|
|
} |
50
|
|
|
return $iResult; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Iterate through all childs of the given parent node and create the according element. |
55
|
|
|
* This method cals itself recursive for all collection elements. |
56
|
|
|
* @param \DOMElement $oXMLParent the DOM Element containing the infos for the formelement to create |
57
|
|
|
* @param FormCollection $oFormParent the parent element in the form |
58
|
|
|
* @return int |
59
|
|
|
*/ |
60
|
|
|
protected function createChildElements(\DOMElement $oXMLParent, FormCollection $oFormParent) : int |
61
|
|
|
{ |
62
|
|
|
$iResult = self::E_OK; |
63
|
|
|
if (!$oXMLParent->hasChildNodes()) { |
64
|
|
|
return $iResult; |
65
|
|
|
} |
66
|
|
|
foreach($oXMLParent->childNodes as $oXMLChild) { |
67
|
|
|
if (strtolower($oXMLChild->nodeName) == '#text') { |
68
|
|
|
continue; |
69
|
|
|
} |
70
|
|
|
$strClassname = __NAMESPACE__ . '\Form' . $oXMLChild->nodeName; |
71
|
|
|
if (class_exists($strClassname) && is_subclass_of($strClassname, __NAMESPACE__ . '\FormElement') ) { |
72
|
|
|
$oFormElement = $strClassname::fromXML($oXMLChild, $oFormParent); |
73
|
|
|
// recursive call for collection element (div, fieldset, line) |
74
|
|
|
if ($oFormElement instanceof FormCollection) { |
75
|
|
|
$iResult = $this->createChildElements($oXMLChild, $oFormElement); |
76
|
|
|
} |
77
|
|
|
} else { |
78
|
|
|
$iResult = self::E_INVALID_FORM_ELEMENT; |
79
|
|
|
} |
80
|
|
|
if ($iResult != self::E_OK) { |
81
|
|
|
break; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
return $iResult; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|