Skip to content

Commit 92d46a8

Browse files
authored
Merge pull request #312 from kynx/hotfix/form-element-manager
Hotfix/form element manager
2 parents 3a77700 + 75c9d2b commit 92d46a8

File tree

3 files changed

+287
-1
lines changed

3 files changed

+287
-1
lines changed

src/FormElementManager.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use function array_unshift;
1919
use function class_exists;
2020
use function get_debug_type;
21+
use function method_exists;
2122
use function sprintf;
2223

2324
/**
@@ -213,7 +214,11 @@ class FormElementManager extends AbstractPluginManager
213214
*/
214215
public function injectFactory(ContainerInterface $container, mixed $instance): void
215216
{
216-
if (! $instance instanceof Fieldset) {
217+
if (! $instance instanceof FieldsetInterface) {
218+
return;
219+
}
220+
221+
if (! method_exists($instance, 'getFormFactory')) {
217222
return;
218223
}
219224

test/FormElementManagerTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
use Laminas\Form\Exception\DomainException;
1010
use Laminas\Form\Exception\InvalidElementException;
1111
use Laminas\Form\Factory;
12+
use Laminas\Form\FieldsetInterface;
1213
use Laminas\Form\Form;
1314
use Laminas\Form\FormElementManager;
1415
use Laminas\Hydrator\HydratorInterface;
1516
use Laminas\Hydrator\HydratorPluginManager;
1617
use Laminas\ServiceManager\Exception\InvalidServiceException;
1718
use Laminas\ServiceManager\PluginManagerInterface;
1819
use Laminas\ServiceManager\ServiceManager;
20+
use LaminasTest\Form\TestAsset\FieldsetInterfaceImplementation;
1921
use LaminasTest\Form\TestAsset\InvokableForm;
2022
use PHPUnit\Framework\Attributes\Group;
2123
use PHPUnit\Framework\MockObject\MockObject;
@@ -62,6 +64,20 @@ public function testInjectsFormElementManagerToFormComposedByFormFactoryAwareEle
6264
self::assertSame($this->manager, $form->getFormFactory()->getFormElementManager());
6365
}
6466

67+
public function testInjectFormElementManagerToCustomFieldset(): void
68+
{
69+
$factory = new Factory();
70+
$this->manager->setFactory('my-fieldset', static function ($elements) use ($factory): FieldsetInterface {
71+
$fieldset = new FieldsetInterfaceImplementation();
72+
$fieldset->setFormFactory($factory);
73+
return $fieldset;
74+
});
75+
$fieldset = $this->manager->get('my-fieldset');
76+
assert($fieldset instanceof FieldsetInterfaceImplementation);
77+
self::assertSame($factory, $fieldset->getFormFactory());
78+
self::assertSame($this->manager, $fieldset->getFormFactory()->getFormElementManager());
79+
}
80+
6581
public function testRegisteringInvalidElementRaisesException(): void
6682
{
6783
$this->expectException($this->getInvalidServiceException());
Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace LaminasTest\Form\TestAsset;
6+
7+
use ArrayIterator;
8+
use Laminas\Form\Element;
9+
use Laminas\Form\ElementInterface;
10+
use Laminas\Form\Factory;
11+
use Laminas\Form\FieldsetInterface;
12+
use Laminas\Form\FormInterface;
13+
use Laminas\Hydrator\HydratorInterface;
14+
use Traversable;
15+
16+
final class FieldsetInterfaceImplementation implements FieldsetInterface
17+
{
18+
private Factory $formFactory;
19+
20+
public function __construct()
21+
{
22+
$this->formFactory = new Factory();
23+
}
24+
25+
public function getIterator(): Traversable
26+
{
27+
return new ArrayIterator([]);
28+
}
29+
30+
public function count(): int
31+
{
32+
return 0;
33+
}
34+
35+
/**
36+
* @inheritDoc
37+
*/
38+
public function setName(string $name)
39+
{
40+
return $this;
41+
}
42+
43+
public function getName(): ?string
44+
{
45+
return null;
46+
}
47+
48+
/**
49+
* @inheritDoc
50+
*/
51+
public function setOptions(iterable $options)
52+
{
53+
return $this;
54+
}
55+
56+
/**
57+
* @inheritDoc
58+
*/
59+
public function setOption(string $key, mixed $value)
60+
{
61+
return $this;
62+
}
63+
64+
public function getOptions(): array
65+
{
66+
return [];
67+
}
68+
69+
/**
70+
* @inheritDoc
71+
*/
72+
public function getOption(string $option)
73+
{
74+
return null;
75+
}
76+
77+
/**
78+
* @inheritDoc
79+
*/
80+
public function setAttribute(string $key, mixed $value)
81+
{
82+
return $this;
83+
}
84+
85+
/**
86+
* @inheritDoc
87+
*/
88+
public function getAttribute(string $key)
89+
{
90+
return null;
91+
}
92+
93+
public function hasAttribute(string $key): bool
94+
{
95+
return false;
96+
}
97+
98+
/**
99+
* @inheritDoc
100+
*/
101+
public function setAttributes(iterable $arrayOrTraversable)
102+
{
103+
return $this;
104+
}
105+
106+
public function getAttributes(): array
107+
{
108+
return [];
109+
}
110+
111+
/**
112+
* @inheritDoc
113+
*/
114+
public function setValue(mixed $value)
115+
{
116+
return $this;
117+
}
118+
119+
/**
120+
* @inheritDoc
121+
*/
122+
public function getValue()
123+
{
124+
return null;
125+
}
126+
127+
/**
128+
* @inheritDoc
129+
*/
130+
public function setLabel(?string $label)
131+
{
132+
return $this;
133+
}
134+
135+
public function getLabel(): ?string
136+
{
137+
return null;
138+
}
139+
140+
/**
141+
* @inheritDoc
142+
*/
143+
public function setMessages(iterable $messages)
144+
{
145+
return $this;
146+
}
147+
148+
public function getMessages(): array
149+
{
150+
return [];
151+
}
152+
153+
public function prepareElement(FormInterface $form): void
154+
{
155+
}
156+
157+
/**
158+
* @inheritDoc
159+
*/
160+
public function add($elementOrFieldset, array $flags = [])
161+
{
162+
return $this;
163+
}
164+
165+
public function has(string $elementOrFieldset): bool
166+
{
167+
return false;
168+
}
169+
170+
public function get(string $elementOrFieldset): ElementInterface
171+
{
172+
return new Element();
173+
}
174+
175+
/**
176+
* @inheritDoc
177+
*/
178+
public function remove(string $elementOrFieldset)
179+
{
180+
return $this;
181+
}
182+
183+
/**
184+
* @inheritDoc
185+
*/
186+
public function setPriority(string $elementOrFieldset, int $priority)
187+
{
188+
return $this;
189+
}
190+
191+
public function getElements(): array
192+
{
193+
return [];
194+
}
195+
196+
public function getFieldsets(): array
197+
{
198+
return [];
199+
}
200+
201+
public function populateValues(iterable $data): void
202+
{
203+
}
204+
205+
/**
206+
* @inheritDoc
207+
*/
208+
public function setObject($object)
209+
{
210+
return $this;
211+
}
212+
213+
/**
214+
* @inheritDoc
215+
*/
216+
public function getObject()
217+
{
218+
return null;
219+
}
220+
221+
public function allowObjectBinding(object $object): bool
222+
{
223+
return false;
224+
}
225+
226+
/**
227+
* @inheritDoc
228+
*/
229+
public function setHydrator(HydratorInterface $hydrator)
230+
{
231+
return $this;
232+
}
233+
234+
public function getHydrator(): ?HydratorInterface
235+
{
236+
return null;
237+
}
238+
239+
/**
240+
* @inheritDoc
241+
*/
242+
public function bindValues(array $values = [])
243+
{
244+
return null;
245+
}
246+
247+
public function allowValueBinding(): bool
248+
{
249+
return false;
250+
}
251+
252+
/**
253+
* @inheritDoc
254+
*/
255+
public function setFormFactory(Factory $formFactory)
256+
{
257+
$this->formFactory = $formFactory;
258+
return $this;
259+
}
260+
261+
public function getFormFactory(): Factory
262+
{
263+
return $this->formFactory;
264+
}
265+
}

0 commit comments

Comments
 (0)