Skip to content
Permalink
Browse files

Extract a trait for generating ID values.

This trait will make re-using the same ID logic possible in other
widgets like the multicheckbox one.
  • Loading branch information...
markstory committed Feb 19, 2014
1 parent 39962eb commit d4e69440468d5e3fed8e600e802730a11889f03a
Showing with 65 additions and 30 deletions.
  1. +60 −0 src/View/Widget/IdGeneratorTrait.php
  2. +5 −30 src/View/Widget/Radio.php
@@ -0,0 +1,60 @@
<?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\Widget;
use Cake\Utility\Inflector;
/**
* A trait that provides id generating methods to be
* used in various widget classes.
*/
trait IdGeneratorTrait {
/**
* A list of id suffixes used in the current rendering.
*
* @var array
*/
protected $_idSuffixes = [];
/**
* Clear the stored ID suffixes.
*
* @return void
*/
protected function _clearIds() {
$this->_idSuffixes = [];
}
/**
* Generate an ID attribute for a radio button.
*
* Ensures that id's for a given set of fields are unique.
*
* @param array $radio The radio properties.
* @return string Generated id.
*/
protected function _id($name, $val) {
$name = mb_strtolower(Inflector::slug($name, '-'));
$idSuffix = mb_strtolower(str_replace(array('@', '<', '>', ' ', '"', '\''), '-', $val));
$count = 1;
$check = $idSuffix;
while (in_array($check, $this->_idSuffixes)) {
$check = $idSuffix . $count++;
}
$this->_idSuffixes[] = $check;
return trim($name . '-' . $check, '-');
}
}
@@ -14,7 +14,7 @@
*/
namespace Cake\View\Widget;
use Cake\Utility\Inflector;
use Cake\View\Widget\IdGeneratorTrait;
use Cake\View\Widget\WidgetInterface;
use Traversable;
@@ -26,6 +26,8 @@
*/
class Radio implements WidgetInterface {
use IdGeneratorTrait;
/**
* Template instance.
*
@@ -40,13 +42,6 @@ class Radio implements WidgetInterface {
*/
protected $_label;
/**
* A list of id suffixes used in the current rendering.
*
* @var array
*/
protected $_idSuffixes = [];
/**
* Constructor
*
@@ -106,7 +101,7 @@ public function render(array $data) {
}
unset($data['empty']);
$this->_idSuffixes = [];
$this->_clearIds();
$opts = [];
foreach ($options as $val => $text) {
$opts[] = $this->_renderInput($val, $text, $data);
@@ -151,7 +146,7 @@ protected function _renderInput($val, $text, $data) {
$radio['name'] = $data['name'];
if (empty($radio['id'])) {
$radio['id'] = $this->_id($radio);
$radio['id'] = $this->_id($radio['name'], $radio['value']);
}
if (isset($data['val']) && is_bool($data['val'])) {
@@ -214,24 +209,4 @@ protected function _renderLabel($radio, $label, $input, $escape) {
return $this->_label->render($labelAttrs);
}
/**
* Generate an ID attribute for a radio button.
*
* Ensures that id's for a given set of fields are unique.
*
* @param array $radio The radio properties.
* @return string Generated id.
*/
protected function _id($radio) {
$value = mb_strtolower(Inflector::slug($radio['name'], '-'));
$idSuffix = mb_strtolower(str_replace(array('@', '<', '>', ' ', '"', '\''), '-', $radio['value']));
$count = 1;
$check = $idSuffix;
while (in_array($check, $this->_idSuffixes)) {
$check = $idSuffix . $count++;
}
$this->_idSuffixes[] = $check;
return trim($value . '-' . $check, '-');
}
}

0 comments on commit d4e6944

Please sign in to comment.
You can’t perform that action at this time.