<commit_before><?php

namespace Recca0120\Repository\Compilers;

use BadMethodCallException;

class CollectionCompiler extends Compiler
{
    /**
     * where.
     *
     * @method where
     *
     * @param mixed $model
     * @param array $actions
     *
     * @return mixed
     */
    public function where($model, $actions)
    {
        $filters = [];
        foreach ($actions as $action) {
            $method = $action['method'];
            $parameters = $action['parameters'];
            if (count($parameters) === 3) {
                list($key, $operator, $value) = $parameters;
            } else {
                list($key, $value) = $parameters;
                $operator = '=';
            }
            if ($value instanceof Expression) {
                $value = $value->getValue();
            }

            $filters[$method][] = [$key, $operator, $value];
        }

        $model = $model->filter(function ($item) use ($filters) {
            foreach ($filters as $method => $rules) {
                switch ($method) {
                    case 'where':
                    case 'having':
                        foreach ($rules as $parameters) {
                            if ($this->checkOperator($item, $parameters) === false) {
                                return false;
                            }
                        }
                        break;
                    default:
                        throw new BadMethodCallException('Call to undefined method '.static::class."::{$method}()");
                        break;
                }
            }

            return true;
        });

        return $model;
    }

    protected function checkOperator($item, $parameters)
    {
        list($key, $operator, $value) = $parameters;
        $retrieved = $item[$key];
        switch ($operator) {
            case '=':
            case '==':  return $retrieved == $value;
            case '!=':
            case '<>':  return $retrieved != $value;
            case '<':   return $retrieved < $value;
            case '>':   return $retrieved > $value;
            case '<=':  return $retrieved <= $value;
            case '>=':  return $retrieved >= $value;
            case '===': return $retrieved === $value;
            case '!==': return $retrieved !== $value;
        }

        return false;
    }

    /**
     * having.
     *
     * @method having
     *
     * @param mixed $model
     * @param array $actions
     *
     * @return mixed
     */
    public function having($model, $actions)
    {
        return $this->where($model, $actions);
    }

    /**
     * order.
     *
     * @method order
     *
     * @param mixed $model
     * @param array $actions
     *
     * @return mixed
     */
    public function order($model, $actions)
    {
        $sort = [];
        foreach ($actions as $action) {
            $parameters = $action['parameters'];
            $sort[$parameters[0]] = array_get($parameters, 1, 'asc');
        }

        return $model->sort($this->orderComparer($sort));
    }<commit_msg>Arr::get<commit_after><?php

namespace Recca0120\Repository\Compilers;

use BadMethodCallException;
use Illuminate\Support\Arr;

class CollectionCompiler extends Compiler
{
    /**
     * where.
     *
     * @method where
     *
     * @param mixed $model
     * @param array $actions
     *
     * @return mixed
     */
    public function where($model, $actions)
    {
        $filters = [];
        foreach ($actions as $action) {
            $method = $action['method'];
            $parameters = $action['parameters'];
            if (count($parameters) === 3) {
                list($key, $operator, $value) = $parameters;
            } else {
                list($key, $value) = $parameters;
                $operator = '=';
            }
            if ($value instanceof Expression) {
                $value = $value->getValue();
            }

            $filters[$method][] = [$key, $operator, $value];
        }

        $model = $model->filter(function ($item) use ($filters) {
            foreach ($filters as $method => $rules) {
                switch ($method) {
                    case 'where':
                    case 'having':
                        foreach ($rules as $parameters) {
                            if ($this->checkOperator($item, $parameters) === false) {
                                return false;
                            }
                        }
                        break;
                    default:
                        throw new BadMethodCallException('Call to undefined method '.static::class."::{$method}()");
                        break;
                }
            }

            return true;
        });

        return $model;
    }

    protected function checkOperator($item, $parameters)
    {
        list($key, $operator, $value) = $parameters;
        $retrieved = $item[$key];
        switch ($operator) {
            case '=':
            case '==':  return $retrieved == $value;
            case '!=':
            case '<>':  return $retrieved != $value;
            case '<':   return $retrieved < $value;
            case '>':   return $retrieved > $value;
            case '<=':  return $retrieved <= $value;
            case '>=':  return $retrieved >= $value;
            case '===': return $retrieved === $value;
            case '!==': return $retrieved !== $value;
        }

        return false;
    }

    /**
     * having.
     *
     * @method having
     *
     * @param mixed $model
     * @param array $actions
     *
     * @return mixed
     */
    public function having($model, $actions)
    {
        return $this->where($model, $actions);
    }

    /**
     * order.
     *
     * @method order
     *
     * @param mixed $model
     * @param array $actions
     *
     * @return mixed
     */
    public function order($model, $actions)
    {
        $sort = [];
        foreach ($actions as $action) {
            $parameters = $action['parameters'];
            $sort[$parameters[0]] = Arr::get($parameters, 1, 'asc');
        }

        return $model->sort($this->orderComparer($sort));
    }
{'fraction_non_alphanumeric': 0.14283403429310904, 'fraction_numerical': 0.002264639275315432, 'mean_word_length': 1.3937282229965158, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 27, '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': '5155220', 'n_tokens_mistral': 1667, 'n_tokens_neox': 1592, 'n_words': 380}