diff --git a/composer.json b/composer.json index e4765127..3cdaaa67 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/src/Form.php b/src/Form.php index 301ec163..c967d90e 100644 --- a/src/Form.php +++ b/src/Form.php @@ -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; @@ -30,6 +35,7 @@ class Form extends BaseHtmlElement public function setRequest($request) { $this->request = $request; + $this->emit(Form::ON_REQUEST, [$request]); return $this; } @@ -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(); diff --git a/src/FormElement/FormElementContainer.php b/src/FormElement/FormElementContainer.php index 5da2e8d1..a584e761 100644 --- a/src/FormElement/FormElementContainer.php +++ b/src/FormElement/FormElementContainer.php @@ -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 = []; @@ -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 @@ -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; } @@ -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) { @@ -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; } @@ -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]); } } @@ -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, + ]); + } }