Completed
Push — develop ( a3ab64...524863 )
by Daniel
13:34
created

base::get_message()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 12
ccs 0
cts 6
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
/**
3
*
4
* Pages extension for the phpBB Forum Software package.
5
*
6
* @copyright (c) 2014 phpBB Limited <https://www.phpbb.com>
7
* @license GNU General Public License, version 2 (GPL-2.0)
8
*
9
*/
10
11
namespace blitze\sitemaker\exception;
12
13
/**
14
* Base exception
15
*/
16
class base extends \Exception
17
{
18
	/**
19
	* Null if the message is a string, array if the message was submitted as an array
20
	* @var string|array
21
	*/
22
	protected $message_full;
23
24
	protected $previous;
25
26
	/**
27
	* Constructor
28
	*
29
	* Different from normal exceptions in that we do not enforce $message to be a string.
30
	*
31
	* @param string|array $message
32
	* @param int $code
33
	* @param \Exception $previous
34
	* @access public
35
	*/
36 32
	public function __construct($message = null, $code = 0, \Exception $previous = null)
37
	{
38
		// We're slightly changing the way exceptions work
39
		// Tools, such as xdebug, expect the message to be a string, so to prevent errors
40
		// with those tools, we store our full message in message_full and only a string in message
41 32
		if (is_array($message))
42 32
		{
43 20
			$this->message = (string) $message[0];
44 20
		}
45
		else
46
		{
47 12
			$this->message = $message;
48
		}
49 32
		$this->message_full = $message;
50
51 32
		$this->code = $code;
52 32
		$this->previous = $previous;
53 32
	}
54
55
	/**
56
	* Translate all portions of the message sent to the exception
57
	*
58
	* Goes through each element of the array and tries to translate them
59
	*
60
	* @param \phpbb\language\language $translator
61
	* @param string|array $message_portions The message portions to translate
62
	* @param string|null $parent_message Send a string to translate all of the
63
	*     portions with the parent message (typically used to format a string
64
	*     with the given message portions). Null to ignore. Default: Null
65
	* @return array|string Array if $parent_message === null else a string
66
	* @access protected
67
	*/
68
	protected function translate_portions(\phpbb\language\language $translator, $message_portions, $parent_message = null)
69
	{
70
		// Make sure our language file has been loaded
71
		$this->add_lang($translator);
72
73
		// Ensure we have an array
74
		if (!is_array($message_portions))
75
		{
76
			$message_portions = array($message_portions);
77
		}
78
79
		// Translate each message portion
80
		foreach ($message_portions as &$message)
81
		{
82
			// Attempt to translate each portion
83
			$translated_message = $translator->lang('EXCEPTION_' . $message);
84
85
			// Check if translating did anything
86
			if ($translated_message !== 'EXCEPTION_' . $message)
87
			{
88 32
				// It did, so replace message with the translated version
89
				$message = $translated_message;
90
			}
91 32
		}
92
		// Always unset a variable passed by reference in a foreach loop
93
		unset($message);
94 32
95 32
		if ($parent_message !== null)
96 12
		{
97 12
			// Prepend the parent message to the message portions
98
			array_unshift($message_portions, (string) $parent_message);
99
100 32
			// We return a string
101
			return call_user_func_array(array($translator, 'lang'), $message_portions);
102
		}
103 32
104
		// We return an array
105
		return $message_portions;
106 32
	}
107 32
108
	/**
109
	* Add our language file
110
	*
111 32
	* @param \phpbb\language\language $translator
112
	* @return null
113 32
	* @access public
114
	*/
115 32
	public function add_lang(\phpbb\language\language $translator)
116 32
	{
117
		static $is_loaded = false;
118 32
119
		// We only need to load the language file once
120
		if ($is_loaded)
121 32
		{
122
			return;
123
		}
124
125
		// Add our language file
126
		$translator->add_lang('exceptions', 'blitze/sitemaker');
127
128
		// So the language file is only loaded once
129
		$is_loaded = true;
130
	}
131
132
	/**
133
	* Output a string of this error message
134
	*
135 32
	* This will hopefully be never called, always catch the expected exceptions
136
	* and call get_message to translate them into an error that a user can
137 32
	* understand
138
	*
139
	* @return string
140
	* @access public
141 32
	*/
142 29
	public function __toString()
143
	{
144
		return (is_array($this->message_full)) ? var_export($this->message_full, true) : (string) $this->message_full;
145
	}
146
}
147