1: <?php
2:
3: 4: 5: 6: 7:
8:
9: namespace academicpuma\citeproc;
10:
11: 12: 13: 14: 15:
16:
17: class Label extends Format {
18:
19: private $plural;
20:
21: function render($data, $mode = NULL) {
22: $text = '';
23:
24: $variables = explode(' ', $this->variable);
25: $form = (($form = $this->form)) ? $form : 'long';
26: switch ($this->plural) {
27: case 'never':
28: $plural = 'single';
29: break;
30: case 'always':
31: $plural = 'multiple';
32: break;
33: case 'contextual':
34: default:
35: }
36: foreach ($variables as $variable) {
37: if (isset($data->{$variable})) {
38: if (!isset($this->plural) && empty($plural) && is_array($data->{$variable})) {
39: $count = count($data->{$variable});
40: if ($count == 1) {
41: $plural = 'single';
42: } elseif ($count > 1) {
43: $plural = 'multiple';
44: }
45: } else {
46: $plural = $this->evaluateStringPluralism($data, $variable);
47: }
48: if (!empty($data->{$variable}) && ($term = $this->citeproc->get_locale('term', $variable, $form, $plural))) {
49: $text = $term;
50: break;
51: }
52: }
53: }
54:
55: if (empty($text))
56: return;
57: if ($this->{'strip-periods'})
58: $text = str_replace('.', '', $text);
59: return $this->format($text);
60: }
61:
62: function evaluateStringPluralism($data, $variable) {
63: $str = $data->{$variable};
64: $plural = 'single';
65:
66: if (!empty($str)) {
67:
68: switch ($variable) {
69: case 'page':
70: $page_regex = "/([a-zA-Z]*)([0-9]+)\s*(?:–|-)\s*([a-zA-Z]*)([0-9]+)/";
71: $err = preg_match($page_regex, $str, $m);
72: if ($err !== FALSE && count($m) == 0) {
73: $plural = 'single';
74: } elseif ($err !== FALSE && count($m)) {
75: $plural = 'multiple';
76: }
77: break;
78: default:
79: }
80: }
81: return $plural;
82: }
83:
84: }
85: