1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of slick/form package |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Slick\Form; |
11
|
|
|
|
12
|
|
|
use League\Event\Emitter; |
13
|
|
|
use League\Event\EmitterInterface; |
14
|
|
|
use Slick\Filter\StaticFilter; |
15
|
|
|
use Slick\Form\Parser\ParserInterface; |
16
|
|
|
use Slick\Form\Parser\PhpParser; |
17
|
|
|
use Slick\Form\Parser\YamlParser; |
18
|
|
|
use Slick\Validator\StaticValidator; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Form Registry and factory class |
22
|
|
|
* |
23
|
|
|
* @package Slick\Form |
24
|
|
|
* @author Filipe Silva <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
final class FormRegistry extends Emitter implements EmitterInterface |
27
|
|
|
{ |
28
|
|
|
const PARSER_YAML = 'yaml'; |
29
|
|
|
const PARSER_PHP = 'php'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string parser type |
33
|
|
|
*/ |
34
|
|
|
private $parser; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var FormRegistry |
38
|
|
|
*/ |
39
|
|
|
private static $instance; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* FormRegistry constructor, singleton pattern |
43
|
|
|
*/ |
44
|
2 |
|
private function __construct() |
45
|
|
|
{ |
46
|
2 |
|
StaticValidator::$validators['requiredUpload'] = |
47
|
|
|
'Slick\Form\Input\Validator\RequiredUpload'; |
48
|
2 |
|
StaticValidator::$validators['validUpload'] = |
49
|
|
|
'Slick\Form\Input\Validator\ValidUpload'; |
50
|
2 |
|
StaticFilter::$filters['integer'] = |
51
|
|
|
'Slick\Form\Input\Filter\Integer'; |
52
|
2 |
|
StaticFilter::$filters['boolean'] = |
53
|
|
|
'Slick\Form\Input\Filter\Boolean'; |
54
|
|
|
|
55
|
2 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var array List of known parser types |
59
|
|
|
*/ |
60
|
|
|
private static $knownParsers = [ |
61
|
|
|
self::PARSER_YAML => YamlParser::class, |
62
|
|
|
self::PARSER_PHP => PhpParser::class |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Generates a for for the provided definitions file |
67
|
|
|
* |
68
|
|
|
* @param string $defFile The path to the form definition file |
69
|
|
|
* @param string $type Parser type |
70
|
|
|
* |
71
|
|
|
* @return FormInterface |
72
|
|
|
*/ |
73
|
4 |
|
public function get($defFile, $type = self::PARSER_YAML) |
74
|
|
|
{ |
75
|
4 |
|
$this->parser = $type; |
76
|
4 |
|
$parserClass = self::$knownParsers[$this->parser]; |
77
|
|
|
/** @var ParserInterface $parser */ |
78
|
4 |
|
$parser = new $parserClass($defFile); |
79
|
4 |
|
$form = $parser->getForm(); |
80
|
4 |
|
$this->emit('form.created', $form); |
81
|
4 |
|
return $form; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Returns a self instance |
86
|
|
|
* |
87
|
|
|
* @return FormRegistry|static |
88
|
|
|
*/ |
89
|
4 |
|
public static function getInstance() |
90
|
|
|
{ |
91
|
4 |
|
if (null === self::$instance) { |
92
|
2 |
|
self::$instance = new static; |
93
|
2 |
|
} |
94
|
4 |
|
return self::$instance; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Generates a for for the provided definitions file |
99
|
|
|
* |
100
|
|
|
* @param string $defFile The path to the form definition file |
101
|
|
|
* @param string $type Parser type |
102
|
|
|
* |
103
|
|
|
* @return FormInterface |
104
|
|
|
*/ |
105
|
2 |
|
public static function getForm($defFile, $type = self::PARSER_YAML) |
106
|
|
|
{ |
107
|
2 |
|
return self::getInstance()->get($defFile, $type); |
108
|
|
|
} |
109
|
|
|
} |