<commit_before><?php
/**
 * Trait with common method for collection objects (objects having children)
 */
namespace FormManager\Traits;

use FormManager\InputInterface;
use FormManager\CollectionInterface;

trait CollectionTrait
{
    protected $children = [];
    protected $sanitizer;

    public function __clone()
    {
        foreach ($this->children as $key => $child) {
            $this->children[$key] = clone $child;
            $this->children[$key]->setParent($this);
        }
    }

    //Iterator interface methods:
    public function rewind()
    {
        return reset($this->children);
    }

    public function current()
    {
        return current($this->children);
    }

    public function key()
    {
        return key($this->children);
    }

    public function next()
    {
        return next($this->children);
    }

    public function valid()
    {
        return key($this->children) !== null;
    }

    //ArrayAccess interface methods
    public function offsetSet($offset, $value)
    {
        if ($offset === null) {
            throw new \InvalidArgumentException('You must define a key to every element added');
        }

        $this->add($offset, $value);
    }

    public function offsetExists($offset)
    {
        return isset($this->children[$offset]);
    }

    public function offsetUnset($offset)
    {
        unset($this->children[$offset]);
    }

    public function offsetGet($offset)
    {
        return isset($this->children[$offset]) ? $this->children[$offset] : null;
    }

    /**
     * Adds new children to this element
     *
     * @param array|InputInterface $key   The child or an array with children
     * @param InputInterface       $value The child
     *
     * @return $this;
     */
    public function add($key, $value = null)
    {
        if (is_array($key)) {
            foreach ($key as $key => $value) {
                $this->add($key, $value);
            }

            return $this;
        }

        $this->children[$key] = $value->setParent($this);
        $this->prepareChild($value, $key);

        return $this;
    }

    /**
     * Checks whether all input values are valid
     *
     * @return boolean
     */
    public function isValid()
    {
        foreach ($this->children as $child) {
            if (!$child->isValid()) {
                return false;
            }
        }

        return $this->validate();
    }

    /**
     * Set/Get the values of this collection
     *
     * @param null|array $value null to getter, array to setter
     *
     * @return mixed
     */
    public function val($value = null)
    {
        if ($value === null) {
            $value = [];

            foreach ($this->children as $key => $child) {
                $value[$key] = $child->val();
            }

            return $value;
        }

        foreach ($this->children as $key => $child) {
            $child->val(isset($value[$key]) ? $value[$key] : null);
        }
<commit_msg>make isValid executes all validations<commit_after><?php
/**
 * Trait with common method for collection objects (objects having children)
 */
namespace FormManager\Traits;

use FormManager\InputInterface;
use FormManager\CollectionInterface;

trait CollectionTrait
{
    protected $children = [];
    protected $sanitizer;

    public function __clone()
    {
        foreach ($this->children as $key => $child) {
            $this->children[$key] = clone $child;
            $this->children[$key]->setParent($this);
        }
    }

    //Iterator interface methods:
    public function rewind()
    {
        return reset($this->children);
    }

    public function current()
    {
        return current($this->children);
    }

    public function key()
    {
        return key($this->children);
    }

    public function next()
    {
        return next($this->children);
    }

    public function valid()
    {
        return key($this->children) !== null;
    }

    //ArrayAccess interface methods
    public function offsetSet($offset, $value)
    {
        if ($offset === null) {
            throw new \InvalidArgumentException('You must define a key to every element added');
        }

        $this->add($offset, $value);
    }

    public function offsetExists($offset)
    {
        return isset($this->children[$offset]);
    }

    public function offsetUnset($offset)
    {
        unset($this->children[$offset]);
    }

    public function offsetGet($offset)
    {
        return isset($this->children[$offset]) ? $this->children[$offset] : null;
    }

    /**
     * Adds new children to this element
     *
     * @param array|InputInterface $key   The child or an array with children
     * @param InputInterface       $value The child
     *
     * @return $this;
     */
    public function add($key, $value = null)
    {
        if (is_array($key)) {
            foreach ($key as $key => $value) {
                $this->add($key, $value);
            }

            return $this;
        }

        $this->children[$key] = $value->setParent($this);
        $this->prepareChild($value, $key);

        return $this;
    }

    /**
     * Checks whether all input values are valid
     *
     * @return boolean
     */
    public function isValid()
    {
        $valid = true;

        foreach ($this->children as $child) {
            if (!$child->isValid()) {
                $valid = false;
            }
        }

        if (!$this->validate()) {
            $valid = false;
        }

        return $valid;
    }

    /**
     * Set/Get the values of this collection
     *
     * @param null|array $value null to getter, array to setter
     *
     * @return mixed
     */
    public function val($value = null)
    {
        if ($value === null) {
            $value = [];

            foreach ($this->children as $key => $child) {
                $value[$key] = $child->val();
            }

            return $value;
        }

        foreach ($this->children as $key => $child) {
            $child->val(isset($value[$key]) ? $value[$key] : null);
        }

{'fraction_non_alphanumeric': 0.13512627090849458, 'fraction_numerical': 0.0, 'mean_word_length': 1.85, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 61, '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': '5084622', 'n_tokens_mistral': 1702, 'n_tokens_neox': 1663, 'n_words': 434}