Completed
Push — master ( 98eb76...c74b62 )
by Max
01:10
created

SortIterator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 0
loc 38
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A compare() 0 4 1
1
<?php
2
3
/*
4
 *  @copyright (c) 2019 Mendel <[email protected]>
5
 *  @license see license.txt
6
 */
7
8
namespace drycart\data\Iterator;
9
use drycart\data\CompareHelper;
10
11
/**
12
 * Sort iterator data using order rules
13
 *
14
 * @author mendel
15
 */
16
class SortIterator extends \SplHeap
17
{
18
    /**
19
     * Orders
20
     * 
21
     * @var array
22
     */
23
    protected $orders = [];
24
    
25
    /**
26
     * Constructor
27
     * 
28
     * @param \Traversable $iterator
29
     * @param array $orders
30
     */
31
    public function __construct(\Traversable $iterator, array $orders)
32
    {
33
        $this->orders = $orders;
34
        foreach ($iterator as $line) {
35
            $this->insert($line);
36
        }
37
        // Dont call parent, because it not exist (Yes, not exist!)
38
        //parent::__construct();
39
    }
40
41
    /**
42
     * Compare two element using order rules
43
     * 
44
     * @param type $value1
45
     * @param type $value2
46
     * @return int
47
     */
48
    protected function compare($value1, $value2): int
49
    {
50
        return CompareHelper::compareByOrders($this->orders, $value1, $value2);
51
    }
52
53
}
54