Completed
Push — master ( 90072e...c81959 )
by Daniel
11:23
created

Parser::Translate_Default()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\i18n\TextCollection;
4
5
use SilverStripe\i18n\i18n;
6
use SilverStripe\View\SSTemplateParser;
7
8
/**
9
 * Parser that scans through a template and extracts the parameters to the _t and <%t calls
10
 */
11
class Parser extends SSTemplateParser
12
{
13
    /**
14
     * List of all entities
15
     *
16
     * @var array
17
     */
18
    protected $entities = [];
19
20
    /**
21
     * Current entity
22
     *
23
     * @var array
24
     */
25
    protected $currentEntity = [];
26
27
    /**
28
     * Key of current entity
29
     *
30
     * @var string
31
     */
32
    protected $currentEntityKey = null;
33
34
    /*
35
     * Show warning if default omitted
36
     *
37
     * @var bool
38
     */
39
    protected $warnIfEmpty = true;
40
41
    /**
42
     * @param string $string
43
     * @param bool $warnIfEmpty
44
     */
45
    public function __construct($string, $warnIfEmpty = true)
46
    {
47
        parent::__construct();
48
        $this->string = $string;
49
        $this->pos = 0;
50
        $this->depth = 0;
51
        $this->regexps = array();
52
        $this->warnIfEmpty = $warnIfEmpty;
53
    }
54
55
    public function Translate__construct(&$res)
56
    {
57
        $this->currentEntity = [];
58
        $this->currentEntityKey = null;
59
    }
60
61
    public function Translate_Entity(&$res, $sub)
62
    {
63
        $this->currentEntityKey = $sub['text']; // key
64
    }
65
66
    public function Translate_Default(&$res, $sub)
67
    {
68
        $this->currentEntity['default'] = $sub['String']['text']; // default
69
    }
70
71
    public function Translate_Context(&$res, $sub)
72
    {
73
        $this->currentEntity['comment'] = $sub['String']['text']; //comment
74
    }
75
76
    public function Translate__finalise(&$res)
77
    {
78
        // Validate entity
79
        $entity = $this->currentEntity;
80
        if (empty($entity['default'])) {
81
            if ($this->warnIfEmpty) {
82
                trigger_error("Missing localisation default for key " . $this->currentEntityKey, E_USER_NOTICE);
83
            }
84
            return;
85
        }
86
87
        // Detect plural forms
88
        $plurals = i18n::parse_plurals($entity['default']);
89
        if ($plurals) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $plurals of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
90
            unset($entity['default']);
91
            $entity = array_merge($entity, $plurals);
92
        }
93
94
        // If only default is set, simplify
95
        if (count($entity) === 1 && !empty($entity['default'])) {
96
            $entity = $entity['default'];
97
        }
98
99
        $this->entities[$this->currentEntityKey] = $entity;
100
    }
101
102
    /**
103
     * Parses a template and returns any translatable entities
104
     *
105
     * @param string $template String to parse for translations
106
     * @param bool $warnIfEmpty Show warnings if default omitted
107
     * @return array Map of keys -> values
108
     */
109
    public static function getTranslatables($template, $warnIfEmpty = true)
110
    {
111
        // Run the parser and throw away the result
112
        $parser = new Parser($template, $warnIfEmpty);
113
        if (substr($template, 0, 3) == pack("CCC", 0xef, 0xbb, 0xbf)) {
114
            $parser->pos = 3;
115
        }
116
        $parser->match_TopTemplate();
117
        return $parser->getEntities();
118
    }
119
120
    /**
121
     * @return array
122
     */
123
    public function getEntities()
124
    {
125
        return $this->entities;
126
    }
127
}
128