Skip to content

Commit

Permalink
Update:refactor the core code with resolver db and eloquent
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdi-fathi committed Aug 14, 2024
1 parent 233fcb2 commit c163101
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 100 deletions.
4 changes: 1 addition & 3 deletions src/QueryFilter/Core/FilterBuilder/Core/QueryFilterCore.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ interface QueryFilterCore
public function __construct(array $defaultSeriesInjected, array $detectInjected = null, MainBuilderConditionsContract $mainBuilderConditions);

/**
* @param array|null $default_detect
* @param array|null $detectInjected
* @return mixed
*/
public function getDetectorFactory(array $default_detect = null, array $detectInjected = null): DetectorFactoryContract;
public function getDetectorFactory(): DetectorFactoryContract;

/**
* @param $default_detect
Expand Down
63 changes: 47 additions & 16 deletions src/QueryFilter/Core/FilterBuilder/Core/QueryFilterCoreBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use eloquentFilter\QueryFilter\Detection\ConditionsDetect\DB\DBBuilderQueryByCondition;
use eloquentFilter\QueryFilter\Detection\ConditionsDetect\TypeQueryConditions\WhereCondition;
use eloquentFilter\QueryFilter\Detection\Contract\DetectorDbFactoryContract;
use eloquentFilter\QueryFilter\Detection\Contract\DetectorFactoryContract;
use eloquentFilter\QueryFilter\Detection\Contract\MainBuilderConditionsContract;
use eloquentFilter\QueryFilter\Detection\DetectionFactory\DetectionDbFactory;
Expand Down Expand Up @@ -34,6 +35,11 @@ class QueryFilterCoreBuilder implements QueryFilterCore
*/
private DetectorFactoryContract $detect_factory;

/**
* @var DetectorDbFactoryContract
*/
private DetectorDbFactoryContract $detect_db_factory;

protected MainBuilderConditionsContract $mainBuilderConditions;

/**
Expand All @@ -51,16 +57,20 @@ public function __construct(array $defaultSeriesInjected, array $detectInjected

$this->setDefaultDetect($defaultSeriesInjected);


if ($mainBuilderConditions->getName() == DBBuilderQueryByCondition::NAME) {

$factories = $this->getDbDetectorFactory($this->getDefaultDetect(), $this->getInjectedDetections());

$this->setDetectDbFactory($factories);

} else {

$factories = $this->getDetectorFactory($this->getDefaultDetect(), $this->getInjectedDetections());

}
$this->setDetectFactory($factories);

$this->setDetectFactory($factories);
}

$this->mainBuilderConditions = $mainBuilderConditions;
}
Expand Down Expand Up @@ -131,6 +141,22 @@ public function getDetectFactory(): DetectorFactoryContract
return $this->detect_factory;
}

/**
* @param \eloquentFilter\QueryFilter\Detection\Contract\DetectorDbFactoryContract $detect_factory
*/
public function setDetectDbFactory(DetectorDBFactoryContract $detect_factory): void
{
$this->detect_db_factory = $detect_factory;
}

/**
* @return \eloquentFilter\QueryFilter\Detection\Contract\DetectorDbFactoryContract
*/
public function getDetectDbFactory(): DetectorDBFactoryContract
{
return $this->detect_db_factory;
}

/**
* @return array
*/
Expand Down Expand Up @@ -166,13 +192,7 @@ public function getInjectedDetections(): mixed
*/
public function getDetectorFactory(array $default_detect = null, array $detectInjected = null): DetectorFactoryContract
{
$detections = $default_detect;

if (!empty($detectInjected)) {
$detections = array_merge($detectInjected, $default_detect);
}

$this->setDetections($detections);
$this->mergeTypesDetections($default_detect, $detectInjected);

return app(DetectionEloquentFactory::class, ['detections' => $this->getDetections()]);
}
Expand All @@ -183,14 +203,9 @@ public function getDetectorFactory(array $default_detect = null, array $detectIn
*
* @return \eloquentFilter\QueryFilter\Detection\DetectionFactory\DetectionDbFactory
*/
public function getDbDetectorFactory(array $default_detect = null, array $detectInjected = null): DetectorFactoryContract
public function getDbDetectorFactory(array $default_detect = null, array $detectInjected = null): DetectorDbFactoryContract
{
$detections = $default_detect;

if (!empty($detectInjected)) {
$detections = array_merge($detectInjected, $default_detect);
}
$this->setDetections($detections);
$this->mergeTypesDetections($default_detect, $detectInjected);

return app(DetectionDbFactory::class, ['detections' => $this->getDetections()]);
}
Expand Down Expand Up @@ -227,4 +242,20 @@ public function setDetectionsDbInjected(?array $injected_detections): void
}
}

/**
* @param array|null $default_detect
* @param array|null $detectInjected
* @return void
*/
private function mergeTypesDetections(?array $default_detect, ?array $detectInjected): void
{
$detections = $default_detect;

if (!empty($detectInjected)) {
$detections = array_merge($detectInjected, $default_detect);
}

$this->setDetections($detections);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace eloquentFilter\QueryFilter\Core\FilterBuilder\QueryBuilder;

use eloquentFilter\QueryFilter\Core\DbBuilder\DbBuilderWrapperInterface;
use eloquentFilter\QueryFilter\Core\ResolverDetections;
use eloquentFilter\QueryFilter\Core\ResolverDetection\ResolverDetectionDb;
use eloquentFilter\QueryFilter\Factory\QueryBuilderWrapperFactory;

/**
Expand Down Expand Up @@ -58,18 +58,18 @@ private function resolveDetections($detections_injected, $black_list_detections)
$this->queryFilterCore->unsetDetection($black_list_detections);
$this->queryFilterCore->setDetectionsDbInjected($detections_injected);

/** @see ResolverDetections */
app()->bind('ResolverDetections', function () {
return new ResolverDetections(
/** @see \eloquentFilter\QueryFilter\Core\ResolverDetection\ResolverDetectionDb */
app()->bind('ResolverDetectionsDb', function () {
return new ResolverDetectionDb(
builder: $this->getQueryBuilderWrapper()->getBuilder(),
request: $this->requestFilter->getRequest(),
detector_factory: $this->queryFilterCore->getDetectFactory(),
detector_db_factory: $this->queryFilterCore->getDetectDbFactory(),
main_builder_conditions_contract: $this->queryFilterCore->getMainBuilderConditions()
);
});

/** @see ResolverDetections::getResolverOut() */
$responseResolver = app('ResolverDetections')->getResolverOut();
/** @see ResolverDetectionDb::getResolverOut() */
$responseResolver = app('ResolverDetectionsDb')->getResolverOut();

$this->responseFilter->setResponse($responseResolver);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
namespace eloquentFilter\QueryFilter\Core\FilterBuilder\QueryBuilder;

use eloquentFilter\QueryFilter\Core\EloquentBuilder\EloquentModelBuilderWrapper;
use eloquentFilter\QueryFilter\Core\ResolverDetections;
use eloquentFilter\QueryFilter\Core\ResolverDetection\ResolverDetectionEloquent;
use eloquentFilter\QueryFilter\Core\ResolverDetection\ResolverDetections;
use eloquentFilter\QueryFilter\Factory\QueryBuilderWrapperFactory;

/**
Expand Down Expand Up @@ -60,18 +61,18 @@ private function resolveDetections($detections_injected, $black_list_detections)

$this->queryFilterCore->setDetectionsInjected($detections_injected);

/** @see ResolverDetections */
app()->bind('ResolverDetections', function () {
return new ResolverDetections(
/** @see ResolverDetectionEloquent */
app()->bind('ResolverDetectionEloquent', function () {
return new ResolverDetectionEloquent(
builder: $this->getQueryBuilderWrapper()->getBuilder(),
request: $this->requestFilter->getRequest(),
detector_factory: $this->queryFilterCore->getDetectFactory(),
main_builder_conditions_contract: $this->queryFilterCore->getMainBuilderConditions()
);
});

/** @see ResolverDetections::getResolverOut() */
$responseResolver = app('ResolverDetections')->getResolverOut();
/** @see ResolverDetectionEloquent::getResolverOut() */
$responseResolver = app('ResolverDetectionEloquent')->getResolverOut();


$this->responseFilter->setResponse($responseResolver);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

namespace eloquentFilter\QueryFilter\Core\FilterBuilder\QueryBuilder;

use eloquentFilter\QueryFilter\Core\EloquentBuilder\EloquentModelBuilderWrapper;
use eloquentFilter\QueryFilter\Core\FilterBuilder\Core\QueryFilterCore;
use eloquentFilter\QueryFilter\Core\FilterBuilder\IO\RequestFilter;
use eloquentFilter\QueryFilter\Core\FilterBuilder\IO\ResponseFilter;
use eloquentFilter\QueryFilter\Core\HelperEloquentFilter;
use eloquentFilter\QueryFilter\Core\ResolverDetections;

/**
* Class QueryFilterBuilder.
Expand Down
61 changes: 61 additions & 0 deletions src/QueryFilter/Core/ResolverDetection/ResolverDetectionDb.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace eloquentFilter\QueryFilter\Core\ResolverDetection;

use eloquentFilter\QueryFilter\Detection\Contract\DetectorDbFactoryContract;
use eloquentFilter\QueryFilter\Detection\Contract\MainBuilderConditionsContract;
use eloquentFilter\QueryFilter\Queries\BaseClause;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\Arr;

class ResolverDetectionDb extends ResolverDetections
{

/**
* ResolverDetectionDb constructor.
* @param $builder
* @param array $request
* @param \eloquentFilter\QueryFilter\Detection\Contract\DetectorDbFactoryContract $detector_db_factory
* @param \eloquentFilter\QueryFilter\Detection\Contract\MainBuilderConditionsContract $main_builder_conditions_contract
*/
public function __construct($builder, array $request, DetectorDbFactoryContract $detector_db_factory, MainBuilderConditionsContract $main_builder_conditions_contract)
{
$this->builder = $builder;
$this->request = $request;
$this->detector_db_factory = $detector_db_factory;
$this->main_builder_conditions = $main_builder_conditions_contract;
}

/**
* @return array
*/
public function getFiltersDetection(): array
{
$filter_detections = collect($this->request)->map(function ($values, $filter){
return $this->resolve($filter, $values);
})->reverse()->filter(function ($item) {
return $item instanceof BaseClause;
})->toArray();

$out = Arr::isAssoc($filter_detections) ? $filter_detections : [];

return $out;
}

/**
* @param $filterName
* @param $values
*
* @return Application|mixed
* @throws ReflectionException
*
*/
protected function resolve($filterName, $values)
{
$detectedConditions = $this->detector_db_factory->buildDetections($filterName, $values);

$builderDriver = $this->main_builder_conditions->build($detectedConditions);

return app($builderDriver, ['filter' => $filterName, 'values' => $values]);
}
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,15 @@
<?php

namespace eloquentFilter\QueryFilter\Core;
namespace eloquentFilter\QueryFilter\Core\ResolverDetection;

use eloquentFilter\QueryFilter\Core\FilterBuilder\MainQueryFilterBuilder;
use eloquentFilter\QueryFilter\Detection\ConditionsDetect\DB\DBBuilderQueryByCondition;
use eloquentFilter\QueryFilter\Detection\Contract\DetectorFactoryContract;
use eloquentFilter\QueryFilter\Detection\Contract\MainBuilderConditionsContract;
use eloquentFilter\QueryFilter\Queries\BaseClause;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Pipeline\Pipeline;
use Illuminate\Support\Arr;

/**
* Class ResolverDetections
* @package eloquentFilter\QueryFilter\Core
*/
class ResolverDetections
class ResolverDetectionEloquent extends ResolverDetections
{
/**
* @var
*/
private $builder;
/**
* @var array
*/
private array $request;
/**
* @var \eloquentFilter\QueryFilter\Detection\Contract\DetectorFactoryContract
*/
private DetectorFactoryContract $detector_factory;


private MainBuilderConditionsContract $main_builder_conditions;

/**
* ResolverDetections constructor.
Expand All @@ -45,21 +23,23 @@ public function __construct($builder, array $request, DetectorFactoryContract $d
$this->builder = $builder;
$this->request = $request;
$this->detector_factory = $detector_factory;

$this->main_builder_conditions = $main_builder_conditions_contract;
}

/**
* @return mixed
* @see MainQueryFilterBuilder
* @return array
*/
public function getResolverOut()
public function getFiltersDetection(): array
{
$filter_detections = $this->getFiltersDetection();
$model = $this->builder->getModel();

$out = app(Pipeline::class)
->send($this->builder)
->through($filter_detections)
->thenReturn();
$filter_detections = collect($this->request)->map(function ($values, $filter) use ($model) {
return $this->resolve($filter, $values, $model);
})->reverse()->filter(function ($item) {
return $item instanceof BaseClause;
})->toArray();

$out = Arr::isAssoc($filter_detections) ? $filter_detections : [];

return $out;
}
Expand All @@ -73,37 +53,12 @@ public function getResolverOut()
* @throws ReflectionException
*
*/
private function resolve($filterName, $values, $model)
protected function resolve($filterName, $values, $model)
{
$detectedConditions = $this->detector_factory->buildDetections($filterName, $values, $model);

$builderDriver = $this->main_builder_conditions->build($detectedConditions);

return app($builderDriver, ['filter' => $filterName, 'values' => $values]);
}

/**
* @return array
*/
private function getFiltersDetection(): array
{
/** @var MainQueryFilterBuilder $eloquentFilter */
$eloquentFilter = app('eloquentFilter');

if ($eloquentFilter->getNameBuilder() != DBBuilderQueryByCondition::NAME) {
$model = $this->builder->getModel();
} else {
$model = $this->builder->from;
}

$filter_detections = collect($this->request)->map(function ($values, $filter) use ($model) {
return $this->resolve($filter, $values, $model);
})->reverse()->filter(function ($item) {
return $item instanceof BaseClause;
})->toArray();

$out = Arr::isAssoc($filter_detections) ? $filter_detections : [];

return $out;
}
}
Loading

0 comments on commit c163101

Please sign in to comment.