Completed
Push — master ( e87eab...888d29 )
by Sebastian
03:03
created

Number::roman()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 2
nop 1
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is a part of HDS (HeBIS Discovery System). HDS is an 
4
 * extension of the open source library search engine VuFind, that 
5
 * allows users to search and browse beyond resources. More 
6
 * Information about VuFind you will find on http://www.vufind.org
7
 * 
8
 * Copyright (C) 2016 
9
 * HeBIS Verbundzentrale des HeBIS-Verbundes 
10
 * Goethe-Universität Frankfurt / Goethe University of Frankfurt
11
 * http://www.hebis.de
12
 * 
13
 * This program is free software; you can redistribute it and/or
14
 * modify it under the terms of the GNU General Public License
15
 * as published by the Free Software Foundation; either version 2
16
 * of the License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
26
 */
27
28
namespace Seboettg\CiteProc\Rendering;
29
use Seboettg\CiteProc\CiteProc;
30
use Seboettg\CiteProc\Util;
31
use Seboettg\CiteProc\Styles\AffixesTrait;
32
use Seboettg\CiteProc\Styles\DisplayTrait;
33
use Seboettg\CiteProc\Styles\FormattingTrait;
34
use Seboettg\CiteProc\Styles\TextCaseTrait;
35
36
37
/**
38
 * Class Number
39
 * @package Seboettg\CiteProc\Rendering
40
 *
41
 * @author Sebastian Böttger <[email protected]>
42
 */
43
class Number
44
{
45
46
    use FormattingTrait,
47
        AffixesTrait,
48
        TextCaseTrait,
49
        DisplayTrait;
50
51
    private $variable;
52
53
    private $form;
54
55
    public function __construct(\SimpleXMLElement $node)
56
    {
57
        //<number variable="edition" form="ordinal"/>
58
        /** @var \SimpleXMLElement $attribute */
59 View Code Duplication
        foreach ($node->attributes() as $attribute) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
            switch ($attribute->getName()) {
61
                case 'variable':
62
                    $this->variable = (string) $attribute;
63
                    break;
64
                case 'form':
65
                    $this->form = (string) $attribute;
66
            }
67
        }
68
69
        $this->initFormattingAttributes($node);
70
        $this->initAffixesAttributes($node);
71
        $this->initTextCaseAttributes($node);
72
    }
73
74
    public function render($data)
75
    {
76
77
        if (empty($this->variable) || empty($data->{$this->variable})) {
78
            return "";
79
        }
80
        switch ($this->form) {
81
            case 'ordinal':
82
                $text = self::ordinal($data->{$this->variable});
83
                break;
84
            case 'long-ordinal':
85
                $text = self::longOrdinal($data->{$this->variable});
86
                break;
87
            case 'roman':
88
                $text = Util\Number::dec2roman($data->{$this->variable});
89
                break;
90
            case 'numeric':
91
            default:
92
                $text = $data->{$this->variable};
93
                break;
94
        }
95
        return $this->wrapDisplayBlock($this->addAffixes($this->format($this->applyTextCase($text))));
96
    }
97
98
    public static function ordinal($num) {
99
        if (($num / 10) % 10 == 1) {
100
            $num .= CiteProc::getContext()->getLocale()->filter('terms', 'ordinal-04')->single;
101
        } elseif ($num % 10 == 1) {
102
            $num .= CiteProc::getContext()->getLocale()->filter('terms', 'ordinal-01')->single;
103
        } elseif ($num % 10 == 2) {
104
            $num .= CiteProc::getContext()->getLocale()->filter('terms', 'ordinal-02')->single;
105
        } elseif ($num % 10 == 3) {
106
            $num .= CiteProc::getContext()->getLocale()->filter('terms', 'ordinal-03')->single;
107
        } else {
108
            $num .= CiteProc::getContext()->getLocale()->filter('terms', 'ordinal-04')->single;
109
        }
110
        return $num;
111
    }
112
113
114
    public static function longOrdinal($num) {
115
        $num = sprintf("%02d", $num);
116
        $ret = CiteProc::getContext()->getLocale()->filter('terms', 'long-ordinal-' . $num)->single;
117
        if (!$ret) {
118
            return self::ordinal($num);
119
        }
120
        return $ret;
121
    }
122
123
124
}