<commit_before><?php

/* !
 * SmoothPHP
 * This file is part of the SmoothPHP project.
 * * * *
 * Copyright (C) 2016 <NAME>
 * License: https://github.com/Ikkerens/SmoothPHP/blob/master/License.md
 * * * *
 * FormBuilder.php
 * Description
 */

namespace SmoothPHP\Framework\Forms;

use SmoothPHP\Framework\Forms\Containers\FormContainer;
use SmoothPHP\Framework\Forms\Containers\Type;
use SmoothPHP\Framework\Forms\Types as Types;

class FormBuilder {
    private $properties;

    /**
     * @param string $field of the field
     * @param string $type Type name of the field
     * @param array $attributes Properties to be used, such as Label
     * @return $this
     */
    public function add($field, $type = null, array $attributes = array()) {
        if (isset($this->properties[$field]))
            throw new \RuntimeException("Form field has already been declared.");

        $this->properties[$field] = array_merge_recursive(array(
            'field' => $field,
            'type' => $type ?: Types\StringType::class,
            'attr' => array()
        ), $attributes);

        return $this;
    }

    public function getForm() {
        $elements = array();

        foreach ($this->properties as $key => $value) {
            /* @var $element Type */
            $element = new $value['type']($key, $value);
            $elements[] = new FormContainer(array(
                'rowstart' => '<tr><td>',
                'label' => $element->generateLabel(),
                'rowseparator' => '</td><td>',
                'input' => $element,
                'rowend' => '</td></tr>'
            ));
        }

        return new Form($elements);
    }
}<commit_msg>Fixed form elements not properly being given an array key<commit_after><?php

/* !
 * SmoothPHP
 * This file is part of the SmoothPHP project.
 * * * *
 * Copyright (C) 2016 <NAME>
 * License: https://github.com/Ikkerens/SmoothPHP/blob/master/License.md
 * * * *
 * FormBuilder.php
 * Description
 */

namespace SmoothPHP\Framework\Forms;

use SmoothPHP\Framework\Forms\Containers\FormContainer;
use SmoothPHP\Framework\Forms\Containers\Type;
use SmoothPHP\Framework\Forms\Types as Types;

class FormBuilder {
    private $properties;

    /**
     * @param string $field of the field
     * @param string $type Type name of the field
     * @param array $attributes Properties to be used, such as Label
     * @return $this
     */
    public function add($field, $type = null, array $attributes = array()) {
        if (isset($this->properties[$field]))
            throw new \RuntimeException("Form field has already been declared.");

        $this->properties[$field] = array_merge_recursive(array(
            'field' => $field,
            'type' => $type ?: Types\StringType::class,
            'attr' => array()
        ), $attributes);

        return $this;
    }

    public function getForm() {
        $elements = array();

        foreach ($this->properties as $key => $value) {
            /* @var $element Type */
            $element = new $value['type']($key, $value);
            $elements[$key] = new FormContainer(array(
                'rowstart' => '<tr><td>',
                'label' => $element->generateLabel(),
                'rowseparator' => '</td><td>',
                'input' => $element,
                'rowend' => '</td></tr>'
            ));
        }

        return new Form($elements);
    }
}
{'fraction_non_alphanumeric': 0.1476608187134503, 'fraction_numerical': 0.0023391812865497076, 'mean_word_length': 2.2643129770992365, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 43, 'https://': 2, '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': '1055253', 'n_tokens_mistral': 986, 'n_tokens_neox': 954, 'n_words': 251}