/** * Reload default template. * * @param string $name * @param string $value * @param string $id * @param string $text * @param array $attrs * @return string */ protected function _elementTpl($name, $value = '', $id = '', $text = '', array $attrs = array()) { $alias = Str::slug($value, true); $inpClass = 'jb-val-' . $alias; $input = $this->input($name, $value, $id, $inpClass, $attrs); $text = '<span class="label-title">' . $text . '</span>'; $html = $this->label($id, $alias, $input . $text); return '<div class="jb-input jb-checkbox">' . $html . '</div>'; }
/** * Clear string classes. * * @param string $classes * @return string */ protected function _clearClasses($classes) { $newClasses = []; $details = explode(' ', $classes); foreach ($details as $class) { if (empty($class)) { continue; } $newClasses[] = Str::slug(Str::clean($class)); } return implode(' ', $newClasses); }
/** * Test radio list output. * * return void */ public function testRadio() { //////////////////////////////////////////////////////////////////////////////////////////////////////////////// $html = $this->radio->render(array('val-1', 'val-2', 'val-3'), 'test', array(), array(), true); $expected = array(array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-0'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 0, 'class' => 'jb-val-0')), ' val-1', '/label', array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-1'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 1, 'class' => 'jb-val-1')), ' val-2', '/label', array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-2'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 2, 'class' => 'jb-val-2')), ' val-3', '/label'); isHtml($expected, $html); //////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Test data and checked single option by array. $html = $this->radio->render(array('<p>tag</p>' => 'Tag', ' ' => 'Empty', '"Test label 3"' => 'Custom', 'значение' => 'Translate cyrillic value', 'common' => 'Common label'), 'test', array('common'), array(), true); $expected = array(array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-lt-p-gt-tag-lt-p-gt'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => '<p>tag</p>', 'class' => 'jb-val-lt-p-gt-tag-lt-p-gt')), ' Tag', '/label', array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => ' ', 'class' => 'jb-val-')), ' Empty', '/label', array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-quot-test-label-3-quot'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => '"Test label 3"', 'class' => 'jb-val-quot-test-label-3-quot')), ' Custom', '/label', array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-znachenie'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'значение', 'class' => 'jb-val-znachenie')), ' Translate cyrillic value', '/label', array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-common'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'common', 'class' => 'jb-val-common', 'checked' => 'checked')), ' Common label', '/label'); isHtml($expected, $html); //////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Test checked single option by string. $html = $this->radio->render(array('"Test label 3"' => 'Custom', 'значение' => 'Translate cyrillic value', 'common' => 'Common label'), 'test', 'common', array(), true); $expected = array(array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-quot-test-label-3-quot'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => '"Test label 3"', 'class' => 'jb-val-quot-test-label-3-quot')), ' Custom', '/label', array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-znachenie'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'значение', 'class' => 'jb-val-znachenie')), ' Translate cyrillic value', '/label', array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-common'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'common', 'class' => 'jb-val-common', 'checked' => 'checked')), ' Common label', '/label'); isHtml($expected, $html); //////////////////////////////////////////////////////////////////////////////////////////////////////////////// $html = $this->radio->render(array('test-7' => 'Test label 7', 'test-8' => 'Test label 8'), 'test', array('common', 'test-8', 'test-2', 'test-4')); $expected = array(array('input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'no-exits', 'class' => 'jb-val-no-exits', 'checked' => 'checked')), array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-no-exits')), 'No exits', '/label', array('input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'test-7', 'class' => 'jb-val-test-7')), array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-test-7')), 'Test label 7', '/label', array('input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'test-8', 'class' => 'jb-val-test-8')), array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-test-8')), 'Test label 8', '/label'); isHtml($expected, $html); //////////////////////////////////////////////////////////////////////////////////////////////////////////////// $html = $this->radio->render(array('test-7' => 'Test label 7', 'test-8' => 'Test label 8'), 'test', 'test'); $expected = array(array('input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'no-exits', 'class' => 'jb-val-no-exits', 'checked' => 'checked')), array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-no-exits')), 'No exits', '/label', array('input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'test-7', 'class' => 'jb-val-test-7')), array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-test-7')), 'Test label 7', '/label', array('input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'test-8', 'class' => 'jb-val-test-8')), array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-test-8')), 'Test label 8', '/label'); isHtml($expected, $html); //////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Test checked options by array. $html = $this->radio->render(array('test-1' => 'Test label 1', 'test-2' => 'Test label 2', 'test-3' => 'Test label 3', 'test-4' => 'Test label 4', 'test-6' => 'Test label 6', 'test-7' => 'Test label 7', 'test-8' => 'Test label 8', 'common' => 'Common label'), 'test', array('common', 'test-8', 'test-2', 'test-4'), array(), true); $expected = array(array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-test-1'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'test-1', 'class' => 'jb-val-test-1')), ' Test label 1', '/label', array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-test-2'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'test-2', 'class' => 'jb-val-test-2')), ' Test label 2', '/label', array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-test-3'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'test-3', 'class' => 'jb-val-test-3')), ' Test label 3', '/label', array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-test-4'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'test-4', 'class' => 'jb-val-test-4', 'checked' => 'checked')), ' Test label 4', '/label', array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-test-6'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'test-6', 'class' => 'jb-val-test-6')), ' Test label 6', '/label', array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-test-7'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'test-7', 'class' => 'jb-val-test-7')), ' Test label 7', '/label', array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-test-8'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'test-8', 'class' => 'jb-val-test-8')), ' Test label 8', '/label', array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-common'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'common', 'class' => 'jb-val-common')), ' Common label', '/label'); isHtml($expected, $html); //////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Test setup callable template. $options = array('test-1' => 'Test label 1'); $html = $this->radio->render($options, 'test', 0, array(), function ($list, $name, $value, $id, $text, $attrs) { $alias = Str::slug($value, true); $inpClass = 'jb-val-' . $alias; $input = $list->input($name, $value, $id, $inpClass, $attrs); $text = '<span class="label-title">' . $text . '</span>'; $label = $list->label($id, $alias, $text); return implode(PHP_EOL, array($input, $label)); }); $expected = array(array('input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'test-1', 'class' => 'jb-val-test-1', 'checked' => 'checked')), array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-test-1')), 'span' => array('class' => 'label-title'), 'Test label 1', '/span', '/label'); isHtml($expected, $html); //////////////////////////////////////////////////////////////////////////////////////////////////////////////// $html = $this->radio->render($options, 'test', 0, array(), 'no-exits'); isEmpty($html); //////////////////////////////////////////////////////////////////////////////////////////////////////////////// $inputReload = Html::_('checkbox', 'Custom\\Html'); $html = $inputReload->render($options, 'test'); $expected = array('div' => array('class' => 'jb-input jb-checkbox'), 'label' => array('for' => 'preg:/checkbox-[0-9]+/', 'class' => 'jb-checkbox-lbl jb-label-test-1'), array('input' => array('id' => 'preg:/checkbox-[0-9]+/', 'name' => 'test', 'type' => 'checkbox', 'value' => 'test-1', 'class' => 'jb-val-test-1')), 'span' => array('class' => 'label-title'), 'Test label 1', '/span', '/label', '/div'); isHtml($expected, $html); }
public function testSlugCache() { $_this = $this; runBench(array('Str::slug(2, false)' => function () use($_this) { return Str::slug($_this->getRandomString(2), false); }, 'Str::slug(2, true)' => function () use($_this) { return Str::slug($_this->getRandomString(2), true); }, 'Slug::filter(2)' => function () use($_this) { return Slug::filter($_this->getRandomString(2)); }, 'Str::slug(3, false)' => function () use($_this) { return Str::slug($_this->getRandomString(3), false); }, 'Str::slug(3, true)' => function () use($_this) { return Str::slug($_this->getRandomString(3), true); }, 'Slug::filter(3)' => function () use($_this) { return Slug::filter($_this->getRandomString(3)); }, 'Str::slug(4, false)' => function () use($_this) { return Str::slug($_this->getRandomString(4), false); }, 'Str::slug(4, true)' => function () use($_this) { return Str::slug($_this->getRandomString(4), true); }, 'Slug::filter(4)' => function () use($_this) { return Slug::filter($_this->getRandomString(4)); }), array('count' => 1000, 'name' => 'Random slug')); }
/** * Single element template output. * * @param string $name * @param string $value * @param string $id * @param string $text * @param array $attrs * @return mixed|null|string */ protected function _elementTpl($name, $value = '', $id = '', $text = '', array $attrs = array()) { $alias = Str::slug($value, true); $inpClass = $this->_jbSrt('val-' . $alias); $input = $this->input($name, $value, $id, $inpClass, $attrs); if ($this->_tpl === 'default') { return $input . $this->label($id, $alias, $text); } elseif ($this->_tpl === 'wrap') { return $this->label($id, $alias, $input . ' ' . $text); } if (is_callable($this->_tpl)) { return call_user_func($this->_tpl, $this, $name, $value, $id, $text, $attrs); } return null; }
/** * Get safe string * * @param $string * @return mixed */ public static function alias($string) { $cleaned = Filter::strip($string); $cleaned = Str::slug($cleaned); return $cleaned; }
/** * Get select options. * * @param array $options * @param array $selected * @param bool|false $isMultiple * @return string */ protected function _getOptions(array $options, array $selected = array(), $isMultiple = false) { $output = array(); list($options, $_selected) = $this->_checkNoSelected($options, $selected, $isMultiple); foreach ($options as $key => $data) { $label = $data; $value = $key; $classes = implode(' ', array($this->_jbSrt('option'), $this->_jbSrt('option-' . Str::slug($value, true)))); $isSelected = $this->_isSelected($value, $_selected); if (is_array($data)) { $output[] = $this->_createGroup($key, $data, $_selected, $options); } else { $output[] = $this->_option($value, $isSelected, $classes, $label); } } return implode(PHP_EOL, $output); }