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
|
|
|
|
74
|
|
|
if ($from > 100 && ($from % 100 > 0) && intval(($from / 100), 10) === intval(($to / 100), 10)) { |
75
|
|
|
return "" . ($to % 100); |
76
|
|
|
} else if ($from >= 10000) { |
77
|
|
|
return "" . ($to % 1000); |
78
|
|
|
} |
79
|
|
|
return $to; |
80
|
|
|
} |
81
|
|
|
} |