diff --git a/phalcon/Factory/AbstractFactory.zep b/phalcon/Factory/AbstractFactory.zep index 8745d24ed02..9e57cbca847 100644 --- a/phalcon/Factory/AbstractFactory.zep +++ b/phalcon/Factory/AbstractFactory.zep @@ -44,7 +44,7 @@ abstract class AbstractFactory extends AbstractConfigFactory } /** - * AdapterFactory constructor. + * Initialize services/add new services */ protected function init(array! services = []) -> void { diff --git a/phalcon/Forms/Element/AbstractElement.zep b/phalcon/Forms/Element/AbstractElement.zep index 8fba9b11c17..698569486a2 100644 --- a/phalcon/Forms/Element/AbstractElement.zep +++ b/phalcon/Forms/Element/AbstractElement.zep @@ -11,12 +11,12 @@ namespace Phalcon\Forms\Element; use InvalidArgumentException; +use Phalcon\Filter\Validation\ValidatorInterface; use Phalcon\Forms\Form; use Phalcon\Forms\Exception; +use Phalcon\Html\TagFactory; use Phalcon\Messages\MessageInterface; use Phalcon\Messages\Messages; -use Phalcon\Tag; -use Phalcon\Filter\Validation\ValidatorInterface; /** * This is a base class for form elements @@ -43,6 +43,11 @@ abstract class AbstractElement implements ElementInterface */ protected label = null; + /** + * @var string + */ + protected method = "inputText"; + /** * @var Messages */ @@ -58,6 +63,11 @@ abstract class AbstractElement implements ElementInterface */ protected options = []; + /** + * @var TagFactory|null + */ + protected tagFactory = null; + /** * @var array */ @@ -84,9 +94,9 @@ abstract class AbstractElement implements ElementInterface ); } - let this->name = name; - let this->attributes = attributes; - let this->messages = new Messages(); + let this->name = name, + this->attributes = attributes, + this->messages = new Messages(); } /** @@ -133,18 +143,19 @@ abstract class AbstractElement implements ElementInterface * Adds a group of validators * * @param \Phalcon\Filter\Validation\ValidatorInterface[] validators - * @param bool merge + * @param bool merge */ public function addValidators(array! validators, bool merge = true) -> { - if merge { - let validators = array_merge( - this->validators, - validators - ); + var validator; + + if unlikely !merge { + let this->validators = []; } - let this->validators = validators; + for validator in validators { + this->addValidator(validator); + } return this; } @@ -165,13 +176,10 @@ abstract class AbstractElement implements ElementInterface public function clear() -> { var form = this->form, - name = this->name, - value = this->value; + name = this->name; if typeof form == "object" { form->clear(name); - } else { - Tag::setDefault(name, value); } return this; @@ -303,15 +311,7 @@ abstract class AbstractElement implements ElementInterface } /** - * Otherwise check Phalcon\Tag - */ - if Tag::hasValue(name) { - let value = Tag::getValue(name); - } - - /** - * Assign the default value if there is no form available or - * Phalcon\Tag returns null + * Assign the default value if there is no form available */ if value === null { let value = this->value; @@ -333,12 +333,13 @@ abstract class AbstractElement implements ElementInterface */ public function label(array attributes = []) -> string { - var internalAttributes, label, name, code; + var code, internalAttributes, labelName, name, tagFactory; /** * Check if there is an "id" attribute defined */ - let internalAttributes = this->getAttributes(); + let tagFactory = this->getTagFactory(), + internalAttributes = this->attributes; if !fetch name, internalAttributes["id"] { let name = this->name; @@ -348,25 +349,23 @@ abstract class AbstractElement implements ElementInterface let attributes["for"] = name; } - let code = Tag::renderAttributes("label; + let labelName = this->label; - if label || is_numeric(label) { - let code .= ">" . label . ""; - } else { - let code .= ">" . name . ""; + if !(labelName || is_numeric(labelName)) { + let labelName = name; } + let code = tagFactory->{"label"}(labelName, attributes); + return code; } /** - * Returns an array of prepared attributes for Phalcon\Tag helpers - * according to the element parameters + * Returns an array of prepared attributes for Phalcon\Html\TagFactory + * helpers according to the element parameters */ public function prepareAttributes(array attributes = [], bool useChecked = false) -> array { @@ -379,12 +378,8 @@ abstract class AbstractElement implements ElementInterface /** * Merge passed parameters with default ones */ - let defaultAttributes = this->attributes; - - let mergedAttributes = array_merge( - defaultAttributes, - attributes - ); + let defaultAttributes = this->attributes, + mergedAttributes = array_merge(defaultAttributes, attributes); /** * Get the current element value @@ -423,6 +418,29 @@ abstract class AbstractElement implements ElementInterface return mergedAttributes; } + /** + * Renders the element widget returning HTML + */ + public function render(array attributes = []) -> string + { + var helper, mergedAttributes, method, name, tagFactory, value; + + let name = this->name, + value = null, + method = this->method, + tagFactory = this->getTagFactory(), + helper = tagFactory->newInstance(method); + + if isset attributes["value"] { + let value = attributes["value"]; + unset attributes["value"]; + } + + let mergedAttributes = array_merge(this->attributes, attributes); + + return helper->__invoke(name, value, mergedAttributes); + } + /** * Sets a default attribute for the element */ @@ -462,7 +480,7 @@ abstract class AbstractElement implements ElementInterface public function setFilters(var filters) -> { if unlikely (typeof filters != "string" && typeof filters != "array") { - throw new Exception("Wrong filter type added"); + throw new Exception("The filter needs to be an array or string"); } let this->filters = filters; @@ -510,6 +528,16 @@ abstract class AbstractElement implements ElementInterface return this; } + /** + * Sets the TagFactory + */ + public function setTagFactory( tagFactory) -> + { + let this->tagFactory = tagFactory; + + return this; + } + /** * Sets an option for the element */ @@ -529,4 +557,18 @@ abstract class AbstractElement implements ElementInterface return this; } + + /** + * Returns the tagFactory; throws exception if not present + */ + protected function getTagFactory() -> + { + if unlikely empty this->tagFactory { + throw new Exception( + "The TagFactory must be set for this element to render" + ); + } + + return this->tagFactory; + } } diff --git a/phalcon/Forms/Element/Check.zep b/phalcon/Forms/Element/Check.zep index bc5d56cabda..13736a0be79 100644 --- a/phalcon/Forms/Element/Check.zep +++ b/phalcon/Forms/Element/Check.zep @@ -10,22 +10,22 @@ namespace Phalcon\Forms\Element; -use Phalcon\Tag; - /** - * Phalcon\Forms\Element\Check - * * Component INPUT[type=check] for forms */ class Check extends AbstractElement { /** - * Renders the element widget returning HTML + * @var string */ - public function render(array attributes = []) -> string - { - return Tag::checkField( - this->prepareAttributes(attributes, true) - ); - } + protected method = "inputCheckbox"; +// /** +// * Renders the element widget returning HTML +// */ +// public function render(array attributes = []) -> string +// { +// return Tag::checkField( +// this->prepareAttributes(attributes, true) +// ); +// } } diff --git a/phalcon/Forms/Element/Date.zep b/phalcon/Forms/Element/Date.zep index 354f8f291f7..366225972bc 100644 --- a/phalcon/Forms/Element/Date.zep +++ b/phalcon/Forms/Element/Date.zep @@ -18,12 +18,7 @@ use Phalcon\Tag; class Date extends AbstractElement { /** - * Renders the element widget returning html + * @var string */ - public function render(array attributes = []) -> string - { - return Tag::dateField( - this->prepareAttributes(attributes) - ); - } + protected method = "inputDate"; } diff --git a/phalcon/Forms/Element/Email.zep b/phalcon/Forms/Element/Email.zep index 4fe76d7af92..670eb832606 100644 --- a/phalcon/Forms/Element/Email.zep +++ b/phalcon/Forms/Element/Email.zep @@ -20,12 +20,7 @@ use Phalcon\Tag; class Email extends AbstractElement { /** - * Renders the element widget returning HTML + * @var string */ - public function render(array attributes = []) -> string - { - return Tag::emailField( - this->prepareAttributes(attributes) - ); - } + protected method = "inputEmail"; } diff --git a/phalcon/Forms/Element/File.zep b/phalcon/Forms/Element/File.zep index dc4111ee537..9145564458e 100644 --- a/phalcon/Forms/Element/File.zep +++ b/phalcon/Forms/Element/File.zep @@ -18,12 +18,7 @@ use Phalcon\Tag; class File extends AbstractElement { /** - * Renders the element widget returning HTML + * @var string */ - public function render(array attributes = []) -> string - { - return Tag::fileField( - this->prepareAttributes(attributes) - ); - } + protected method = "inputFile"; } diff --git a/phalcon/Forms/Element/Hidden.zep b/phalcon/Forms/Element/Hidden.zep index 0d6f6270cd6..2418cfafdde 100644 --- a/phalcon/Forms/Element/Hidden.zep +++ b/phalcon/Forms/Element/Hidden.zep @@ -20,12 +20,7 @@ use Phalcon\Tag; class Hidden extends AbstractElement { /** - * Renders the element widget returning HTML + * @var string */ - public function render(array attributes = []) -> string - { - return Tag::hiddenField( - this->prepareAttributes(attributes) - ); - } + protected method = "inputHidden"; } diff --git a/phalcon/Forms/Element/Numeric.zep b/phalcon/Forms/Element/Numeric.zep index d8032d434d4..8998f57977a 100644 --- a/phalcon/Forms/Element/Numeric.zep +++ b/phalcon/Forms/Element/Numeric.zep @@ -20,12 +20,7 @@ use Phalcon\Tag; class Numeric extends AbstractElement { /** - * Renders the element widget returning HTML + * @var string */ - public function render(array attributes = []) -> string - { - return Tag::numericField( - this->prepareAttributes(attributes) - ); - } + protected method = "inputNumeric"; } diff --git a/phalcon/Forms/Element/Password.zep b/phalcon/Forms/Element/Password.zep index 45b85ba3e08..a693fe838bb 100644 --- a/phalcon/Forms/Element/Password.zep +++ b/phalcon/Forms/Element/Password.zep @@ -20,12 +20,7 @@ use Phalcon\Tag; class Password extends AbstractElement { /** - * Renders the element widget returning HTML + * @var string */ - public function render(array attributes = []) -> string - { - return Tag::passwordField( - this->prepareAttributes(attributes) - ); - } + protected method = "inputPassword"; } diff --git a/phalcon/Forms/Element/Radio.zep b/phalcon/Forms/Element/Radio.zep index 66ed67cb3b8..03199d385da 100644 --- a/phalcon/Forms/Element/Radio.zep +++ b/phalcon/Forms/Element/Radio.zep @@ -20,12 +20,7 @@ use Phalcon\Tag; class Radio extends AbstractElement { /** - * Renders the element widget returning HTML + * @var string */ - public function render(array attributes = []) -> string - { - return Tag::radioField( - this->prepareAttributes(attributes, true) - ); - } + protected method = "inputRadio"; } diff --git a/phalcon/Forms/Element/Submit.zep b/phalcon/Forms/Element/Submit.zep index f732a01185d..1e98478249a 100644 --- a/phalcon/Forms/Element/Submit.zep +++ b/phalcon/Forms/Element/Submit.zep @@ -18,15 +18,7 @@ use Phalcon\Tag; class Submit extends AbstractElement { /** - * Renders the element widget + * @var string */ - public function render(array attributes = []) -> string - { - /** - * Merged passed attributes with previously defined ones - */ - return Tag::submitButton( - this->prepareAttributes(attributes) - ); - } + protected method = "inputSubmit"; } diff --git a/phalcon/Forms/Element/Text.zep b/phalcon/Forms/Element/Text.zep index c8da7ac55bb..86ee352d3b9 100644 --- a/phalcon/Forms/Element/Text.zep +++ b/phalcon/Forms/Element/Text.zep @@ -10,7 +10,7 @@ namespace Phalcon\Forms\Element; -use Phalcon\Tag; +use Phalcon\Forms\Exception; /** * Phalcon\Forms\Element\Text @@ -19,13 +19,4 @@ use Phalcon\Tag; */ class Text extends AbstractElement { - /** - * Renders the element widget - */ - public function render(array attributes = []) -> string - { - return Tag::textField( - this->prepareAttributes(attributes) - ); - } } diff --git a/phalcon/Forms/Element/TextArea.zep b/phalcon/Forms/Element/TextArea.zep index 6e83245350b..08dce19de07 100644 --- a/phalcon/Forms/Element/TextArea.zep +++ b/phalcon/Forms/Element/TextArea.zep @@ -18,12 +18,7 @@ use Phalcon\Tag; class TextArea extends AbstractElement { /** - * Renders the element widget + * @var string */ - public function render(array attributes = []) -> string - { - return Tag::textArea( - this->prepareAttributes(attributes) - ); - } + protected method = "inputTextarea"; } diff --git a/phalcon/Forms/Form.zep b/phalcon/Forms/Form.zep index df8036de09a..5636754a337 100644 --- a/phalcon/Forms/Form.zep +++ b/phalcon/Forms/Form.zep @@ -73,6 +73,11 @@ class Form extends Injectable implements Countable, Iterator, AttributesInterfac */ protected options = []; + /** + * @var TagFactory|null + */ + protected tagFactory = null; + /** * @var ValidationInterface|null */ @@ -129,6 +134,9 @@ class Form extends Injectable implements Countable, Iterator, AttributesInterfac * Link the element to the form */ element->setForm(this); + if method_exists(element, "setTagFactory") { + element->{"setTagFactory"}(this->tagFactory); + } if position == null || empty this->elements { /** @@ -545,6 +553,7 @@ class Form extends Injectable implements Countable, Iterator, AttributesInterfac "label": true, "value": true, "di": true, + "tagFactory": true, "eventsmanager": true ]; @@ -564,13 +573,13 @@ class Form extends Injectable implements Countable, Iterator, AttributesInterfac return this->{method}(); } - /** - * Check if the tag has a default value - */ - if Tag::hasValue(name) { - return Tag::getValue(name); - } - +// /** +// * Check if the tag has a default value +// */ +// if Tag::hasValue(name) { +// return Tag::getValue(name); +// } +// /** * Check if element has default value */ diff --git a/phalcon/Html/Escaper.zep b/phalcon/Html/Escaper.zep index 41c19cccc62..402737b21c4 100644 --- a/phalcon/Html/Escaper.zep +++ b/phalcon/Html/Escaper.zep @@ -42,9 +42,11 @@ class Escaper implements EscaperInterface protected encoding = "utf-8" { get }; /** + * ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 + * * @var int */ - protected flags = 3 { get }; + protected flags = 11 { get }; /** * Escapes a HTML attribute string diff --git a/phalcon/Html/Helper/Label.zep b/phalcon/Html/Helper/Label.zep index 1ebb34f0dc2..c51314a9206 100644 --- a/phalcon/Html/Helper/Label.zep +++ b/phalcon/Html/Helper/Label.zep @@ -20,13 +20,18 @@ class Label extends AbstractHelper /** * Produce a `