Completed
Push — master ( 0bb17e...5a7e4d )
by Sebastian
04:41
created

PageHelper   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 79
rs 10
c 0
b 0
f 0
wmc 24
lcom 0
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
C processPageRangeFormats() 0 28 7
A renderMinimal() 0 12 3
C renderChicago() 0 26 14
1
<?php
2
/*
3
 * citeproc-php
4
 *
5
 * @link        http://github.com/seboettg/citeproc-php for the source repository
6
 * @copyright   Copyright (c) 2017 Sebastian Böttger.
7
 * @license     https://opensource.org/licenses/MIT
8
 */
9
10
namespace Seboettg\CiteProc\Util;
11
12
use Seboettg\CiteProc\Style\Options\PageRangeFormats;
13
14
/**
15
 * Class PageHelper
16
 * @package Seboettg\CiteProc\Util
17
 * @author Sebastian Böttger <[email protected]>
18
 */
19
class PageHelper
20
{
21
22
    public static function processPageRangeFormats($ranges, $pageRangeFormat)
23
    {
24
        list($from, $to) = $ranges;
25
26
        if (!empty($pageRangeFormat) ) {
27
28
            switch ($pageRangeFormat) {
29
                case PageRangeFormats::MINIMAL:
30
                    $resTo = self::renderMinimal($from, $to);
31
                    break;
32
                case PageRangeFormats::MINIMAL_TWO:
33
                    if (strlen($to) > 2) {
34
                        $resTo = self::renderMinimal($from, $to, strlen($to)-2);
35
                    } else {
36
                        $resTo = $to;
37
                    }
38
                    break;
39
                case PageRangeFormats::CHICAGO:
40
                    $resTo = self::renderChicago($from, $to);
41
                    break;
42
                case PageRangeFormats::EXPANDED:
43
                default:
44
                    $resTo = $to;
45
            }
46
            return "$from-$resTo";
47
        }
48
        return "$from-$to";
49
    }
50
51
    /**
52
     *
53
     * @param $from
54
     * @param $to
55
     * @param int $limit
56
     * @return string
57
     */
58
    private static function renderMinimal($from, $to, $limit = 1)
59
    {
60
        $resTo = "";
61
        for ($i = strlen($to) - 1; $i >= $limit; --$i) {
62
            $digitTo = $to{$i};
63
            $digitFrom = $from{$i};
64
            if ($digitTo !== $digitFrom) {
65
                $resTo = $digitTo . $resTo;
66
            }
67
        }
68
        return $resTo;
69
    }
70
71
    private static function renderChicago($from, $to)
72
    {
73
        if ($from == 100 || $from % 100 === 0) {
74
            return $to;
75
        } else if ($from < 100) {
76
            return $to;
77
        } else if ($from % 100 < 10) {
78
            return self::renderMinimal($from, $to); //Use changed part only, omitting unneeded zeros
79
        } else if (strlen($from) == 4) {
80
            if ($from % 1000 != $to % 1000) {
81
                return $to;
82
            }
83
            return self::renderMinimal($from, $to);
84
        } else if (strlen($from) != 4 && $from % 100 >= 10 && $from % 100 <= 99) {
85
            $resTo = substr($to,-2);
86
            $from_ = substr($from, 0, strlen($from) - 2);
87
            $to_ = substr($to, 0, strlen($to) - 2);
88
            if ($from_ != $to_ && strlen($from_) == strlen($to_)) {
89
                for ($i = strlen($from_)-1; $i >= 0 && $to_{$i} != $from_{$i}; --$i) {
90
                    $resTo = $to{$i} . $resTo;
91
                }
92
            }
93
            return $resTo;
94
        }
95
        return self::renderMinimal($from, $to, 1);
96
    }
97
}