Skip to content

Commit 4b6d33a

Browse files
authored
Merge pull request #23 from ray-di/to_string
__toString exception handling
2 parents 37ba08e + 9e5bb67 commit 4b6d33a

File tree

6 files changed

+67
-9
lines changed

6 files changed

+67
-9
lines changed

src/AbstractForm.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Aura\Input\BuilderInterface;
1515
use Aura\Input\Fieldset;
1616
use Ray\WebFormModule\Exception\CsrfViolationException;
17+
use Ray\WebFormModule\Exception\LogicException;
1718

1819
abstract class AbstractForm extends Fieldset implements FormInterface
1920
{
@@ -47,6 +48,26 @@ public function __clone()
4748
$this->init();
4849
}
4950

51+
/**
52+
* Return form markup string
53+
*
54+
* @return string
55+
*/
56+
public function __toString()
57+
{
58+
try {
59+
if (! $this instanceof ToStringInterface) {
60+
throw new LogicException(ToStringInterface::class . ' is not implemented');
61+
}
62+
63+
return $this->toString();
64+
} catch (\Exception $e) {
65+
trigger_error($e->getMessage() . PHP_EOL . $e->getTraceAsString(), E_USER_ERROR);
66+
67+
return '';
68+
}
69+
}
70+
5071
/**
5172
* @param BuilderInterface $builder
5273
* @param FilterFactory $filterFactory
@@ -140,9 +161,8 @@ public function apply(array $data)
140161
throw new CsrfViolationException;
141162
}
142163
$this->fill($data);
143-
$isValid = $this->filter->apply($data);
144164

145-
return $isValid;
165+
return $this->filter->apply($data);
146166
}
147167

148168
/**
@@ -152,9 +172,7 @@ public function apply(array $data)
152172
*/
153173
public function getFailureMessages()
154174
{
155-
$messages = $this->filter->getFailures()->getMessages();
156-
157-
return $messages;
175+
return $this->filter->getFailures()->getMessages();
158176
}
159177

160178
/**

src/ToStringInterface.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* This file is part of the Ray.WebFormModule package.
4+
*
5+
* @license http://opensource.org/licenses/MIT MIT
6+
*/
7+
namespace Ray\WebFormModule;
8+
9+
/**
10+
* Return form markup string
11+
*/
12+
interface ToStringInterface
13+
{
14+
public function toString() : string;
15+
}

src/VndErrorHandler.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ public function handle(AbstractValidation $formValidation, MethodInvocation $inv
3232
unset($formValidation);
3333
$vndError = $this->reader->getMethodAnnotation($invocation->getMethod(), VndError::class);
3434
$error = new FormValidationError($this->makeVndError($form, $vndError));
35-
$e = new ValidationException('Validation failed.', 400, null, $error);
3635

37-
throw $e;
36+
throw new ValidationException('Validation failed.', 400, null, $error);
3837
}
3938

4039
private function makeVndError(AbstractForm $form, VndError $vndError = null)

tests/AbstractAuraFormTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,17 @@ public function tesetInputDataReamainedOnValidationFailure($html)
6060
$expected = '<input id="name" type="text" name="name" value="@invalid@" />';
6161
$this->assertContains($expected, $html);
6262
}
63+
64+
public function testNotToStringImplemented()
65+
{
66+
$errNo = $errStr = '';
67+
set_error_handler(function (int $no, string $str) use (&$errNo, &$errStr) {
68+
$errNo = $no;
69+
$errStr = $str;
70+
});
71+
$form = new FakeErrorForm;
72+
(string) $form;
73+
$this->assertSame(256, $errNo);
74+
restore_error_handler();
75+
}
6376
}

tests/Fake/FakeErrorForm.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Ray\WebFormModule;
4+
5+
class FakeErrorForm extends AbstractForm
6+
{
7+
/**
8+
* {@inheritdoc}
9+
*/
10+
public function init()
11+
{
12+
}
13+
}

tests/Fake/FakeNameForm.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Aura\Html\Helper\Tag;
66

7-
class FakeNameForm extends AbstractForm
7+
class FakeNameForm extends AbstractForm implements ToStringInterface
88
{
99
/**
1010
* {@inheritdoc}
@@ -41,7 +41,7 @@ public function submit()
4141
/**
4242
* {@inheritdoc}
4343
*/
44-
public function __toString()
44+
public function toString() : string
4545
{
4646
$form = $this->form([
4747
'method' => 'post',

0 commit comments

Comments
 (0)