Permalink
Browse files

Extract a label widget.

This will make re-use in other widgets easier. Once widgets are in
a registry, this class will allow easy replacement of label generation
everywhere.
  • Loading branch information...
1 parent 6321ee2 commit b3d943e76df2daa9c841054e7906d578d4d5489b @markstory markstory committed Jan 16, 2014
Showing with 170 additions and 0 deletions.
  1. +69 −0 src/View/Input/Label.php
  2. +101 −0 tests/TestCase/View/Input/LabelTest.php
View
@@ -0,0 +1,69 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link http://cakephp.org CakePHP(tm) Project
+ * @since CakePHP(tm) v3.0
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\View\Input;
+
+/**
+ * Form 'widget' for creating labels.
+ *
+ * Generally this element is used by other widgets,
+ * and FormHelper itself.
+ */
+class Label {
+
+/**
+ * Templates
+ *
+ * @var Cake\View\StringTemplate
+ */
+ protected $_templates;
+
+/**
+ * Constructor.
+ *
+ * @param Cake\View\StringTemplate $templates
+ */
+ public function __construct($templates) {
+ $this->_templates = $templates;
+ }
+
+/**
+ * Render a label widget.
+ *
+ * Accepts the following keys in $data:
+ *
+ * - `text` The text for the label.
+ * - `input` The input that can be formatted into the label if the template allows it.
+ * - `escape` Set to false to disable HTML escaping.
+ *
+ * All other attributes will be converted into HTML attributes.
+ *
+ * @param array $data
+ * @return string
+ */
+ public function render($data) {
+ $data += [
+ 'text' => '',
+ 'input' => '',
+ 'escape' => true,
+ ];
+
+ return $this->_templates->format('label', [
+ 'text' => $data['escape'] ? h($data['text']) : $data['text'],
+ 'input' => $data['input'],
+ 'attrs' => $this->_templates->formatAttributes($data, ['text', 'input']),
+ ]);
+ }
+
+}
@@ -0,0 +1,101 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link http://cakephp.org CakePHP(tm) Project
+ * @since CakePHP(tm) v3.0
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Test\TestCase\View\Input;
+
+use Cake\TestSuite\TestCase;
+use Cake\View\Input\Label;
+use Cake\View\StringTemplate;
+
+/**
+ * Label test case.
+ */
+class LabelTest extends TestCase {
+
+/**
+ * setup method.
+ *
+ * @return void
+ */
+ public function setUp() {
+ parent::setUp();
+ $templates = [
+ 'label' => '<label{{attrs}}>{{text}}</label>',
+ ];
+ $this->templates = new StringTemplate($templates);
+ }
+
+/**
+ * test render
+ *
+ * @return void
+ */
+ public function testRender() {
+ $label = new Label($this->templates);
+ $data = [
+ 'text' => 'My text',
+ ];
+ $result = $label->render($data);
+ $expected = [
+ 'label' => [],
+ 'My text',
+ '/label'
+ ];
+ $this->assertTags($result, $expected);
+ }
+
+/**
+ * test render escape
+ *
+ * @return void
+ */
+ public function testRenderEscape() {
+ $label = new Label($this->templates);
+ $data = [
+ 'text' => 'My > text',
+ 'for' => 'Some > value',
+ 'escape' => false,
+ ];
+ $result = $label->render($data);
+ $expected = [
+ 'label' => ['for' => 'Some > value'],
+ 'My > text',
+ '/label'
+ ];
+ $this->assertTags($result, $expected);
+ }
+
+/**
+ * test render escape
+ *
+ * @return void
+ */
+ public function testRenderAttributes() {
+ $label = new Label($this->templates);
+ $data = [
+ 'text' => 'My > text',
+ 'for' => 'some-id',
+ 'id' => 'some-id',
+ 'data-foo' => 'value',
+ ];
+ $result = $label->render($data);
+ $expected = [
+ 'label' => ['id' => 'some-id', 'data-foo' => 'value', 'for' => 'some-id'],
+ 'My &gt; text',
+ '/label'
+ ];
+ $this->assertTags($result, $expected);
+ }
+
+}

0 comments on commit b3d943e

Please sign in to comment.