Sorting   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 53
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 9 3
A addField() 0 4 1
A sorting() 0 4 1
A fields() 0 4 1
A isEmpty() 0 4 1
1
<?php
2
/**
3
 * Author: Nil Portugués Calderó <[email protected]>
4
 * Date: 12/14/15
5
 * Time: 7:11 PM.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace NilPortugues\Api\JsonApi\Http\Request\Parameters;
11
12
/**
13
 * Class Sorting.
14
 */
15
class Sorting
16
{
17
    /**
18
     * @var array
19
     */
20
    protected $sort = [];
21
22
    /**
23
     * @return string
24
     */
25
    public function get()
26
    {
27
        $get = [];
28
        foreach ($this->sort as $field => $direction) {
29
            $get[] = ('descending' === $direction) ? '-'.$field : $field;
30
        }
31
32
        return implode(',', $get);
33
    }
34
35
    /**
36
     * @param string $field
37
     * @param string $direction
38
     */
39
    public function addField($field, $direction)
40
    {
41
        $this->sort[(string) $field] = (string) $direction;
42
    }
43
44
    /**
45
     * @return array
46
     */
47
    public function sorting()
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
48
    {
49
        return $this->sort;
50
    }
51
52
    /**
53
     * @return array
54
     */
55
    public function fields()
56
    {
57
        return array_keys($this->sort);
58
    }
59
60
    /**
61
     * @return bool
62
     */
63
    public function isEmpty()
64
    {
65
        return 0 === count($this->sort);
66
    }
67
}
68