MessageFileLanguage::getMessage()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
/*
3
 * Copyright (c) 2012-2015 Marc TEYSSIER
4
 * 
5
 * See the file LICENSE.txt for copying permission.
6
 */
7
namespace Mouf\Utils\I18n\Fine\Translator;
8
9
/**
10
 * The FineMessageLanguage class represents a PHP resource file that can be loaded / saved / modified.
11
 * There are many files for on language. Files are write with the start information of the key. Function used the separator ., - or _. 
12
 */
13
class MessageFileLanguage {
14
15
	/**
16
	 * The path to the folder to be loaded
17
	 * @var string
18
	 */
19
	private $folder;
20
21
	/**
22
	 * The array of messages in the folder loaded.
23
	 * @var array<string, string>
24
	 */
25
	private $msg = [];
26
27
	/**
28
	 * Language load
29
	 * @var string
30
	 */
31
	private $language = null;
32
	
33
	/**
34
	 * Loads all message for a language
35
	 * @param string $folder
36
	 * @param string $language
37
	 */
38
	public function __construct($folder, $language) {
39
		$this->folder = $folder;
40
		$this->language = $language;
41
42
		if(file_exists($folder."messages_".$language.".php")) {
43
			$this->msg = include($folder."messages_".$language.".php");
44
		}
45
	}
46
47
	/**
48
	 * Saves the file for current language
49
	 */
50
	public function save() {
51
		if($this->msg) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->msg 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...
52
			ksort($this->msg);
53
			
54
			$file = $this->folder."messages_".$this->language.".php";
55
56
			$old = umask(00002);
57
58
			if (file_exists($file) && !is_writable($file)) {
59
				throw new FileTranslatorException('Cannot write to file "'.$file.'".');
60
			} elseif (!file_exists($file)) {
61
				if (!is_dir($this->folder)) {
62
					$result = mkdir($this->folder, 0775, true);
63
					if (!$result) {
64
						throw new FileTranslatorException('Cannot create directory "'.$this->folder.'".');
65
					}
66
				}
67
				if (!is_writable($this->folder)) {
68
					throw new FileTranslatorException('Cannot write to directory "'.$this->folder.'".');
69
				}
70
			}
71
72
73
			$fp = fopen($file, "w");
74
			fwrite($fp, "<?php\n");
75
			fwrite($fp, 'return ');
76
			fwrite($fp, var_export($this->msg, true));
77
			fwrite($fp, ";\n");
78
			
79
			fclose($fp);
80
			umask($old);
81
		}
82
	}
83
84
	/**
85
	 * Delete file of language (Delete language too)
86
	 * @param $language string Language to delete
87
	 */
88
	private function deleteFile() {
89
		if(file_exists($this->folder."messages_".$this->language.".php")) {
90
			unlink($this->folder."messages_".$this->language.".php");
91
		}
92
	}
93
	
94
	/**
95
	 * Set message for a key
96
	 * @param $key string Message key
97
	 * @param $message string Message
98
	 */
99
	public function setMessage($key, $message) {
100
		$this->msg[$key] = $message;
101
	}
102
103
	/**
104
	 * Sets messages
105
	 * 
106
	 * @param $translations array<string, string> To set many messages in one time. The array is key message.
107
	 */
108
	public function setMessages(array $translations) {
109
		foreach ($translations as $key => $message) {
110
			if($message) {
111
				$this->msg[$key] = $message;
112
			}
113
		}
114
	}
115
	
116
	/**
117
	 * Returns a message for the key $key.
118
	 * @var $key string Key of translation search.
119
	 */
120
	public function getMessage($key) {
121
		if (isset($this->msg[$key])) {
122
			return $this->msg[$key];
123
		} else {
124
			return null;
125
		}
126
	}
127
128
	/**
129
	 * Sets the message
130
	 * @var $key Remove a message for key
131
	 */
132
	public function deleteMessage($key) {
133
		if(isset($this->msg[$key])) {
134
			unset($this->msg[$key]);
135
		}
136
		if(!$this->msg) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->msg 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...
137
			$this->deleteFile();
138
		}
139
	}
140
	
141
	/**
142
	 * Returns all messages for this file.
143
	 * @return array<string, string> Return the array of message (key, value)
0 ignored issues
show
Documentation introduced by
The doc-type array<string, could not be parsed: Expected ">" at position 5, but found "end of type". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
144
	 */
145
	public function getAllMessages() {
146
		return $this->msg;
147
	}
148
}
149