1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the WPSymfonyForm plugin. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2015-2016 LIN3S <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace LIN3S\WPSymfonyForm\Registry; |
13
|
|
|
|
14
|
|
|
use LIN3S\WPSymfonyForm\Wrapper\FormWrapper; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* FormWrapperRegistry class. |
18
|
|
|
* |
19
|
|
|
* @author Gorka Laucirica <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class FormWrapperRegistry |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Collection of form wrappers. |
25
|
|
|
* |
26
|
|
|
* @var FormWrapper[] |
27
|
|
|
*/ |
28
|
|
|
private $formWrappers; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Initializes the registry with an array of wrappers. |
32
|
|
|
* |
33
|
|
|
* @param FormWrapper[] $formWrappers |
34
|
|
|
* |
35
|
|
|
* @throws \InvalidArgumentException |
36
|
|
|
*/ |
37
|
|
|
public function __construct($formWrappers = []) |
38
|
|
|
{ |
39
|
|
|
if (is_array($formWrappers)) { |
40
|
|
|
foreach ($formWrappers as $wrapper) { |
41
|
|
|
if (!$wrapper instanceof FormWrapper) { |
42
|
|
|
throw new \InvalidArgumentException( |
43
|
|
|
sprintf( |
44
|
|
|
'FormWrapperRegistry requires an array of FormWrapper objects, %s given', |
45
|
|
|
get_class($wrapper) |
46
|
|
|
) |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
$this->formWrappers = $formWrappers; |
51
|
|
|
} else { |
52
|
|
|
throw new \InvalidArgumentException( |
53
|
|
|
sprintf( |
54
|
|
|
'FormWrapperRegistry requires an array of FormWrapper objects, %s given', |
55
|
|
|
get_class($formWrappers) |
56
|
|
|
) |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Gets a form wrapper by form name. |
63
|
|
|
* |
64
|
|
|
* @param string $formName The form name |
65
|
|
|
* |
66
|
|
|
* @throw \InvalidArgumentException if wrapper not found |
67
|
|
|
* |
68
|
|
|
* @return \LIN3S\WPSymfonyForm\Wrapper\FormWrapper |
69
|
|
|
*/ |
70
|
|
|
public function get($formName) |
71
|
|
|
{ |
72
|
|
|
foreach ($this->formWrappers as $wrapper) { |
73
|
|
|
if ($wrapper->getForm()->getName() === $formName) { |
74
|
|
|
return $wrapper; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
throw new \InvalidArgumentException( |
78
|
|
|
sprintf( |
79
|
|
|
'Form with name %s not found in FormWrapperRegistry', |
80
|
|
|
$formName |
81
|
|
|
) |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Adds a form wrapper to the registry. |
87
|
|
|
* |
88
|
|
|
* @param FormWrapper $formWrapper The form wrapper |
89
|
|
|
*/ |
90
|
|
|
public function add(FormWrapper $formWrapper) |
91
|
|
|
{ |
92
|
|
|
$this->formWrappers[] = $formWrapper; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|