<commit_before><?php

declare(strict_types=1);

namespace AlgoWeb\PODataLaravel\Query;

use AlgoWeb\PODataLaravel\Enums\ActionVerb;
use AlgoWeb\PODataLaravel\Models\MetadataTrait;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Facades\App;
use POData\Common\InvalidOperationException;
use POData\Common\ODataException;
use POData\Providers\Metadata\ResourceProperty;
use POData\Providers\Metadata\ResourceSet;
use POData\Providers\Query\QueryResult;
use POData\Providers\Query\QueryType;
use POData\UriProcessor\QueryProcessor\ExpressionParser\FilterInfo;
use POData\UriProcessor\QueryProcessor\OrderByParser\InternalOrderByInfo;
use POData\UriProcessor\QueryProcessor\SkipTokenParser\SkipTokenInfo;
use POData\UriProcessor\ResourcePathProcessor\SegmentParser\KeyDescriptor;
use Symfony\Component\Process\Exception\InvalidArgumentException;

class LaravelReadQuery extends LaravelBaseQuery
{
    use LaravelReadQueryUtilityTrait;

    /**
     * Gets collection of entities belongs to an entity set
     * IE: http://host/EntitySet
     *  http://host/EntitySet?$skip=10&$top=5&filter=Prop gt Value.
     *
     * @param QueryType                $queryType            Is this is a query for a count, entities,
     *                                                       or entities-with-count?
     * @param ResourceSet              $resourceSet          The entity set containing the entities to fetch
     * @param FilterInfo|null          $filterInfo           The $filter parameter of the OData query.  NULL if absent
     * @param null|InternalOrderByInfo $orderBy              sorted order if we want to get the data in some
     *                                                       specific order
     * @param int|null                 $top                  number of records which need to be retrieved
     * @param int|null                 $skip                 number of records which need to be skipped
     * @param SkipTokenInfo|null       $skipToken            value indicating what records to skip
     * @param string[]|null            $eagerLoad            array of relations to eager load
     * @param Model|Relation|null      $sourceEntityInstance Starting point of query
     *
     * @throws InvalidArgumentException
     * @throws InvalidOperationException
     * @throws \ReflectionException
     * @throws ODataException
     * @return QueryResult
     */
    public function getResourceSet(
        QueryType $queryType,
        ResourceSet $resourceSet,
        FilterInfo $filterInfo = null,
        $orderBy = null,
        $top = null,
        $skip = null,
        SkipTokenInfo $skipToken = null,
        array $eagerLoad = null,
        $sourceEntityInstance = null
    ) {
        $rawLoad = $this->processEagerLoadList($eagerLoad);

        $sourceEntityInstance = $this->checkSourceInstance($sourceEntityInstance, $resourceSet);

        /** @var MetadataTrait $model */
        $model     = $sourceEntityInstance instanceof Model ? $sourceEntityInstance : $sourceEntityInstance->getRelated();
        $modelLoad = $model->getEagerLoad();
        $tableName = $model->getTable();
        $rawLoad   = array_unique(array_merge($rawLoad, $modelLoad));

        $checkInstance = $sourceEntityInstance instanceof Model ? $sourceEntityInstance : null;
        $this->checkAuth($sourceEntityInstance, $checkInstance);

        $result          = new QueryResult();
        $result->results = null;
        $result->count   = null;

        $sourceEntityInstance = $this->buildOrderBy($sourceEntityInstance, $tableName, $orderBy);

        // throttle up for trench run
        if (null != $skipToken) {
            $sourceEntityInstance = $this->processSkipToken($skipToken, $sourceEntityInstance);
        }

        if (!isset($skip)) {
            $skip = 0;
        }
        if (!isset($top)) {
            $top = PHP_INT_MAX;
        }

        $nullFilter = !isset($filterInfo);
        $isvalid    = null;
        if (isset($filterInfo)) {
            $method  = 'return ' . $filterInfo->getExpressionAsString() . ';';
            $clln    = $resourceSet->getResourceType()->getName();
            $isvalid = function ($inputD) use ($clln, $method) {
                ${$clln} = $inputD;
                return eval($method);
            };
        }

        list($bulkSetCount, $resultSet, $resultCount, $skip) = $this->applyFiltering(
            $sourceEntityInstance,
            $nullFilter,
            $rawLoad,
            $top,
            $skip,
            $isvalid
        );

        if (isset($top)) {
            $resultSet = $resultSet->take($top);
        }

        $this->packageResourceSetResults($queryType, $skip, $result, $resultSet, $resultCount, $bulkSetCount);
        return $result;
    }

    /**
     * Get related resource set for a resource
     * IE: http://host/EntitySet(1L)/NavigationPropertyToCollection
     * http://host/EntitySet?$expand=NavigationPropertyToCollection.
     *
     * @param QueryType          $queryType            Is this is a query for a count, entities, or entities-with-count
     * @param ResourceSet        $sourceResourceSet    The entity set containing the source entity
     * @param Model              $sourceEntityInstance The source entity instance
     * @param ResourceSet        $targetResourceSet    The resource set pointed to by the navigation property
     * @param ResourceProperty   $targetProperty       The navigation property to retrieve
     * @param FilterInfo|null    $filter               The $filter parameter of the OData query.  NULL if none specified
     * @param mixed|null         $orderBy              sorted order if we want to get the data in some specific order
     * @param int|null           $top                  number of records which need to be retrieved
     * @param int|null           $skip                 number of records which need to be skipped
     * @param SkipTokenInfo|null $skipToken            value indicating what records to skip
     *
     * @throws InvalidOperationException
     * @throws ODataException
     * @throws \ReflectionException
     * @return QueryResult
     */
    public function getRelatedResourceSet(<commit_msg>Rip out always-true check in LaravelReadQuery<commit_after><?php

declare(strict_types=1);

namespace AlgoWeb\PODataLaravel\Query;

use AlgoWeb\PODataLaravel\Enums\ActionVerb;
use AlgoWeb\PODataLaravel\Models\MetadataTrait;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Facades\App;
use POData\Common\InvalidOperationException;
use POData\Common\ODataException;
use POData\Providers\Metadata\ResourceProperty;
use POData\Providers\Metadata\ResourceSet;
use POData\Providers\Query\QueryResult;
use POData\Providers\Query\QueryType;
use POData\UriProcessor\QueryProcessor\ExpressionParser\FilterInfo;
use POData\UriProcessor\QueryProcessor\OrderByParser\InternalOrderByInfo;
use POData\UriProcessor\QueryProcessor\SkipTokenParser\SkipTokenInfo;
use POData\UriProcessor\ResourcePathProcessor\SegmentParser\KeyDescriptor;
use Symfony\Component\Process\Exception\InvalidArgumentException;

class LaravelReadQuery extends LaravelBaseQuery
{
    use LaravelReadQueryUtilityTrait;

    /**
     * Gets collection of entities belongs to an entity set
     * IE: http://host/EntitySet
     *  http://host/EntitySet?$skip=10&$top=5&filter=Prop gt Value.
     *
     * @param QueryType                $queryType            Is this is a query for a count, entities,
     *                                                       or entities-with-count?
     * @param ResourceSet              $resourceSet          The entity set containing the entities to fetch
     * @param FilterInfo|null          $filterInfo           The $filter parameter of the OData query.  NULL if absent
     * @param null|InternalOrderByInfo $orderBy              sorted order if we want to get the data in some
     *                                                       specific order
     * @param int|null                 $top                  number of records which need to be retrieved
     * @param int|null                 $skip                 number of records which need to be skipped
     * @param SkipTokenInfo|null       $skipToken            value indicating what records to skip
     * @param string[]|null            $eagerLoad            array of relations to eager load
     * @param Model|Relation|null      $sourceEntityInstance Starting point of query
     *
     * @throws InvalidArgumentException
     * @throws InvalidOperationException
     * @throws \ReflectionException
     * @throws ODataException
     * @return QueryResult
     */
    public function getResourceSet(
        QueryType $queryType,
        ResourceSet $resourceSet,
        FilterInfo $filterInfo = null,
        $orderBy = null,
        $top = null,
        $skip = null,
        SkipTokenInfo $skipToken = null,
        array $eagerLoad = null,
        $sourceEntityInstance = null
    ) {
        $rawLoad = $this->processEagerLoadList($eagerLoad);

        $sourceEntityInstance = $this->checkSourceInstance($sourceEntityInstance, $resourceSet);

        /** @var MetadataTrait $model */
        $model     = $sourceEntityInstance instanceof Model ? $sourceEntityInstance : $sourceEntityInstance->getRelated();
        $modelLoad = $model->getEagerLoad();
        $tableName = $model->getTable();
        $rawLoad   = array_unique(array_merge($rawLoad, $modelLoad));

        $checkInstance = $sourceEntityInstance instanceof Model ? $sourceEntityInstance : null;
        $this->checkAuth($sourceEntityInstance, $checkInstance);

        $result          = new QueryResult();
        $result->results = null;
        $result->count   = null;

        $sourceEntityInstance = $this->buildOrderBy($sourceEntityInstance, $tableName, $orderBy);

        // throttle up for trench run
        if (null != $skipToken) {
            $sourceEntityInstance = $this->processSkipToken($skipToken, $sourceEntityInstance);
        }

        if (!isset($skip)) {
            $skip = 0;
        }
        if (!isset($top)) {
            $top = PHP_INT_MAX;
        }

        $nullFilter = !isset($filterInfo);
        $isvalid    = null;
        if (isset($filterInfo)) {
            $method  = 'return ' . $filterInfo->getExpressionAsString() . ';';
            $clln    = $resourceSet->getResourceType()->getName();
            $isvalid = function ($inputD) use ($clln, $method) {
                ${$clln} = $inputD;
                return eval($method);
            };
        }

        list($bulkSetCount, $resultSet, $resultCount, $skip) = $this->applyFiltering(
            $sourceEntityInstance,
            $nullFilter,
            $rawLoad,
            $top,
            $skip,
            $isvalid
        );

        $resultSet = $resultSet->take($top);

        $this->packageResourceSetResults($queryType, $skip, $result, $resultSet, $resultCount, $bulkSetCount);
        return $result;
    }

    /**
     * Get related resource set for a resource
     * IE: http://host/EntitySet(1L)/NavigationPropertyToCollection
     * http://host/EntitySet?$expand=NavigationPropertyToCollection.
     *
     * @param QueryType          $queryType            Is this is a query for a count, entities, or entities-with-count
     * @param ResourceSet        $sourceResourceSet    The entity set containing the source entity
     * @param Model              $sourceEntityInstance The source entity instance
     * @param ResourceSet        $targetResourceSet    The resource set pointed to by the navigation property
     * @param ResourceProperty   $targetProperty       The navigation property to retrieve
     * @param FilterInfo|null    $filter               The $filter parameter of the OData query.  NULL if none specified
     * @param mixed|null         $orderBy              sorted order if we want to get the data in some specific order
     * @param int|null           $top                  number of records which need to be retrieved
     * @param int|null           $skip                 number of records which need to be skipped
     * @param SkipTokenInfo|null $skipToken            value indicating what records to skip
     *
     * @throws InvalidOperationException
     * @throws ODataException
     * @throws \ReflectionException
     * @return QueryResult
     */
    public function getRelatedResourceSet(
{'fraction_non_alphanumeric': 0.08793633283429202, 'fraction_numerical': 0.0009455519659601293, 'mean_word_length': 2.363901404717731, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5300392', 'n_tokens_mistral': 3262, 'n_tokens_neox': 3214, 'n_words': 864}