Completed
Push — master ( b4ed61...4cdb8c )
by Fabio
15s
created

TGettext_PO::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * TGettext_PO class file.
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the BSD License.
7
 *
8
 * Copyright(c) 2004 by Qiang Xue. All rights reserved.
9
 *
10
 * To contact the author write to {@link mailto:[email protected] Qiang Xue}
11
 * The latest version of PRADO can be obtained from:
12
 * {@link http://prado.sourceforge.net/}
13
 *
14
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
15
 * @version $Revision: 1.2 $  $Date: 2005/01/05 03:15:14 $
16
 * @package System.I18N.core
17
 */
18
19
// +----------------------------------------------------------------------+
20
// | PEAR :: File :: Gettext :: PO                                        |
21
// +----------------------------------------------------------------------+
22
// | This source file is subject to version 3.0 of the PHP license,       |
23
// | that is available at http://www.php.net/license/3_0.txt              |
24
// | If you did not receive a copy of the PHP license and are unable      |
25
// | to obtain it through the world-wide-web, please send a note to       |
26
// | [email protected] so we can mail you a copy immediately.               |
27
// +----------------------------------------------------------------------+
28
// | Copyright (c) 2004 Michael Wallner <[email protected]>                  |
29
// +----------------------------------------------------------------------+
30
//
31
// $Id: PO.php 3187 2012-07-12 11:21:01Z ctrlaltca $
32
33
/**
34
 * File::Gettext::PO
35
 *
36
 * @author      Michael Wallner <[email protected]>
37
 * @license     PHP License
38
 */
39
40
require_once dirname(__FILE__).'/TGettext.php';
41
42
/**
43
 * File_Gettext_PO
44
 *
45
 * GNU PO file reader and writer.
46
 *
47
 * @author      Michael Wallner <[email protected]>
48
 * @version     $Revision: 1.2 $
49
 * @access      public
50
 * @package System.I18N.core
51
 */
52
class TGettext_PO extends TGettext
0 ignored issues
show
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
53
{
54
    /**
55
     * Constructor
56
     *
57
     * @access  public
58
     * @return  object      File_Gettext_PO
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
59
     * @param   string      path to GNU PO file
60
     */
61
    function __construct($file = '')
62
    {
63
        $this->file = $file;
64
    }
65
66
    /**
67
     * Load PO file
68
     *
69
     * @access  public
70
     * @return  mixed   Returns true on success or PEAR_Error on failure.
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use boolean.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
71
     * @param   string  $file
0 ignored issues
show
Documentation introduced by
Should the type for parameter $file not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
72
     */
73
    function load($file = null)
74
    {
75
        if (!isset($file)) {
76
            $file = $this->file;
77
        }
78
79
        // load file
80
        if (!$contents = @file($file)) {
81
            return false;
82
        }
83
        $contents = implode('', $contents);
84
85
        // match all msgid/msgstr entries
86
        $matched = preg_match_all(
87
            '/(msgid\s+("([^"]|\\\\")*?"\s*)+)\s+' .
88
            '(msgstr\s+("([^"]|\\\\")*?"\s*)+)/',
89
            $contents, $matches
90
        );
91
        unset($contents);
92
93
        if (!$matched) {
94
            return false;
95
        }
96
97
        // get all msgids and msgtrs
98
        for ($i = 0; $i < $matched; $i++) {
99
            $msgid = preg_replace(
100
                '/\s*msgid\s*"(.*)"\s*/s', '\\1', $matches[1][$i]);
101
            $msgstr= preg_replace(
102
                '/\s*msgstr\s*"(.*)"\s*/s', '\\1', $matches[4][$i]);
103
            $this->strings[parent::prepare($msgid)] = parent::prepare($msgstr);
104
        }
105
106
        // check for meta info
107
        if (isset($this->strings[''])) {
108
            $this->meta = parent::meta2array($this->strings['']);
109
            unset($this->strings['']);
110
        }
111
112
        return true;
113
    }
114
115
    /**
116
     * Save PO file
117
     *
118
     * @access  public
119
     * @return  mixed   Returns true on success or PEAR_Error on failure.
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use boolean.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
120
     * @param   string  $file
0 ignored issues
show
Documentation introduced by
Should the type for parameter $file not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
121
     */
122
    function save($file = null)
123
    {
124
        if (!isset($file)) {
125
            $file = $this->file;
126
        }
127
128
        // open PO file
129
        if (!is_resource($fh = @fopen($file, 'w'))) {
130
            return false;
131
        }
132
133
        // lock PO file exclusively
134
        if (!flock($fh, LOCK_EX)) {
135
            fclose($fh);
136
            return false;
137
        }
138
        // write meta info
139
        if (count($this->meta)) {
140
            $meta = 'msgid ""' . "\nmsgstr " . '""' . "\n";
141
            foreach ($this->meta as $k => $v) {
142
                $meta .= '"' . $k . ': ' . $v . '\n"' . "\n";
143
            }
144
            fwrite($fh, $meta . "\n");
145
        }
146
        // write strings
147
        foreach ($this->strings as $o => $t) {
148
            fwrite($fh,
149
                'msgid "'  . parent::prepare($o, true) . '"' . "\n" .
150
                'msgstr "' . parent::prepare($t, true) . '"' . "\n\n"
151
            );
152
        }
153
154
        //done
155
        @flock($fh, LOCK_UN);
156
        @fclose($fh);
157
        chmod($file,PRADO_CHMOD);
158
        return true;
159
    }
160
}
161