Factory::fromDefault()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace EloquentJs\Query\Guard\Policy;
4
5
use InvalidArgumentException;
6
7
class Factory
8
{
9
    /**
10
     * @var mixed
11
     */
12
    protected $defaultPolicy;
13
14
    /**
15
     * Create a new Factory instance.
16
     *
17
     * @param \Closure $defaultPolicy
18
     */
19
    public function __construct($defaultPolicy = null)
20
    {
21
        $this->defaultPolicy = $defaultPolicy;
22
    }
23
24
    /**
25
     * Make a Policy object.
26
     *
27
     * @param string|array|callable|null $rules
28
     * @return Policy
29
     */
30
    public static function make($rules)
31
    {
32
        $factory = app(__CLASS__);
33
34
        if (is_string($rules)) {
35
            return $factory->fromPattern($rules);
36
        }
37
38
        if (is_array($rules)) {
39
            return $factory->fromArray($rules);
40
        }
41
42
        if (is_callable($rules)) {
43
            return $factory->fromCallback($rules);
44
        }
45
46
        if (is_null($rules)) {
47
            return $factory->fromDefault();
48
        }
49
50
        if (empty($rules)) {
51
            return $factory->fromEmpty();
52
        }
53
54
        throw new InvalidArgumentException('Could not make rules');
55
    }
56
57
    /**
58
     * Parse a string pattern and get a Policy.
59
     *
60
     * @param string $pattern
61
     * @return Policy
62
     */
63
    protected function fromPattern($pattern)
64
    {
65
        return $this->fromArray(
66
            $this->parseStringToArray($pattern)
67
        );
68
    }
69
70
    /**
71
     * Parse a rules array and get a Policy.
72
     *
73
     * @param array $rules
74
     * @return Policy
75
     */
76
    protected function fromArray(array $rules)
77
    {
78
        return $this->fromCallback(function($guard) use ($rules) {
79
            foreach ($rules as $method => $arguments) {
80
                $guard->allow($method, $arguments);
81
            }
82
        });
83
    }
84
85
    /**
86
     * Parse string into an array of method names and argument lists.
87
     *
88
     * For example
89
     *   "where(id|name, =, *)"
90
     * becomes
91
     *   [
92
     *      "where" => ["id|name", "=", "*"]
93
     *   ]
94
     *
95
     * @param string $input
96
     * @return array
97
     */
98
    protected function parseStringToArray($input)
99
    {
100
        return array_build(
0 ignored issues
show
Deprecated Code introduced by
The function array_build() has been deprecated with message: since version 5.2.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
101
            preg_split('#(?<=\))\s*(?=[a-z])#i', $input), // split on spaces between e.g. `where(*) orderBy(*)`
102
            function($index, $rule) {
103
104
                if ( ! preg_match('#^(?<method>[a-z]+)\((?<args>[^)]*)\)$#i', $rule, $clause)) {
105
                    throw new InvalidArgumentException('Could not parse rule ['.$rule.']');
106
                }
107
108
                return [
109
                    $clause['method'],
110
                    preg_split('#\s*,\s*#', $clause['args'])
111
                ];
112
            }
113
        );
114
    }
115
116
    /**
117
     * Use a callback to create a Policy.
118
     *
119
     * @param callable $callback
120
     * @return Policy
121
     */
122
    protected function fromCallback(callable $callback)
123
    {
124
        $builder = new Builder();
125
126
        call_user_func($callback, $builder);
127
128
        return $builder->toPolicy();
129
    }
130
131
    /**
132
     * Return the default policy.
133
     *
134
     * @return Policy
135
     */
136
    protected function fromDefault()
137
    {
138
        return $this->make($this->defaultPolicy);
139
    }
140
141
    /**
142
     * Create an null Policy.
143
     *
144
     * @return Policy
145
     */
146
    protected function fromEmpty()
147
    {
148
        return new Policy();
149
    }
150
}