Completed
Push — master ( be02a5...a1ec95 )
by Nick
22:04 queued 02:39
created

Interpreter::parse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace EloquentJs\Query;
4
5
use Illuminate\Http\Request;
6
use InvalidArgumentException;
7
8
class Interpreter
9
{
10
    /**
11
     * Read an EloquentJs `Query` from the given request.
12
     *
13
     * @param Request $request
14
     * @return Query
15
     */
16
    public function parse(Request $request)
17
    {
18
        $calls = json_decode($request->input('query', '[]'));
19
20
        if ($calls === null) {
21
            throw new InvalidArgumentException('The query could not be decoded: '.json_last_error_msg());
22
        }
23
24
        return new Query($this->getMethodCalls($calls));
25
    }
26
27
    /**
28
     * Convert plain array of [methodName, [methodArgs...]] to MethodCall instances.
29
     *
30
     * @param array $calls
31
     * @return array
32
     */
33
    protected function getMethodCalls(array $calls)
34
    {
35
        return array_map(function ($call) {
36
            return new MethodCall($call[0], $call[1]);
37
        }, $calls);
38
    }
39
}
40