Skip to content

Commit

Permalink
Merge pull request #19 from Icinga/feature/use-stdlib-eventemitter
Browse files Browse the repository at this point in the history
Form: use ipl\Stdlib\EventEmitter
  • Loading branch information
Thomas-Gelf authored May 16, 2019
2 parents dfab35c + 4f153ea commit 60e9fb5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
8 changes: 1 addition & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,9 @@
"config": {
"sort-packages": true
},
"repositories": [{
"type": "vcs",
"url": "https://github.com/Icinga/ipl-stdlib",
"no-api": true
}],
"require": {
"php": ">=5.4.0",
"evenement/evenement": "^2",
"ipl/stdlib": "^0.1",
"ipl/stdlib": "dev-feature/event-emitter || ^0.2",
"psr/http-message": "~1.0"
},
"autoload": {
Expand Down
8 changes: 8 additions & 0 deletions src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

class Form extends BaseHtmlElement
{
const ON_ELEMENT_REGISTERED = 'elementRegistered';
const ON_ERROR = 'error';
const ON_REQUEST = 'request';
const ON_SUCCESS = 'success';

use FormElementContainer;
use MessageContainer;

Expand All @@ -30,6 +35,7 @@ class Form extends BaseHtmlElement
public function setRequest($request)
{
$this->request = $request;
$this->emit(Form::ON_REQUEST, [$request]);

return $this;
}
Expand Down Expand Up @@ -57,9 +63,11 @@ public function handleRequest(ServerRequestInterface $request)
if ($this->isValid()) {
try {
$this->onSuccess();
$this->emitOnce(Form::ON_SUCCESS, [$this]);
} catch (Exception $e) {
$this->addMessage($e);
$this->onError();
$this->emit(Form::ON_ERROR, [$e, $this]);
}
} else {
$this->onError();
Expand Down
25 changes: 19 additions & 6 deletions src/FormElement/FormElementContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
use ipl\Html\Form;
use ipl\Html\FormDecorator\DecoratorInterface;
use ipl\Html\ValidHtml;
use ipl\Stdlib\EventEmitter;
use ipl\Stdlib\Loader\PluginLoader;

trait FormElementContainer
{
use PluginLoader;
use EventEmitter;

/** @var BaseFormElement[] */
private $elements = [];
Expand Down Expand Up @@ -119,7 +121,7 @@ protected function eventuallyRegisterDefaultDecoratorLoader()
*/
public function getElement($name)
{
if (! array_key_exists($name, $this->elements)) {
if (! \array_key_exists($name, $this->elements)) {
throw new InvalidArgumentException(sprintf(
'Trying to get non-existent element "%s"',
$name
Expand All @@ -134,10 +136,10 @@ public function getElement($name)
*/
public function hasElement($element)
{
if (is_string($element)) {
return array_key_exists($element, $this->elements);
if (\is_string($element)) {
return \array_key_exists($element, $this->elements);
} elseif ($element instanceof BaseFormElement) {
return in_array($element, $this->elements, true);
return \in_array($element, $this->elements, true);
} else {
return false;
}
Expand Down Expand Up @@ -194,7 +196,7 @@ protected function decorate(BaseFormElement $element)
*/
public function registerElement($type, $name = null, $options = null)
{
if (is_string($type)) {
if (\is_string($type)) {
$type = $this->createElement($type, $name, $options);
// TODO: } elseif ($type instanceof FormElementInterface) {
} elseif ($type instanceof BaseHtmlElement) {
Expand All @@ -210,6 +212,7 @@ public function registerElement($type, $name = null, $options = null)
$this->elements[$name] = $type;

$this->onElementRegistered($name, $type);
$this->emit(Form::ON_ELEMENT_REGISTERED, [$name, $type]);

return $this;
}
Expand All @@ -221,7 +224,7 @@ public function onElementRegistered($name, BaseFormElement $element)
$this->setSubmitButton($element);
}

if (array_key_exists($name, $this->populatedValues)) {
if (\array_key_exists($name, $this->populatedValues)) {
$element->setValue($this->populatedValues[$name]);
}
}
Expand Down Expand Up @@ -307,4 +310,14 @@ public function getDefaultElementDecorator()
{
return $this->defaultElementDecorator;
}

public function isValidEvent($event)
{
return \in_array($event, [
Form::ON_SUCCESS,
Form::ON_ERROR,
Form::ON_REQUEST,
Form::ON_ELEMENT_REGISTERED,
]);
}
}

0 comments on commit 60e9fb5

Please sign in to comment.