Skip to content

Commit 59578c6

Browse files
committed
Fix Some Namespace and Add Fix Enum for Griew Function
1 parent 93d19f7 commit 59578c6

File tree

7 files changed

+94
-34
lines changed

7 files changed

+94
-34
lines changed

src/Commands/MakeFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ public function handle(): int
7272

7373
// Initialize Class
7474
$factoryContent = "<?php\n\nnamespace $factoryNamespace;\n\n";
75-
$factoryContent .= "use App\Models\Entities\\$entityName;\nuse stdClass;\n\n";
75+
$factoryContent .= "use stdClass;";
76+
$factoryContent .= "\nuse App\Models\Entities\\$entityName;";
77+
$factoryContent .= "\nuse Nanvaie\DatabaseRepository\Models\Factory\Factory;\n\n";
7678
$factoryContent .= "class $factoryName extends Factory\n{\n";
7779

7880
// Create "makeEntityFromStdClass" Function

src/Commands/MakeMySqlRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function handle(): int
117117
$mysqlRepositoryContent = "<?php\n\nnamespace $mysqlRepositoryNamespace\\$entityName;\n\n";
118118
$mysqlRepositoryContent .= "use App\Models\Entities\\$entityName;\n";
119119
$mysqlRepositoryContent .= "use App\Models\Factories\\$factoryName;\n";
120-
$mysqlRepositoryContent .= "use App\Models\Repositories\MySqlRepository;\n";
120+
$mysqlRepositoryContent .= "use Nanvaie\DatabaseRepository\Models\Repository\MySqlRepository;\n";
121121
$mysqlRepositoryContent .= "use Illuminate\Support\Collection;\n\n";
122122
$mysqlRepositoryContent .= "class $mysqlRepositoryName extends MySqlRepository implements $interfaceName\n{";
123123

src/Models/Enums/Enum.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Nanvaie\DatabaseRepository\Models\Enums;
4+
5+
use ReflectionClass;
6+
7+
abstract class Enum
8+
{
9+
public function getList(): array
10+
{
11+
return (new ReflectionClass($this))->getConstants();
12+
}
13+
14+
public function getValue(int|string $key): ?string
15+
{
16+
$list = $this->getList();
17+
$keys = array_keys($list);
18+
$key = is_numeric($key) ? (int)$key : $key;
19+
20+
if (is_int($key) && $key < count($keys)) {
21+
$value = $list[$keys[$key]];
22+
} else {
23+
$value = $list[strtoupper($key)];
24+
}
25+
26+
return $value;
27+
}
28+
29+
public function indexOf(int|string $value): false|int|string
30+
{
31+
$list = $this->getList();
32+
$values = array_values($list);
33+
34+
return array_search($value, $values, true);
35+
}
36+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Nanvaie\DatabaseRepository\Models\Enums;
4+
5+
class GriewFilterOperator extends Enum
6+
{
7+
public const IS_EQUAL_TO = 'is_equal_to';
8+
public const IS_EQUAL_TO_OR_NULL = 'is_equal_to_or_null';
9+
public const IS_NOT_EQUAL_TO = 'is_not_equal_to';
10+
public const IS_NULL = 'is_null';
11+
public const IS_NOT_NULL = 'is_not_null';
12+
public const START_WITH = 'start_with';
13+
public const DOES_NOT_CONTAINS = 'does_not_contains';
14+
public const CONTAINS = 'contains';
15+
public const ENDS_WITH = 'ends_with';
16+
public const IN = 'in';
17+
public const NOT_IN = 'not_In';
18+
public const BETWEEN = 'between';
19+
public const IS_GREATER_THAN_OR_EQUAL_TO = 'is_greater_than_or_equal_to';
20+
public const IS_GREATER_THAN = 'is_greater_than';
21+
public const IS_LESS_THAN_OR_EQUAL_TO = 'is_less_than_or_equal_to';
22+
public const IS_LESS_THAN = 'is_less_than';
23+
public const IS_AFTER_THAN_OR_EQUAL_TO = 'is_after_than_or_equal_to';
24+
public const IS_AFTER_THAN = 'is_after_than';
25+
public const IS_BEFORE_THAN_OR_EQUAL_TO = 'is_Before_than_or_equal_to';
26+
public const IS_BEFORE_THAN = 'is_before_than';
27+
// public const IS_INSIDE_POLYGON = 'is_inside_polygon';
28+
// public const IS_NEAR_TO_POINT = 'is_near_to_point';
29+
}

src/Models/Factory/Factory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
namespace Nanvaie\DatabaseRepository\Models\Factory;
44

5-
use App\Models\Entities\Entity;
5+
use Nanvaie\DatabaseRepository\Models\Entity\Entity;
66
use Illuminate\Support\Collection;
77
use stdClass;
88

99
abstract class Factory implements IFactory
1010
{
11-
public abstract function makeEntityFromStdClass(stdClass $entity): Entity;
11+
abstract public function makeEntityFromStdClass(stdClass $entity): Entity;
1212

1313
public function makeCollectionOfEntities(Collection|array $entities): Collection
1414
{

src/Models/Factory/IFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Nanvaie\DatabaseRepository\Models\Factory;
44

5-
use App\Models\Entities\Entity;
5+
use Nanvaie\DatabaseRepository\Models\Entity\Entity;
66
use Illuminate\Support\Collection;
77
use stdClass;
88

src/Models/Repository/MySqlRepository.php

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
namespace Nanvaie\DatabaseRepository\Models\Repository;
44

5-
use App\Models\Entities\Entity;
6-
use App\Models\Factories\IFactory;
7-
use App\Models\General\Polygon;
8-
use App\Models\Griew\FilterOperator;
5+
use Nanvaie\DatabaseRepository\Models\Entity\Entity;
6+
use Nanvaie\DatabaseRepository\Models\Factory\IFactory;
7+
use Nanvaie\DatabaseRepository\Models\Enums\GriewFilterOperator;
98
use Illuminate\Database\ConnectionInterface;
109
use Illuminate\Database\Query\Builder;
1110
use Illuminate\Support\Collection;
@@ -209,13 +208,13 @@ protected function processFilter(Builder $query, array $filters): Builder
209208
{
210209
foreach ($filters as $filter) {
211210
switch (strtolower(snake_case($filter->operator))) {
212-
case FilterOperator::IS_NULL:
211+
case GriewFilterOperator::IS_NULL:
213212
$query->whereNull($filter->name);
214213
break;
215-
case FilterOperator::IS_NOT_NULL:
214+
case GriewFilterOperator::IS_NOT_NULL:
216215
$query->whereNotNull($filter->name);
217216
break;
218-
case FilterOperator::IS_EQUAL_TO:
217+
case GriewFilterOperator::IS_EQUAL_TO:
219218
if (is_string($filter->operand1) && Str::contains($filter->operand1, '|')) {
220219
// create in functionality with equal string
221220
$arr = array_filter(explode('|', $filter->operand1));
@@ -224,7 +223,7 @@ protected function processFilter(Builder $query, array $filters): Builder
224223
$query->where($filter->name, '=', $filter->operand1);
225224
}
226225
break;
227-
case FilterOperator::IS_NOT_EQUAL_TO:
226+
case GriewFilterOperator::IS_NOT_EQUAL_TO:
228227
if (is_string($filter->operand1) && Str::contains($filter->operand1, '|')) {
229228
// create in functionality with equal string
230229
$arr = array_filter(explode('|', $filter->operand1));
@@ -233,49 +232,43 @@ protected function processFilter(Builder $query, array $filters): Builder
233232
$query->where($filter->name, '<>', $filter->operand1);
234233
}
235234
break;
236-
case FilterOperator::START_WITH:
235+
case GriewFilterOperator::START_WITH:
237236
$query->where($filter->name, 'LIKE', $filter->operand1 . '%');
238237
break;
239-
case FilterOperator::DOES_NOT_CONTAINS:
238+
case GriewFilterOperator::DOES_NOT_CONTAINS:
240239
$query->where($filter->name, 'NOT LIKE', '%' . $filter->operand1 . '%');
241240
break;
242-
case FilterOperator::CONTAINS:
241+
case GriewFilterOperator::CONTAINS:
243242
$query->where($filter->name, 'LIKE', '%' . $filter->operand1 . '%');
244243
break;
245-
case FilterOperator::ENDS_WITH:
244+
case GriewFilterOperator::ENDS_WITH:
246245
$query->where($filter->name, 'LIKE', '%' . $filter->operand1);
247246
break;
248-
case FilterOperator::IN:
247+
case GriewFilterOperator::IN:
249248
$query->whereIn($filter->name, $filter->operand1);
250249
break;
251-
case FilterOperator::NOT_IN:
250+
case GriewFilterOperator::NOT_IN:
252251
$query->whereNotIn($filter->name, $filter->operand1);
253252
break;
254-
case FilterOperator::BETWEEN:
253+
case GriewFilterOperator::BETWEEN:
255254
$query->whereBetween($filter->name, array($filter->operand1, $filter->operand2));
256255
break;
257-
case FilterOperator::IS_AFTER_THAN_OR_EQUAL_TO:
258-
case FilterOperator::IS_GREATER_THAN_OR_EQUAL_TO:
256+
case GriewFilterOperator::IS_AFTER_THAN_OR_EQUAL_TO:
257+
case GriewFilterOperator::IS_GREATER_THAN_OR_EQUAL_TO:
259258
$query->where($filter->name, '>=', $filter->operand1);
260259
break;
261-
case FilterOperator::IS_AFTER_THAN:
262-
case FilterOperator::IS_GREATER_THAN:
260+
case GriewFilterOperator::IS_AFTER_THAN:
261+
case GriewFilterOperator::IS_GREATER_THAN:
263262
$query->where($filter->name, '>', $filter->operand1);
264263
break;
265-
case FilterOperator::IS_LESS_THAN_OR_EQUAL_TO:
266-
case FilterOperator::IS_BEFORE_THAN_OR_EQUAL_TO:
264+
case GriewFilterOperator::IS_LESS_THAN_OR_EQUAL_TO:
265+
case GriewFilterOperator::IS_BEFORE_THAN_OR_EQUAL_TO:
267266
$query->where($filter->name, '<=', $filter->operand1);
268267
break;
269-
case FilterOperator::IS_LESS_THAN:
270-
case FilterOperator::IS_BEFORE_THAN:
268+
case GriewFilterOperator::IS_LESS_THAN:
269+
case GriewFilterOperator::IS_BEFORE_THAN:
271270
$query->where($filter->name, '<', $filter->operand1);
272271
break;
273-
case FilterOperator::IS_INSIDE_POLYGON:
274-
$name = $filter->name;
275-
/** @var Polygon $polygon */
276-
$polygon = $filter->operand1;
277-
$query->whereRaw("Contains(GeomFromText('{$polygon->toRaw()}'),{$name})");
278-
break;
279272
}
280273
}
281274

0 commit comments

Comments
 (0)