<commit_before><?php

namespace Monolyth\Formulaic;

/**
 * Implements a POST-form. The method and source default to that. You could
 * override the method, but that would not make sense. Elements are
 * automatically populated from `$_POST` and/or `$_FILES`.
 */
abstract class Post extends Form
{
    /**
     * Returns the default string representation of this form.
     *
     * @return string The form as '<form>...</form>'.
     * @see Monolyth\Formulaic\Form::__toString
     */
    public function __toString() : string
    {
        foreach ((array)$this as $field) {
            if (is_string($field)) {
                continue;
            }
            if ($field->getElement() instanceof File) {
                $this->attributes['enctype'] = 'multipart/form-data';
            }
        }
        $this->attributes['method'] = 'post';
        return parent::__toString();
    }

    /**
     * Adds an item to the form, checking to see if its $_POST or $_FILES
     * variant exists and if so, uses that as the value.
     *
     * @param integer|string|null $index The index to set the new item at.
     * @param mixed $item An element or a label containing one.
     * @return void
     */
    public function offsetSet($index, $item) : void
    {
        if ($item instanceof Fieldset) {
            foreach ($item as $subitem) {
                $this->setValue($subitem);
            }
        } else {
            $this->setValue($item);
        }
        parent::offsetSet($index, $item);
    }

    /**
     * Internal helper method to set the value of whatever we are dealing with.
     *
     * @param mixed $item
     * @return void
     */
    private function setValue($item) : void
    {
        if (is_string($item)) {
            return;
        }
        $element = $item->getElement();
        $name = $element->name();
        if ($element instanceof File) {
            if (array_key_exists($name, $_FILES)) {
                $element->setValue($_FILES[$name]['tmp_name']);
                $element->valueSuppliedByUser(true);
            }
        } elseif (array_key_exists($name, $_POST)) {
            if ($element instanceof Radio) {
                if ($_POST[$name] == $element->getValue()
                    || (is_array($_POST[$name])
                        && in_array($element->getValue(), $_POST[$name])
                    )
                ) {
                    $element->check();
                } else {
                    $element->check(false);
                }
            } else {
                $element->setValue($_POST[$name]);
            }
            $element->valueSuppliedByUser(true);
        } elseif (($_SERVER['REQUEST_METHOD'] ?? '') === 'POST') {
            if ($element instanceof Radio) {
                $element->check(false);
                $element->valueSuppliedByUser(true);
            } elseif ($element instanceof Radio\Group) {
                foreach ($element as $radio) {
                    $radio->check(false);
                }
                $element->valueSuppliedByUser(true);
            }
        }
    }
}
<commit_msg>cleanup by moving logic to a central place; also, make the check for enctype better<commit_after><?php

namespace Monolyth\Formulaic;

/**
 * Implements a POST-form. The method and source default to that. You could
 * override the method, but that would not make sense. Elements are
 * automatically populated from `$_POST` and/or `$_FILES`.
 */
abstract class Post extends Form
{
    use ContainsFile;

    /**
     * Returns the default string representation of this form.
     *
     * @return string The form as '<form>...</form>'.
     * @see Monolyth\Formulaic\Form::__toString
     */
    public function __toString() : string
    {
        if ($this->containsFile()) {
            $this->attributes['enctype'] = 'multipart/form-data';
        }
        $this->attributes['method'] = 'post';
        return parent::__toString();
    }

    public function wasSubmitted() : bool
    {
        return ($_SERVER['REQUEST_METHOD'] ?? '') === 'POST';
    }

    protected function getSource() : array
    {
        return $_FILES + $_POST;
    }
}

{'fraction_non_alphanumeric': 0.12967401725790986, 'fraction_numerical': 0.0, 'mean_word_length': 1.8329938900203666, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 29, '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': '1590994', 'n_tokens_mistral': 1131, 'n_tokens_neox': 1099, 'n_words': 310}