Skip to content

Commit

Permalink
T13771 service locator (phalcon#13772)
Browse files Browse the repository at this point in the history
* [html] - Added service locator

* [html] - New tag helper structure (anchor)

* [phalcon#13771] - Added more html helpers and tests

* [phalcon#13771] - Changed the exception message; Corrections based on review
  • Loading branch information
niden authored Jan 19, 2019
1 parent 8261f4f commit 48fad40
Show file tree
Hide file tree
Showing 29 changed files with 1,626 additions and 1 deletion.
126 changes: 126 additions & 0 deletions phalcon/html/helper/abstracthelper.zep
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalconphp.com>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

namespace Phalcon\Html\Helper;

use Phalcon\Html\Exception;
use Phalcon\EscaperInterface;

/**
* Phalcon\Html\Helper\AbstractHelper
*
* Abstract class for all html helpers
*/
abstract class AbstractHelper
{
/**
* @var <EscaperInterface>
*/
protected escaper;

/**
* Constructor
*/
public function __construct(<EscaperInterface> escaper)
{
let this->escaper = escaper;
}

/**
* Keeps all the attributes sorted - same order all the tome
*/
protected function orderAttributes(array overrides, array attributes) -> array
{
var order, intersect, results;

let order = [
"rel" : null,
"type" : null,
"for" : null,
"src" : null,
"href" : null,
"action" : null,
"id" : null,
"name" : null,
"value" : null,
"class" : null
];

let intersect = array_intersect_key(order, attributes),
results = array_merge(intersect, attributes),
results = array_merge(overrides, results);

/**
* Just in case remove the "escape" attribute
*/
unset results["escape"];

return results;
}

/**
* Renders all the attributes
*/
protected function renderAttributes(array attributes) -> string
{
var key, result, value;

let result = "";
for key, value in attributes {
if typeof key == "string" && value !== null {
if typeof value == "array" || typeof value == "resource" || typeof value == "object" {
throw new Exception(
"Value at index: '" . key . "' type: '" . gettype(value) . "' cannot be rendered"
);
}

let result .= key . "=\"" . this->escaper->escapeHtmlAttr(value) . "\" ";

}
}

return result;
}

/**
* Renders an element
*/
protected function renderElement(string tag, string text, array attributes = [])
{
var attrs, escapedAttrs, escapedText;

let escapedAttrs = "",
escapedText = this->escaper->escapeHtml(text);

if (count(attributes) > 0) {
let attrs = this->orderAttributes([], attributes),
escapedAttrs = " " . rtrim(this->renderAttributes(attrs));
}

return "<" . tag . escapedAttrs . ">" . escapedText . "</" . tag. ">";
}

/**
* Produces a self close tag i.e. <img />
*/
protected function selfClose(string tag, array attributes = [])
{
var attrs, escapedAttrs;

let escapedAttrs = "";

if (count(attributes) > 0) {
let attrs = this->orderAttributes([], attributes),
escapedAttrs = " " . rtrim(this->renderAttributes(attrs));
}

return "<" . tag . escapedAttrs . "/>";
}
}
45 changes: 45 additions & 0 deletions phalcon/html/helper/anchor.zep
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalconphp.com>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

namespace Phalcon\Html\Helper;

use Phalcon\Html\Helper\AbstractHelper;

/**
* Phalcon\Html\Helper\Anchor
*
* Creates an anchor
*/
class Anchor extends AbstractHelper
{
/**
* @var string href The href tag
* @var string text The text for the anchor
* @var array attributes Any additional attributes
*/
public function __invoke(string href, string text, array attributes = [])
{
var overrides, escapedText;

let overrides = [
"href" : href
];

/**
* Avoid duplicate "href" and ignore it if it is passed in the attributes
*/
unset(attributes["href"]);

let overrides = this->orderAttributes(overrides, attributes),
escapedText = this->escaper->escapeHtml(text);

return "<a " . rtrim(this->renderAttributes(overrides)) . ">" . escapedText . "</a>";
}
}
30 changes: 30 additions & 0 deletions phalcon/html/helper/button.zep
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalconphp.com>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

namespace Phalcon\Html\Helper;

use Phalcon\Html\Helper\AbstractHelper;

/**
* Phalcon\Html\Helper\Button
*
* Creates a button tag
*/
class Button extends AbstractHelper
{
/**
* @var string text The text for the anchor
* @var array attributes Any additional attributes
*/
public function __invoke(string text, array attributes = [])
{
return this->renderElement("button", text, attributes);
}
}
31 changes: 31 additions & 0 deletions phalcon/html/helper/element.zep
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalconphp.com>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

namespace Phalcon\Html\Helper;

use Phalcon\Html\Helper\AbstractHelper;

/**
* Phalcon\Html\Helper\Address
*
* Creates an element
*/
class Element extends AbstractHelper
{
/**
* @var string tag The tag name
* @var string text The text for the anchor
* @var array attributes Any additional attributes
*/
public function __invoke(string tag, string text, array attributes = [])
{
return this->renderElement(tag, text, attributes);
}
}
38 changes: 38 additions & 0 deletions phalcon/html/helper/form.zep
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalconphp.com>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

namespace Phalcon\Html\Helper;

use Phalcon\Html\Helper\AbstractHelper;

/**
* Phalcon\Html\Helper\Form
*
* Creates a form opening tag
*/
class Form extends AbstractHelper
{
/**
* @var array attributes Any additional attributes
*/
public function __invoke(array attributes = [])
{
var overrides;

let overrides = [
"method" : "post",
"enctype" : "multipart/form-data"
];

let overrides = this->orderAttributes(overrides, attributes);

return "<form " . rtrim(this->renderAttributes(overrides)) . ">";
}
}
29 changes: 29 additions & 0 deletions phalcon/html/helper/formclose.zep
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalconphp.com>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

namespace Phalcon\Html\Helper;

use Phalcon\Html\Helper\AbstractHelper;

/**
* Phalcon\Html\Helper\FormClose
*
* Creates a form closing tag
*/
class FormClose extends AbstractHelper
{
/**
* @var array attributes Any additional attributes
*/
public function __invoke()
{
return "</form>";
}
}
30 changes: 30 additions & 0 deletions phalcon/html/helper/textarea.zep
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalconphp.com>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

namespace Phalcon\Html\Helper;

use Phalcon\Html\Helper\AbstractHelper;

/**
* Phalcon\Html\Helper\TextArea
*
* Creates a textarea tag
*/
class TextArea extends AbstractHelper
{
/**
* @var string text The text for the anchor
* @var array attributes Any additional attributes
*/
public function __invoke(string text, array attributes = [])
{
return this->renderElement("textarea", text, attributes);
}
}
2 changes: 1 addition & 1 deletion phalcon/html/tag.zep
Original file line number Diff line number Diff line change
Expand Up @@ -1430,7 +1430,7 @@ class Tag implements InjectionAwareInterface
* use Phalcon\Html\Tag;
*
* $tag = new Tag();
*
*public
* echo $tag->submit('Save')
* </code>
*
Expand Down
22 changes: 22 additions & 0 deletions phalcon/html/taglocator.zep
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalconphp.com>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

namespace Phalcon\Html;

use Phalcon\Service\Locator;

/**
* Phalcon\Html\TagLocator
*
* Lazy loads, stores and exposes tag helper objects
*/
class TagLocator extends Locator
{
}
Loading

0 comments on commit 48fad40

Please sign in to comment.