<commit_before><?php

namespace allejo\stakx\Twig;

use allejo\stakx\Object\FrontMatterObject;
use Twig_Error_Syntax;

class WhereFilter
{
    public function __invoke ($array, $key, $comparison, $value)
    {
        $results = array();
        $this->search_r($array, $key, $comparison, $value, $results);

        return $results;
    }

    public static function get ()
    {
        return new \Twig_SimpleFilter('where', new self());
    }

    private function search_r ($array, $key, $comparison, $value, &$results)
    {
        if (!is_array($array) && !($array instanceof FrontMatterObject))
        {
            return;
        }

        if ($this->compare($array, $key, $comparison, $value))
        {
            $results[] = $array;
        }

        foreach ($array as $subarray)
        {
            $this->search_r($subarray, $key, $comparison, $value, $results);
        }
    }

    private function compare ($array, $key, $comparison, $value)
    {
        $fm = false;

        if ($array instanceof FrontMatterObject)
        {
            $array = $array->getFrontMatter();
            $fm = true;
        }

        if (!isset($array[$key]))
        {
            return ($fm && $comparison === "==" && $value === null);
        }

        switch ($comparison)
        {<commit_msg>Doc updates to Where filter<commit_after><?php

namespace allejo\stakx\Twig;

use allejo\stakx\Object\FrontMatterObject;
use Twig_Error_Syntax;

class WhereFilter
{
    /**
     * @param  array|FrontMatterObject[] $array      The elements to filter through
     * @param  string                    $key        The key value in an associative array or FrontMatter
     * @param  string                    $comparison The actual comparison symbols being used
     * @param  mixed                     $value      The value we're searching for
     *
     * @return array
     */
    public function __invoke ($array, $key, $comparison, $value)
    {
        $results = array();
        $this->search_r($array, $key, $comparison, $value, $results);

        return $results;
    }

    /**
     * @return \Twig_SimpleFilter
     */
    public static function get ()
    {
        return new \Twig_SimpleFilter('where', new self());
    }

    /**
     * Recursive searching calling our comparison
     *
     * @param array|FrontMatterObject[] $array      The elements to filter through
     * @param string                    $key        The key value in an associative array or FrontMatter
     * @param string                    $comparison The actual comparison symbols being used
     * @param string                    $value      The value we're searching for
     * @param array                     $results    The reference to where to keep the filtered elements
     */
    private function search_r ($array, $key, $comparison, $value, &$results)
    {
        if (!is_array($array) && !($array instanceof FrontMatterObject))
        {
            return;
        }

        if ($this->compare($array, $key, $comparison, $value))
        {
            $results[] = $array;
        }

        foreach ($array as $subarray)
        {
            $this->search_r($subarray, $key, $comparison, $value, $results);
        }
    }

    /**
     * The logic for determining if an element matches the filter
     *
     * @param  array|FrontMatterObject[] $array      The elements to filter through
     * @param  string                    $key        The key value in an associative array or FrontMatter
     * @param  string                    $comparison The actual comparison symbols being used
     * @param  mixed                     $value      The value we're searching for
     *
     * @return bool
     *
     * @throws Twig_Error_Syntax
     */
    private function compare ($array, $key, $comparison, $value)
    {
        $fm = false;

        if ($array instanceof FrontMatterObject)
        {
            $array = $array->getFrontMatter();
            $fm = true;
        }

        if (!isset($array[$key]))
        {
            return ($fm && $comparison === "==" && $value === null);
        }

        switch ($comparison)
        {
{'fraction_non_alphanumeric': 0.11506125390343502, 'fraction_numerical': 0.0, 'mean_word_length': 1.8307273963290278, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '7286599', 'n_tokens_mistral': 1165, 'n_tokens_neox': 1111, 'n_words': 330}