<commit_before><?php

namespace Logikos\Forms\Element;

use Phalcon\Tag;
use Phalcon\Forms\Element;
use Phalcon\Forms\Element\Radio;
use Phalcon\Forms\Element\Select;
use Phalcon\Forms\ElementInterface;
use Logikos\Forms\Tag\Radioset as RadiosetTag;

class Radioset extends Select implements ElementInterface {
  public function render($attributes=null) {
    return RadiosetTag::RadiosetField($this->prepareAttributes($attributes), $this->getOptions());
  }
}<commit_msg>added support to render radios in radioset with a partial view template<commit_after><?php

namespace Logikos\Forms\Element;

use Phalcon\Tag;
use Phalcon\Forms\Element;
use Phalcon\Forms\Element\Radio;
use Phalcon\Forms\Element\Select;
use Phalcon\Forms\ElementInterface;
use Logikos\Forms\Tag\Radioset as RadiosetTag;
use InvalidArgumentException;

class Radioset extends Select implements ElementInterface {
  protected $_rendercallback;
  protected $_radiotemplate;

  public function setRadioTemplate($view) {
    $this->_radiotemplate = $view;
  }
  public function getRadioTemplate() {
    return $this->_radiotemplate;
  }
  
  public function setRenderCallback($callback) {
    if (!is_callable($callback))
      throw new InvalidArgumentException("RenderCallback must be callable...");
    
    $this->_rendercallback = $callback;
  }
  
  public function getRenderCallback() {
    if ($this->getRadioTemplate())
      $this->setRenderCallback([$this,'renderWithTemplate']);
    
    return $this->_rendercallback;
  }
  
  public function render($attributes=null) {
    $params    = $this->prepareAttributes($attributes);
    $params[0] = $this->getName();
    $params[1] = $this->getOptions();
    $params[2] = $this->getRenderCallback();
    return RadiosetTag::RadiosetField($params);
  }
  
  public function renderWithTemplate($name,$value,$label,$curent_value) {
    
    $attr = [
        'type'  => 'radio',
        'name'  => $name,
        'value' => $value,
        'id'    => null
    ];
    
    $radio = new Radio($name,$attr);
    
    $radio->setDefault($curent_value);
    
    $view = $this->getView();
    $view->partial(
        $this->_radiotemplate,
        [
            'form'    => $this->getForm(),
            'element' => $radio,
            'name'    => $name,
            'value'   => $value,
            'label'   => $label
        ]
    );
  }
  
  /**
   * @return \Phalcon\Mvc\ViewBaseInterface
   */
  protected function getView() {
    /* @var $form \Phalcon\Forms\Form */
    $form = $this->getForm();
    
    if (isset($this->view) && $this->view instanceof ViewBaseInterface)
      return $this->view;
    return $form->getDI()->get('view');
  }
}
{'fraction_non_alphanumeric': 0.14082397003745317, 'fraction_numerical': 0.0011235955056179776, 'mean_word_length': 3.280448717948718, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2453784', 'n_tokens_mistral': 828, 'n_tokens_neox': 828, 'n_words': 162}