<commit_before><?php

namespace CrazyCodr\Data\Transform;

/**
* The transform iterator class is an iterator over an iterator that features 
* facilities to easily transform content of an enumerable using anonymous functions/closures
* or other types of transformInterfaces.
*
* Important note, the transform iterator transforms data from it's datasource in a live fashion
* it doesn't preprocess everything. Therefore, you can add/remove transforms as you read data and
* change the way the transform iterator operates.
*
* @uses     iterator
*
* @abstract
*
* @category Base class
* @package  CrazyCodr/Data-Transform
* @author   <EMAIL>
* @license  MIT
* @link     crazycoders.net
*/
class TransformingIterator implements \iterator, TransformerContainerInterface
{

    /**
     * Contains the datasource that will be iterated over applying transformation functions to it
     *
     * @var \Traversable
     *
     * @access protected
     */
    protected $datasource = NULL;

    /**
     * Contains the transformer container used to contain the transformers for this transformer iterator
     *
     * @var TransformerContainerInterface
     *
     * @access protected
     */
    protected $container = NULL;

    /**
     * Contains the transformed data as of now after the next move has been done
     *
     * @var mixed
     *
     * @access protected
     */
    protected $transformedData = NULL;

    /**
     * Builds the TransformerIterator using a specific datasource
     *
     * @param TransformerContainerInterface Transformer container that will be proxied to store the different transformers
     * @param \Traversable $datasource Contains the datasource that will be iterated and transformed
     *
     * @throws \InvalidArgumentException Thrown if $datasource is not an array or traversable
     *
     * @access public
     */
    public function __construct(TransformerContainerInterface $transformerContainer, $datasource = NULL)
    {

        //Set the datasource
        $this->setDatasource($datasource);

        //Set the transformer container
        $this->setTransformerContainer($transformerContainer);

    }

    /**
     * Sets the datasource to be used in the iteration context
     * 
     * @param \Traversable $datasource Datasource to be used in iteration context
     *
     * @throws \InvalidArgumentException Thrown if $datasource is not an array or traversable
     *
     * @access public
     */
    public function setDatasource($datasource = NULL)
    {

        //If there are no datasource, set it to an empty array
        if($datasource === NULL)
        {
            $datasource = array();
        }

        //Validate
        if(!is_array($datasource) && !($datasource instanceof \Traversable))
        {
            throw new \InvalidArgumentException('Datasource must be either an array or \\Traversable');
        }

        //Save the datasource
        $this->datasource = $datasource;
        
    }

    /**
     * Returns the current datasource used in the iteration context
     * 
     * @access public
     *
     * @return \Traversable Datasource used in the iteration context
     */
    public function getDatasource()
    {
        return $this->datasource;
    }

    /**
     * Sets the TransformerContainer to be used in the transformer storage context
     * 
     * @param TransformerContainerInterface $transformerContainer TransformerContainer to use in this iterator
     *
     * @access public
     */
    public function setTransformerContainer(TransformerContainerInterface $transformerContainer = NULL)
    {
        $this->transformerContainer = $transformerContainer;
    }

    /**
     * Returns the current TransformerContainer used in the transformer storage context
     * 
     * @access public
     *
     * @return TransformerContainerInterface TransformerContainer used in the transformer storage context
     */
    public function getTransformerContainer()
    {
        return $this->transformerContainer;
    }

    /**
     * Implentation of the Iterator SPL class for Current(), 
     * returns the current element of the data source
     * Returns null if nothing found
     * 
     * @access public
     *
     * @return mixed Current value of the iterator
     */
	public function current()
	{
		return $this->transformedData;
	}

    /**
     * Implentation of the Iterator SPL class for Key(), 
     * returns the current element's identification of the data source
     * Returns null if nothing found
     * 
     * @access public
     *
     * @return mixed Value.
     */
	public function key()
	{
		return key($this->datasource);
	}

    /**
     * Implentation of the Iterator SPL class for Next(), 
     * prepares the next record in line to be read and return by Current() and Key()
     * 
     * @access public
     */
	public function next()
	{

        //Move to the next element
        next($this->datasource);

        //Process the transformers
        $this->transformedData = $this->transform(current($this->datasource), key($this->datasource));

	}

    /**
     * Implentation of the Iterator SPL class for Rewind(), 
     * prepares the whole datasource for an entirely new iterator operation
     * 
     * @access public
     */
    public function rewind()
    {
        reset($this->datasource);

        //Process the transformers
        $this->transformedData = $this->transform(current($this->datasource), key($this->datasource));
    }

    /**
     * Implentation of the Iterator SPL class for Valid(), 
     * Checks if the current item is a valid item for processing
     * NULL keys represent an invalid item
     * 
     * @access public
     */<commit_msg>Quick unseen issue in next() and rewind()<commit_after><?php

namespace CrazyCodr\Data\Transform;

/**
* The transform iterator class is an iterator over an iterator that features 
* facilities to easily transform content of an enumerable using anonymous functions/closures
* or other types of transformInterfaces.
*
* Important note, the transform iterator transforms data from it's datasource in a live fashion
* it doesn't preprocess everything. Therefore, you can add/remove transforms as you read data and
* change the way the transform iterator operates.
*
* @uses     iterator
*
* @abstract
*
* @category Base class
* @package  CrazyCodr/Data-Transform
* @author   <EMAIL>
* @license  MIT
* @link     crazycoders.net
*/
class TransformingIterator implements \iterator, TransformerContainerInterface
{

    /**
     * Contains the datasource that will be iterated over applying transformation functions to it
     *
     * @var \Traversable
     *
     * @access protected
     */
    protected $datasource = NULL;

    /**
     * Contains the transformer container used to contain the transformers for this transformer iterator
     *
     * @var TransformerContainerInterface
     *
     * @access protected
     */
    protected $container = NULL;

    /**
     * Contains the transformed data as of now after the next move has been done
     *
     * @var mixed
     *
     * @access protected
     */
    protected $transformedData = NULL;

    /**
     * Builds the TransformerIterator using a specific datasource
     *
     * @param TransformerContainerInterface Transformer container that will be proxied to store the different transformers
     * @param \Traversable $datasource Contains the datasource that will be iterated and transformed
     *
     * @throws \InvalidArgumentException Thrown if $datasource is not an array or traversable
     *
     * @access public
     */
    public function __construct(TransformerContainerInterface $transformerContainer, $datasource = NULL)
    {

        //Set the datasource
        $this->setDatasource($datasource);

        //Set the transformer container
        $this->setTransformerContainer($transformerContainer);

    }

    /**
     * Sets the datasource to be used in the iteration context
     * 
     * @param \Traversable $datasource Datasource to be used in iteration context
     *
     * @throws \InvalidArgumentException Thrown if $datasource is not an array or traversable
     *
     * @access public
     */
    public function setDatasource($datasource = NULL)
    {

        //If there are no datasource, set it to an empty array
        if($datasource === NULL)
        {
            $datasource = array();
        }

        //Validate
        if(!is_array($datasource) && !($datasource instanceof \Traversable))
        {
            throw new \InvalidArgumentException('Datasource must be either an array or \\Traversable');
        }

        //Save the datasource
        $this->datasource = $datasource;
        
    }

    /**
     * Returns the current datasource used in the iteration context
     * 
     * @access public
     *
     * @return \Traversable Datasource used in the iteration context
     */
    public function getDatasource()
    {
        return $this->datasource;
    }

    /**
     * Sets the TransformerContainer to be used in the transformer storage context
     * 
     * @param TransformerContainerInterface $transformerContainer TransformerContainer to use in this iterator
     *
     * @access public
     */
    public function setTransformerContainer(TransformerContainerInterface $transformerContainer = NULL)
    {
        $this->transformerContainer = $transformerContainer;
    }

    /**
     * Returns the current TransformerContainer used in the transformer storage context
     * 
     * @access public
     *
     * @return TransformerContainerInterface TransformerContainer used in the transformer storage context
     */
    public function getTransformerContainer()
    {
        return $this->transformerContainer;
    }

    /**
     * Implentation of the Iterator SPL class for Current(), 
     * returns the current element of the data source
     * Returns null if nothing found
     * 
     * @access public
     *
     * @return mixed Current value of the iterator
     */
	public function current()
	{
		return $this->transformedData;
	}

    /**
     * Implentation of the Iterator SPL class for Key(), 
     * returns the current element's identification of the data source
     * Returns null if nothing found
     * 
     * @access public
     *
     * @return mixed Value.
     */
	public function key()
	{
		return key($this->datasource);
	}

    /**
     * Implentation of the Iterator SPL class for Next(), 
     * prepares the next record in line to be read and return by Current() and Key()
     * 
     * @access public
     */
	public function next()
	{

        //Move to the next element
        next($this->datasource);

        //Process the transformers
        if($this->valid())
        {
            $this->transformedData = $this->transform(current($this->datasource), key($this->datasource));   
        }

	}

    /**
     * Implentation of the Iterator SPL class for Rewind(), 
     * prepares the whole datasource for an entirely new iterator operation
     * 
     * @access public
     */
    public function rewind()
    {
        reset($this->datasource);

        //Process the transformers
        if($this->valid())
        {
            $this->transformedData = $this->transform(current($this->datasource), key($this->datasource));   
        }
        
    }

    /**
     * Implentation of the Iterator SPL class for Valid(), 
     * Checks if the current item is a valid item for processing
     * NULL keys represent an invalid item
     * 
     * @access public
     */
{'fraction_non_alphanumeric': 0.08029765510080471, 'fraction_numerical': 0.0, 'mean_word_length': 2.50030284675954, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 43, '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': '4665278', 'n_tokens_mistral': 3093, 'n_tokens_neox': 2905, 'n_words': 1050}