<commit_before><?php namespace Michaeljennings\Refinery;

use Closure, Traversable;
use Michaeljennings\Refinery\Exceptions\RefineryMethodNotFound;
use Michaeljennings\Refinery\Exceptions\AttachmentClassNotFound;
use Michaeljennings\Refinery\Contracts\Refinery as RefineryContract;

abstract class Refinery implements RefineryContract {

	/**
	 * The items to be attached to the refined item.
	 * 
	 * @var array
	 */
	protected $attachments = array();

	/**
	 * A filter to run on the raw data.
	 * 
	 * @var callable
	 */
	protected $filter;

	/**
	 * Set the template the refinery will use for each item passed to it
	 *
     * @param mixed $item
	 * @return Closure
	 */
	abstract protected function setTemplate($item);

	/**
	 * Refine the item(s) using the set template.
	 * 
	 * @param  mixed $raw
	 * @return mixed
	 */
	public function refine($raw)
	{
		if ( is_array($raw) || $raw instanceof Traversable ) {
			return $this->refineCollection($raw);
		}

		return $this->refineItem($raw);
	}

	/**
	 * Refine a single item using the supplied callback.
	 * 
	 * @param  mixed $raw
	 * @return mixed
	 */
	public function refineItem($raw)
	{
		$refined = $this->setTemplate($raw);

		if ( ! empty($this->attachments)) {
			$refined = $this->merge($refined, $this->includeAttachments($raw));
		}
		
		return $refined;
	}

	/**
	 * Refine a collection of raw items.<commit_msg>Fixed bug with refining arrays<commit_after><?php namespace Michaeljennings\Refinery;

use Closure, Traversable;
use Michaeljennings\Refinery\Exceptions\RefineryMethodNotFound;
use Michaeljennings\Refinery\Exceptions\AttachmentClassNotFound;
use Michaeljennings\Refinery\Contracts\Refinery as RefineryContract;

abstract class Refinery implements RefineryContract {

	/**
	 * The items to be attached to the refined item.
	 * 
	 * @var array
	 */
	protected $attachments = array();

	/**
	 * A filter to run on the raw data.
	 * 
	 * @var callable
	 */
	protected $filter;

	/**
	 * Set the template the refinery will use for each item passed to it
	 *
     * @param mixed $item
	 * @return Closure
	 */
	abstract protected function setTemplate($item);

	/**
	 * Refine the item(s) using the set template.
	 * 
	 * @param  mixed $raw
	 * @return mixed
	 */
	public function refine($raw)
	{
		if ($raw instanceof Traversable) {
			return $this->refineCollection($raw);
		}

		return $this->refineItem($raw);
	}

	/**
	 * Refine a single item using the supplied callback.
	 * 
	 * @param  mixed $raw
	 * @return mixed
	 */
	public function refineItem($raw)
	{
		$refined = $this->setTemplate($raw);

		if ( ! empty($this->attachments)) {
			$refined = $this->merge($refined, $this->includeAttachments($raw));
		}
		
		return $refined;
	}

	/**
	 * Refine a collection of raw items.
{'fraction_non_alphanumeric': 0.1206084751901485, 'fraction_numerical': 0.0, 'mean_word_length': 3.520458265139116, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '61845', 'n_tokens_mistral': 943, 'n_tokens_neox': 838, 'n_words': 246}