forked from phalcon/cphalcon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
T13771 service locator (phalcon#13772)
* [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
Showing
29 changed files
with
1,626 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 . "/>"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) . ">"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
Oops, something went wrong.