Completed
Push — master ( 70bc0c...0e3f35 )
by Thibaud
06:31
created

SortRequest::coerceToDefaultValue()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 0
cts 0
cp 0
rs 9.2
cc 4
eloc 4
nc 2
nop 1
crap 20
1
<?php
2
3
namespace Alchemy\RestBundle\Rest\Request;
4
5
use Alchemy\Rest\Request\Sort;
6
use Alchemy\Rest\Request\SortOptions;
7
8
class SortRequest implements SortOptions
9
{
10
    /**
11
     * @var string|array
12
     */
13
    private $sorts;
14
15
    /**
16
     * @var string
17
     */
18
    private $property;
19
20
    /**
21
     * @var string
22
     */
23
    private $direction;
24
25
    /**
26
     * @param $sorts
27
     * @param string|null $property
28
     * @param string|null $direction
29
     */
30 22
    public function __construct($sorts, $property = null, $direction = null)
31
    {
32 22
        $this->sorts = $sorts;
33 22
        $this->property = $property;
34 22
        $this->direction = $direction;
35 22
    }
36
37
    /**
38
     * @param array $sortMap
39
     * @return array
40
     */
41 20
    public function getSorts(array $sortMap = array())
42
    {
43 20
        $sorts = array();
44
45 20
        foreach ($this->normalizeSorts($this->sorts) as $sort) {
46 18
            $sort = $this->extractSortProperty($sort);
47 18
            $sort = $this->mapSortProperty($sortMap, $sort[0], $sort[1]);
48
49 18
            if ($sort) {
50 18
                $sorts[] = $sort;
51 18
            }
52 20
        }
53
54 20
        return $sorts;
55
    }
56
57
    /**
58
     * @return array|string|null
59
     */
60 20
    private function normalizeSorts($sorts)
61
    {
62 20
        $sorts = $this->coerceToDefaultValue($sorts);
63 6
64 6
        if (is_string($sorts)) {
65
            $sorts = explode(',', $sorts);
66 20
        }
67 12
68 12
        if ($sorts === null) {
69
            $sorts = array();
70 20
        }
71 2
72 2
        return $sorts;
73
    }
74 20
75
    /**
76
     * @param $sort
77
     * @return array
78
     */
79
    private function extractSortProperty($sort)
80
    {
81 18
        if (is_string($sort)) {
82
            $sort = explode(':', $sort);
83 18
        }
84 12
85 12
        if (count($sort) < 2) {
86
            $sort[1] = self::SORT_ASC;
87 18
        }
88 4
89 4
        return $sort;
90
    }
91 18
92
    /**
93
     * @param array $sortMap
94
     * @param $sort
95
     * @param $direction
96
     * @return Sort
97
     */
98
    private function mapSortProperty(array $sortMap, $sort, $direction)
99
    {
100 18
        if (isset($sortMap[$sort])) {
101
            $sort = $sortMap[$sort];
102 18
        }
103 2
104 2
        return new Sort($sort, $direction);
105
    }
106 18
107
    /**
108
     * @param $sorts
109
     * @return array
110
     */
111
    private function coerceToDefaultValue($sorts)
112
    {
113
        if ($sorts === null && $this->property !== null && $this->direction !== null) {
114
            $sorts = array(array($this->property, $this->direction));
115
        }
116
117
        return $sorts;
118
    }
119
}
120