Completed
Push — master ( cf2b70...0b1628 )
by Gaetano
06:47
created

SortConverter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 0
dl 0
loc 74
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A hash2SortField() 0 14 2
A sortField2Hash() 0 11 4
A hash2SortOrder() 0 14 3
A sortOrder2Hash() 0 8 2
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\Helper;
4
5
use eZ\Publish\API\Repository\Values\Content\Location;
6
7
class SortConverter
8
{
9
    /**
10
     * @param string $value
11
     * @return int|null
12
     */
13
    public function hash2SortField($value)
14
    {
15
        $sortField = null;
16
17
        if ($value !== null) {
18
            $sortFieldId = "SORT_FIELD_" . strtoupper($value);
19
20
            $ref = new \ReflectionClass('eZ\Publish\API\Repository\Values\Content\Location');
21
22
            $sortField = $ref->getConstant($sortFieldId);
23
        }
24
25
        return $sortField;
26
    }
27
28
    /**
29
     * @param int $value
30
     * @return string
31
     * @throws \Exception
32
     */
33
    public function sortField2Hash($value)
34
    {
35
        $ref = new \ReflectionClass('eZ\Publish\API\Repository\Values\Content\Location');
36
        foreach($ref->getConstants() as $key => $val) {
37
            if (strpos($key, 'SORT_FIELD_') === 0 && $val == $value) {
38
                return strtolower(substr($key, 11));
39
            }
40
        }
41
42
        throw new \Exception("Unknown sort field: '$value'");
43
    }
44
45
    /**
46
     * Get the sort order based on the current value and the value in the DSL definition.
47
     *
48
     * @see \eZ\Publish\API\Repository\Values\Content\Location::SORT_ORDER_*
49
     *
50
     * @param string $value ASC|DESC
51
     * @return int
52
     */
53
    public function hash2SortOrder($value)
54
    {
55
        $sortOrder = null;
56
57
        if ($value !== null) {
58
            if (strtoupper($value) === 'ASC') {
59
                $sortOrder = Location::SORT_ORDER_ASC;
60
            } else {
61
                $sortOrder = Location::SORT_ORDER_DESC;
62
            }
63
        }
64
65
        return $sortOrder;
66
    }
67
68
    /**
69
     * @param int $value
70
     * @return string
71
     */
72
    public function sortOrder2Hash($value)
73
    {
74
        if ($value === Location::SORT_ORDER_ASC) {
75
            return 'ASC';
76
        } else {
77
            return 'DESC';
78
        }
79
    }
80
}
81