<commit_before><?php

namespace Orchid\Foundation\Http\Controllers\Api;

use Illuminate\Http\Request;
use Orchid\Foundation\Core\Models\Post;
use Orchid\Foundation\Facades\Dashboard;
use Orchid\Foundation\Filters\BetweenFilter;
use Orchid\Foundation\Filters\ContentFilters;
use Orchid\Foundation\Filters\LikeFilters;
use Orchid\Foundation\Filters\LimitFilters;
use Orchid\Foundation\Filters\WhereFilters;
use Orchid\Foundation\Http\Controllers\Controller;

class PostApiController extends Controller
{
    /**
     * @var array Active filters set
     */
    public $filters = [
        'eq'      => WhereFilters::class,
        'count'   => LimitFilters::class,
        'between' => BetweenFilter::class,
        'search'  => LikeFilters::class,
        'content' => ContentFilters::class,
    ];

    /**
     * @param $type
     * @param $fields
     * @return mixed
     */
    public function index($type, $fields)
    {
        $builder = $type->filters($fields);

        $posts = $builder->get();

        return response()->json($posts);
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function store(Request $request)
    {
        $model = $this->resolveModel($request);

        $builder = new $model();

        $fields = $request->get('fields');
        if ($fields != null) {
            $builder = $this->applyFieldFilters($builder, $fields);
        }

        $content = $request->get('content');

        if ($content != null) {
            $builder = $this->applyContentFilters($builder, $content);
        }

        $posts = $builder->get();

        return response()->json($posts);
    }

    /**
     * @param Post $post
     * @param Request $request
     * @return mixed
     */
    public function show(Post $post, Request $request)
    {
        return $this->index($post, $request);
    }


    /**
     * @param $post
     * @param $fields
     * @return mixed
     */
    public function applyFieldFilters($post, $fields)
    {
        $fieldFilters = Dashboard::getFieldFilters();

        foreach ($fields as $fieldName => $filterDescriptor) {
            foreach ($filterDescriptor as $filterName => $filterParameters) {
                $filterClass = $fieldFilters->get($filterName);

                if ($filterClass != null) {
                    $filter = new $filterClass($post, $fieldName, $filterParameters);
                    $post = $filter->run();
                }
            }
        }

        return $post;
    }


    /**
     * @param Request $request<commit_msg>Apply fixes from StyleCI (#122)<commit_after><?php

namespace Orchid\Foundation\Http\Controllers\Api;

use Illuminate\Http\Request;
use Orchid\Foundation\Core\Models\Post;
use Orchid\Foundation\Facades\Dashboard;
use Orchid\Foundation\Filters\BetweenFilter;
use Orchid\Foundation\Filters\ContentFilters;
use Orchid\Foundation\Filters\LikeFilters;
use Orchid\Foundation\Filters\LimitFilters;
use Orchid\Foundation\Filters\WhereFilters;
use Orchid\Foundation\Http\Controllers\Controller;

class PostApiController extends Controller
{
    /**
     * @var array Active filters set
     */
    public $filters = [
        'eq'      => WhereFilters::class,
        'count'   => LimitFilters::class,
        'between' => BetweenFilter::class,
        'search'  => LikeFilters::class,
        'content' => ContentFilters::class,
    ];

    /**
     * @param $type
     * @param $fields
     *
     * @return mixed
     */
    public function index($type, $fields)
    {
        $builder = $type->filters($fields);

        $posts = $builder->get();

        return response()->json($posts);
    }

    /**
     * @param Request $request
     *
     * @return mixed
     */
    public function store(Request $request)
    {
        $model = $this->resolveModel($request);

        $builder = new $model();

        $fields = $request->get('fields');
        if ($fields != null) {
            $builder = $this->applyFieldFilters($builder, $fields);
        }

        $content = $request->get('content');

        if ($content != null) {
            $builder = $this->applyContentFilters($builder, $content);
        }

        $posts = $builder->get();

        return response()->json($posts);
    }

    /**
     * @param Post    $post
     * @param Request $request
     *
     * @return mixed
     */
    public function show(Post $post, Request $request)
    {
        return $this->index($post, $request);
    }

    /**
     * @param $post
     * @param $fields
     *
     * @return mixed
     */
    public function applyFieldFilters($post, $fields)
    {
        $fieldFilters = Dashboard::getFieldFilters();

        foreach ($fields as $fieldName => $filterDescriptor) {
            foreach ($filterDescriptor as $filterName => $filterParameters) {
                $filterClass = $fieldFilters->get($filterName);

                if ($filterClass != null) {
                    $filter = new $filterClass($post, $fieldName, $filterParameters);
                    $post = $filter->run();
                }
            }
        }

        return $post;
    }

    /**
     * @param Request $request
     *
{'fraction_non_alphanumeric': 0.140534262485482, 'fraction_numerical': 0.0005807200929152149, 'mean_word_length': 2.19344870210136, 'pattern_counts': {'":': 0, '<': 5, '<?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': '674805', 'n_tokens_mistral': 1474, 'n_tokens_neox': 1452, 'n_words': 299}