Completed
Pull Request — master (#3)
by Ryosuke
06:34
created

Processor::field()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Lampager\Idiorm;
4
5
use IdiormResultSet;
6
use Lampager\ArrayProcessor;
7
use Lampager\Contracts\Formatter;
8
use Lampager\Idiorm\Concerns\HasSnakeAliases;
9
use Lampager\Query;
10
11
/**
12
 * Class Processor
13
 *
14
 * @method static set_default_formatter(callable|Formatter|string $formatter) Override static default formatter.
15
 * @method static restore_default_formatter() Restore static default formatter.
16
 * @method $this use_formatter(callable|Formatter|string $formatter) Use custom formatter.
17
 * @method $this restore_formatter() Restore default formatter.
18
 *
19
 * @see AbstractProcessor, ArrayProcessor
20
 */
21
class Processor extends ArrayProcessor
22
{
23
    use HasSnakeAliases;
24
25
    /**
26
     * Return comparable value from a row.
27
     *
28
     * @param  mixed      $row
29
     * @param  string     $column
30
     * @return int|string
31
     */
32 21
    protected function field($row, $column)
33
    {
34 21
        return parent::field($row, static::dropTablePrefix($column));
35
    }
36
37
    /**
38
     * Slice rows, like PHP function array_slice().
39
     *
40
     * @param  array[]|IdiormResultSet $rows
41
     * @param  int                     $offset
42
     * @param  null|int                $length
43
     * @return mixed
44
     */
45 21
    protected function slice($rows, $offset, $length = null)
46
    {
47 21
        $r = $rows instanceof IdiormResultSet ? $rows->get_results() : $rows;
48 21
        $r = array_slice($r, $offset, $length);
49 21
        return $rows instanceof IdiormResultSet ? new IdiormResultSet($r) : $r;
0 ignored issues
show
Bug Compatibility introduced by
The expression $rows instanceof \Idiorm...iormResultSet($r) : $r; of type IdiormResultSet|array adds the type IdiormResultSet to the return on line 49 which is incompatible with the return type of the parent method Lampager\ArrayProcessor::slice of type array.
Loading history...
50
    }
51
52
    /**
53
     * Reverse rows, like PHP function array_reverse().
54
     *
55
     * @param  array[]|IdiormResultSet $rows
56
     * @return mixed
57
     */
58 8
    protected function reverse($rows)
59
    {
60 8
        $r = $rows instanceof IdiormResultSet ? $rows->get_results() : $rows;
61 8
        $r = array_reverse($r);
62 8
        return $rows instanceof IdiormResultSet ? new IdiormResultSet($r) : $r;
0 ignored issues
show
Bug Compatibility introduced by
The expression $rows instanceof \Idiorm...iormResultSet($r) : $r; of type IdiormResultSet|array adds the type IdiormResultSet to the return on line 62 which is incompatible with the return type of the parent method Lampager\ArrayProcessor::reverse of type array.
Loading history...
63
    }
64
65
    /**
66
     * Format result.
67
     *
68
     * @param  array[]|IdiormResultSet $rows
69
     * @param  array                   $meta
70
     * @param  Query                   $query
71
     * @return PaginationResult
72
     */
73 21
    protected function defaultFormat($rows, array $meta, Query $query)
74
    {
75 21
        return new PaginationResult($rows, $meta);
76
    }
77
78
    /**
79
     * Drop table prefix on column name.
80
     *
81
     * @param  string $column
82
     * @return string
83
     */
84 21
    protected static function dropTablePrefix($column)
85
    {
86 21
        $segments = explode('.', $column);
87
88 21
        return end($segments);
89
    }
90
}
91