Completed
Push — master ( 1f4ad9...320f5c )
by John
03:15
created

FieldSorter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 47
ccs 0
cts 12
cp 0
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A sort() 0 13 2
A getOrderByFieldType() 0 7 2
1
<?php
2
3
namespace Graze\CiffRenderer\Parser;
4
5
use Graze\CiffRenderer\Parser\FieldType;
6
use Graze\CiffRenderer\Exception\UnsupportedFieldTypeException;
7
8
class FieldSorter
9
{
10
    /**
11
     * @var int
12
     */
13
    protected $fieldTypeToOrder = [
14
        FieldType::FIELD_STATIC_GRAPHIC    => 1,
15
        FieldType::FIELD_FIXED_TEXT        => 2,
16
        FieldType::FIELD_COMPLEX_TEXT      => 3,
17
        FieldType::FIELD_BARCODE           => 4,
18
        FieldType::FIELD_DATE              => 5,
19
        FieldType::FIELD_OFFSET_DATE       => 6,
20
        FieldType::FIELD_MERGE_FIELD       => 7,
21
        FieldType::FIELD_GRAPHIC_PRIMITIVE => 8,
22
    ];
23
24
    /**
25
     * @param string $fieldType
26
     * @return int
27
     * @throws UnsupportedFieldTypeException;
28
     */
29
    private function getOrderByFieldType($fieldType)
30
    {
31
        if (!isset($this->fieldTypeToOrder[$fieldType])) {
32
            throw new UnsupportedFieldTypeException($fieldType);
33
        }
34
35
        return $this->fieldTypeToOrder[$fieldType];
36
    }
37
38
    /**
39
     * @param array $fields
40
     * @return array
41
     */
42
    public function sort(array $fields)
43
    {
44
        $fieldKeyToOrder = [];
45
        foreach ($fields as $key => $field) {
46
            $fieldType = (string) $field->FldType; // @codingStandardsIgnoreLine
47
            $fieldKeyToOrder[$key] = $this->getOrderByFieldType($fieldType);
48
        }
49
50
        // sort
51
        asort($fieldKeyToOrder);
52
53
        // apply the sort to the fields
54
        return array_replace($fieldKeyToOrder, $fields);
55
    }
56
}
57