Completed
Push — master ( 8a25ad...f9520f )
by Nazar
04:26
created

Text::search()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 17
ccs 0
cts 3
cp 0
crap 2
rs 9.4285
1
<?php
2
/**
3
 * @package   CleverStyle Framework
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2011-2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs;
9
10
/**
11
 * @method static $this instance($check = false)
12
 */
13
class Text {
14
	use Singleton;
15
	/**
16
	 * Gets text on current language
17
	 *
18
	 * @param int      $database
19
	 * @param int|null $id             Getting may be done with group and label or with id
20
	 * @param bool     $store_in_cache If `true` - text will be stored in cache
21
	 *
22
	 * @return false|string
23
	 */
24 4
	function get ($database, $id = null, $store_in_cache = false) {
25 4
		$Cache     = Cache::instance();
26 4
		$L         = Language::instance();
27 4
		$id        = (int)$id;
28 4
		$cache_key = "texts/$database/{$id}_$L->clang";
29 4
		if ($store_in_cache && ($text = $Cache->$cache_key) !== false) {
30 2
			return $text;
31
		}
32 4
		$text = $this->get_text_by_id($database, $L->clang, $id);
33 4
		if ($text === false) {
34 2
			return false;
35
		}
36 4
		if ($store_in_cache) {
37 4
			$Cache->$cache_key = $text;
38
		}
39 4
		return $text;
40
	}
41
	/**
42
	 * @param int    $database
43
	 * @param string $clang
44
	 * @param int    $id
45
	 *
46
	 * @return false|string
47
	 */
48 4
	protected function get_text_by_id ($database, $clang, $id) {
49 4
		$cdb  = DB::instance()->db($database);
50 4
		$text = $cdb->qfs(
51
			"SELECT `d`.`text`
52
			FROM `[prefix]texts` AS `t`
53
				LEFT JOIN `[prefix]texts_data` AS `d`
54
			ON `t`.`id` = `d`.`id`
55
			WHERE
56 4
				`t`.`id`	= $id AND
57
				`d`.`lang`	= '%s'
58 4
			LIMIT 1",
59
			$clang
60
		);
61 4
		if (!$text) {
62 4
			$text = $cdb->qfs(
63
				"SELECT `d`.`text`
64
				FROM `[prefix]texts` AS `t`
65
					LEFT JOIN `[prefix]texts_data` AS `d`
66
				ON `t`.`id` = `d`.`id`
67 4
				WHERE `t`.`id` = $id
68 4
				LIMIT 1"
69
			);
70
		}
71 4
		return $text;
72
	}
73
	/**
74
	 * Sets text on current language
75
	 *
76
	 * @param int    $database
77
	 * @param string $group
78
	 * @param string $label
79
	 * @param string $text
80
	 *
81
	 * @return false|string If multilingual support enabled or was enabled and then disabled but translations remains - returns {¶<i>id</i>}, otherwise returns
82
	 *                      original text
83
	 */
84 4
	function set ($database, $group, $label, $text) {
85 4
		$Cache  = Cache::instance();
86 4
		$Config = Config::instance();
87 4
		$L      = Language::instance();
88 4
		$cdb    = DB::instance()->db_prime($database);
89
		/**
90
		 * Security check, do not allow to silently substitute text from another item
91
		 */
92 4
		if (preg_match('/^\{¶(\d+)\}$/', $text)) {
93 2
			return false;
94
		}
95
		// Find existing text id
96 4
		$id = $cdb->qfs(
97
			"SELECT `id`
98
			FROM `[prefix]texts`
99
			WHERE
100
				`label`	= '%s' AND
101
				`group`	= '%s'
102 4
			LIMIT 1",
103
			$label,
104
			$group
105
		);
106 4
		if (!$id) {
107
			// If not found - either return text directly or add new text entry and obtain id
108 4
			if (!$Config->core['multilingual']) {
109 2
				return $text;
110
			} else {
111 4
				$cdb->q(
112
					"INSERT INTO `[prefix]texts`
113
						(
114
							`label`,
115
							`group`
116
						) VALUES (
117
							'%s',
118
							'%s'
119 4
						)",
120
					$label,
121
					$group
122
				);
123 4
				$id = $cdb->id();
124 4
				if (!$id) {
125
					return $text;
126
				}
127
			}
128
		}
129 4
		$result = $this->set_text($id, $text, $cdb, $L->clang);
1 ignored issue
show
Bug introduced by
It seems like $cdb defined by \cs\DB::instance()->db_prime($database) on line 88 can also be of type object<cs\False_class>; however, cs\Text::set_text() does only seem to accept object<cs\DB\_Abstract>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
130 4
		$Cache->del("texts/$database/{$id}_$L->clang");
131 4
		return $result;
132
	}
133
	/**
134
	 * @param int          $id
135
	 * @param string       $text
136
	 * @param DB\_Abstract $cdb
137
	 * @param string       $clang
138
	 *
139
	 * @return mixed
140
	 */
141 4
	protected function set_text ($id, $text, $cdb, $clang) {
142 4
		$exists_for_current_language = $cdb->qfs(
143
			"SELECT `id`
144
			FROM `[prefix]texts_data`
145
			WHERE
146
				`id`	= '%s' AND
147
				`lang`	= '%s'
148 4
			LIMIT 1",
149
			$id,
150
			$clang
151
		);
152 4
		if ($exists_for_current_language) {
153 2
			$result = $cdb->q(
154
				"UPDATE `[prefix]texts_data`
155
				SET
156
					`text`		= '%s',
157
					`text_md5`	= '%s'
158
				WHERE
159
					`id` = '%s' AND
160 2
					`lang` = '%s'",
161
				$text,
162
				md5($text),
163
				$id,
164
				$clang
165
			);
166
		} else {
167 4
			$result = $cdb->q(
168
				"INSERT INTO `[prefix]texts_data`
169
					(
170
						`id`,
171
						`id_`,
172
						`lang`,
173
						`text`,
174
						`text_md5`
175
					) VALUES (
176
						'%s',
177
						'%s',
178
						'%s',
179
						'%s',
180
						'%s'
181 4
					)",
182
				$id,
183 4
				"{¶$id}",
184
				$clang,
185
				$text,
186
				md5($text)
187
			);
188
		}
189 4
		return $result ? "{¶$id}" : false;
190
	}
191
	/**
192
	 * Deletes text on all languages
193
	 *
194
	 * @param int    $database
195
	 * @param string $group
196
	 * @param string $label
197
	 *
198
	 * @return bool
199
	 */
200 2
	function del ($database, $group, $label) {
201 2
		$Cache = Cache::instance();
202 2
		$cdb   = DB::instance()->db_prime($database);
203 2
		$id    = $cdb->qfs(
204
			"SELECT `id`
205
			FROM `[prefix]texts`
206
			WHERE
207
				`group`	= '%s' AND
208
				`label`	= '%s'
209 2
			LIMIT 1",
210
			$group,
211
			$label
212
		);
213 2
		if ($id) {
214 2
			$L      = Language::instance();
215 2
			$result = $cdb->q(
216
				[
217
					"DELETE FROM `[prefix]texts`
218 2
					WHERE `id` = '%s'",
219
					"DELETE FROM `[prefix]texts_data`
220
					WHERE `id` = '%s'"
221
				],
222
				$id
223
			);
224 2
			$Cache->del("texts/$database/{$id}_$L->clang");
225 2
			return $result;
226
		}
227 2
		return true;
228
	}
229
	/**
230
	 * Process text, and replace {¶([0-9]+)} on real text, is used before showing multilingual information
231
	 *
232
	 * @param int             $database
233
	 * @param string|string[] $data
234
	 * @param bool            $store_in_cache If <b>true</b> - text will be stored in cache
235
	 *
236
	 * @return string|string[]
1 ignored issue
show
Documentation introduced by
Should the return type not be array|string? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
237
	 */
238 38
	function process ($database, $data, $store_in_cache = false) {
239 38
		if (is_array($data)) {
240 2
			foreach ($data as &$d) {
241 2
				$d = $this->process($database, $d);
242
			}
243 2
			return $data;
244
		}
245 38
		return preg_replace_callback(
246 38
			'/^\{¶(\d+)\}$/',
247 38
			function ($input) use ($database, $store_in_cache) {
248 4
				return $this->get($database, $input[1], $store_in_cache);
249 38
			},
250
			$data
251
		);
252
	}
253
}
254