<commit_before><?php

namespace Ibrows\XeditableBundle\Mapper;

use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Templating\EngineInterface;

abstract class AbstractFormXeditableMapper extends AbstractXeditableMapper
{
    /**
     * @var FormInterface
     */
    protected $form;

    /**
     * @var bool
     */
    protected $renderFormPrototype = true;

    /**
     * @return FormInterface
     */
    public function getForm()
    {
        return $this->form;
    }

    /**
     * @param string $path
     * @param null $form
     * @param bool $removeOther
     * @return FormInterface
     * @throws \Exception
     */
    protected function getFormByPath($path, $form = null, $removeOther = false)
    {
        if (!$form) {
            $form = $this->form;
        }

        if (!$path) {
            return $form;
        }

        $parts = explode(".", $path);
        while (!is_null($name = array_shift($parts))) {
            if (!$form->has($name)) {
                throw new \Exception("$name not found in form {$form->getName()}");
            }
            if ($removeOther) { //remove other childs to save time & reduce db requests
                foreach ($form->all() as $childname => $child) {
                    if ($name != $childname) {
                        $form->remove($childname);
                    }
                }
            }
            $form = $form->get($name);
        }

        return $form;
    }

    /**<commit_msg>Added comments in formByPath, and started adding support for a collection of elements in a one-to-many, many-to-one, etc...<commit_after><?php

namespace Ibrows\XeditableBundle\Mapper;

use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Templating\EngineInterface;

abstract class AbstractFormXeditableMapper extends AbstractXeditableMapper
{
    /**
     * @var FormInterface
     */
    protected $form;

    /**
     * @var bool
     */
    protected $renderFormPrototype = true;

    /**
     * @return FormInterface
     */
    public function getForm()
    {
        return $this->form;
    }

    /**
     * @param string $path
     * @param null $form
     * @param bool $removeOther
     * @return FormInterface
     * @throws \Exception
     */
    protected function getFormByPath($path, $form = null, $removeOther = false)
    {
        //Set the form, if it isn't set
        if (!$form) {
            $form = $this->form;
        }

        //If no path is set, then return the whole form
        if (!$path) {
            return $form;
        }

        //Explode every part on a period
        $parts = explode(".", $path);

        //Look through form for field names...
        //Finds information for that particular field
        while (!is_null($name = array_shift($parts))) {

            //Check to see if the basic form doesn't include the type name
            if (!$form->has($name)) {

                //Now loop through all of the children elements to see if it a collection type
                foreach($form->all() as $childForm) {

                    if (!$childForm instanceof Form) {
                        //It must not be, so return new exception
                        throw new \Exception("$name not found in form {$form->getName()}");
                    } else {
                        //Oh it seems like it is so see if childform has the element and store it in an array or throw exception
                        if (!$childForm->has($name)) {
                            throw new \Exception("$name not found in form {$form->getName()}");
                        } else {
                            $form[] = $childForm->get($name);
                        }
                    }
                }

            } else {
                $form = $form->get($name);
            }

            if ($removeOther) { //remove other childs to save time & reduce db requests
                foreach ($form->all() as $childname => $child) {
                    if ($name != $childname) {
                        $form->remove($childname);
                    }
                }
            }
        }

        //Return form if something is found
        return $form;
    }

    /**
{'fraction_non_alphanumeric': 0.11578221915920055, 'fraction_numerical': 0.0, 'mean_word_length': 1.5687315634218288, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, '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': '3816412', 'n_tokens_mistral': 1154, 'n_tokens_neox': 1119, 'n_words': 335}