Completed
Push — 2.0 ( 43165f...27f708 )
by Kirill
03:01
created

AbstractQuery::queryArguments()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 1
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of laravel.su package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace App\GraphQL\Queries;
10
11
use Folklore\GraphQL\Support\Query;
12
use App\GraphQL\Kernel\HasValidation;
13
use Illuminate\Database\Eloquent\Model;
14
use Illuminate\Database\Eloquent\Builder;
15
16
/**
17
 * Class AbstractQuery.
18
 */
19
abstract class AbstractQuery extends Query
20
{
21
    use HasValidation;
22
23
    /**
24
     * @return array
25
     */
26
    public function args(): array
27
    {
28
        return $this->queryArguments();
29
    }
30
31
    /**
32
     * @return array
33
     */
34
    abstract protected function queryArguments(): array;
35
36
    /**
37
     * @param string $model
38
     * @param array  $args
39
     * @return Builder|Model <T>
40
     * @internal param $ string|Model|Model<T> $model
41
     */
42
    protected function queryFor(string $model, array $args = []): Builder
43
    {
44
        $query = $model::query();
45
46
        foreach ($args as $field => $value) {
47
            $query = $query->where($field, $value);
48
        }
49
50
        return $query;
51
    }
52
53
    /**
54
     * @param array $args
55
     * @param string $name
56
     * @param \Closure $then
57
     * @return mixed
58
     */
59
    protected function whenExists(array $args, string $name, \Closure $then)
60
    {
61
        if (isset($args[$name])) {
62
            return $then($args[$name]);
63
        }
64
65
        return null;
66
    }
67
}
68