<commit_before><?php
namespace Intraxia\Jaxion\Axolotl;

use Countable;
use Intraxia\Jaxion\Contract\Axolotl\Serializes;
use Iterator;
use LogicException;
use RuntimeException;

/**
 * Class Collection
 *
 * @package Intraxia\Jaxion
 * @subpackage Axolotl
 */
class Collection implements Countable, Iterator, Serializes {
	/**
	 * Collection elements.
	 *
	 * @var array
	 */
	protected $elements = array();

	/**
	 * Models registered to the collection.
	 *
	 * @var string
	 */
	protected $model;

	/**
	 * Where Collection is in loop.
	 *
	 * @var int
	 */
	protected $position = 0;

	/**
	 * Collection constructor.
	 *
	 * @param array $elements
	 * @param array $config
	 */
	public function __construct( array $elements = array(), array $config = array() ) {
		$this->parse_config( $config );

		foreach ( $elements as $element ) {
			$this->add( $element );
		}
	}

	/**
	 * Adds a new element to the Collection.
	 *
	 * @param mixed $element
	 *
	 * @throws RuntimeException
	 */
	public function add( $element ) {
		if ( $this->model && is_array( $element ) ) {
			$element = new $this->model( $element );
		}

		if ( $this->model && ! ( $element instanceof $this->model ) ) {
			throw new RuntimeException;
		}

		$this->elements[] = $element;
	}

	/**
	 * Fetches the element at the provided index.
	 *
	 * @param int $index
	 *
	 * @return mixed
	 */
	public function at( $index ) {
		return $this->elements[ $index ];
	}

	/**
	 * Return the current element.
	 *
	 * @return mixed
	 */
	public function current() {
		return $this->at( $this->position );
	}

	/**
	 * Move forward to next element.
	 */
	public function next() {
		$this->position ++;
	}

	/**
	 * Return the key of the current element.
	 *
	 * @return mixed
	 */
	public function key() {
		return $this->position;
	}

	/**
	 * Checks if current position is valid.
	 *
	 * @return bool
	 */
	public function valid() {
		return isset( $this->elements[ $this->position ] );
	}

	/**
	 * Rewind the Iterator to the first element.
	 */
	public function rewind() {
		$this->position = 0;
	}

	/**
	 * Count elements of an object.
	 *
	 * @return int
	 */
	public function count() {
		return count( $this->elements );
	}

	/**
	 * Parses the Collection config to set its properties.
	 *
	 * @param array $config
	 *
	 * @throws LogicException
	 */
	protected function parse_config( array $config ) {
		if ( isset( $config['model'] ) ) {
			$model = $config['model'];

			if ( ! is_subclass_of( $model, 'Intraxia\Jaxion\Axolotl\Model' ) ) {
				throw new LogicException;
			}

			$this->model = $model;
		}
	}

	/**
	 * {@inheritDoc}
	 *
	 * @return array
	 */
	public function serialize() {
		return array_map(function( $element ) {
			if ( $element instanceof Serializes ) {
				return $element->serialize();
			}

			return $element;
		}, $this->elements);
	}
}
<commit_msg>Check if element isset before returning<commit_after><?php
namespace Intraxia\Jaxion\Axolotl;

use Countable;
use Intraxia\Jaxion\Contract\Axolotl\Serializes;
use Iterator;
use LogicException;
use RuntimeException;

/**
 * Class Collection
 *
 * @package Intraxia\Jaxion
 * @subpackage Axolotl
 */
class Collection implements Countable, Iterator, Serializes {
	/**
	 * Collection elements.
	 *
	 * @var array
	 */
	protected $elements = array();

	/**
	 * Models registered to the collection.
	 *
	 * @var string
	 */
	protected $model;

	/**
	 * Where Collection is in loop.
	 *
	 * @var int
	 */
	protected $position = 0;

	/**
	 * Collection constructor.
	 *
	 * @param array $elements
	 * @param array $config
	 */
	public function __construct( array $elements = array(), array $config = array() ) {
		$this->parse_config( $config );

		foreach ( $elements as $element ) {
			$this->add( $element );
		}
	}

	/**
	 * Adds a new element to the Collection.
	 *
	 * @param mixed $element
	 *
	 * @throws RuntimeException
	 */
	public function add( $element ) {
		if ( $this->model && is_array( $element ) ) {
			$element = new $this->model( $element );
		}

		if ( $this->model && ! ( $element instanceof $this->model ) ) {
			throw new RuntimeException;
		}

		$this->elements[] = $element;
	}

	/**
	 * Fetches the element at the provided index.
	 *
	 * @param int $index
	 *
	 * @return mixed
	 */
	public function at( $index ) {
		return isset( $this->elements[ $index ] ) ? $this->elements[ $index ] : null;
	}

	/**
	 * Return the current element.
	 *
	 * @return mixed
	 */
	public function current() {
		return $this->at( $this->position );
	}

	/**
	 * Move forward to next element.
	 */
	public function next() {
		$this->position ++;
	}

	/**
	 * Return the key of the current element.
	 *
	 * @return mixed
	 */
	public function key() {
		return $this->position;
	}

	/**
	 * Checks if current position is valid.
	 *
	 * @return bool
	 */
	public function valid() {
		return isset( $this->elements[ $this->position ] );
	}

	/**
	 * Rewind the Iterator to the first element.
	 */
	public function rewind() {
		$this->position = 0;
	}

	/**
	 * Count elements of an object.
	 *
	 * @return int
	 */
	public function count() {
		return count( $this->elements );
	}

	/**
	 * Parses the Collection config to set its properties.
	 *
	 * @param array $config
	 *
	 * @throws LogicException
	 */
	protected function parse_config( array $config ) {
		if ( isset( $config['model'] ) ) {
			$model = $config['model'];

			if ( ! is_subclass_of( $model, 'Intraxia\Jaxion\Axolotl\Model' ) ) {
				throw new LogicException;
			}

			$this->model = $model;
		}
	}

	/**
	 * {@inheritDoc}
	 *
	 * @return array
	 */
	public function serialize() {
		return array_map(function( $element ) {
			if ( $element instanceof Serializes ) {
				return $element->serialize();
			}

			return $element;
		}, $this->elements);
	}
}

{'fraction_non_alphanumeric': 0.15083217753120665, 'fraction_numerical': 0.0006934812760055479, 'mean_word_length': 2.8281353682813535, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 42, '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': '2943688', 'n_tokens_mistral': 2103, 'n_tokens_neox': 1836, 'n_words': 517}