Update code_sniffer build.xml file to be executable on our system
[phpbb.git] / phpBB / includes / formatted_text.php
blobca801bfea7f1055469f168f91de9e65aaf35768f
1 <?php
2 /**
4 * @package phpBB3
5 * @version $Id$
6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
9 */
11 /**
12 * @ignore
14 if (!defined('IN_PHPBB'))
16 exit;
19 /**
20 * Formatted text class to handle any text that can contain BBCodes, smilies, magic URLs or under word censor.
22 class formatted_text
24 /**
25 * Unformated text.
27 * @var string
29 private $text;
31 /**
32 * Internal representation (first_pass data).
34 * @var string
36 private $meta;
38 /**
39 * Formatting options as bit flag.
41 * @see bbcode_parser
43 * @var int
45 private $flags;
47 /**
48 * Compiled $text. For dispaly.
50 * @var string
52 private $compiled;
54 /**
55 * Set to true if text, meta or flags have been changed, false otherwise.
57 * @var bool
59 private $changed;
61 /**
62 * DB table to update
64 * @var string
66 private $update_table = '';
68 /**
69 * Column in $update_table to update.
71 * @var string
73 private $update_column = '';
75 /**
76 * Where clause for auto update.
78 * @var string
80 private $update_where = '';
82 /**
83 * Creates a new instance.
85 * @param string $text
86 * @param string $meta
87 * @param int $flags
89 public function __construct($text, $meta = '', $flags = 0)
91 $this->text = $text;
92 $this->meta = $meta;
93 $this->flags = $flags;
94 $this->compiled = '';
96 if ($meta == '')
98 $this->changed = true;
100 else
102 $this->changed = false;
106 public function __destruct()
108 if ($this->changed && $this->update_table)
110 $this->to_db($this->update_table, $this->update_column, $this->update_where);
115 * Convieniently initialize a formatted_text object from
116 * a database result set. The array must contain the following indexes:
117 * $column, {$column}_meta and {$column}_flags
120 * @param array $data
121 * @param string $column
122 * @return formatted_text
124 public static function from_db_data(array $data, $column)
126 return new formatted_text($data[$column], $data[$column . '_meta'], (int) $data[$column . '_flags']);
130 * Returns the $text formatted, ready to be displayed on a webpage.
132 * @return string
134 public function to_display()
136 $this->set_compiled();
137 return $this->compiled;
141 * Updates $table, sets $column, {$column}_meta and {$column}_flags.
142 * All 3 columns must exist.
144 * @param string $table
145 * @param string $column
146 * @param string $where
147 * @return bool
149 public function to_db($table, $column, $where = '1')
151 $this->changed = false;
153 $sql = 'UPDATE ' . $table . ' SET ' . phpbb::$db->sql_build_query('UPDATE', $this->to_db_data($column))
154 . ' WHERE ' . $where;
155 return (bool) phpbb::$db->sql_query($sql);
159 * Returns an array containing $column, {$column}_meta and {$column}_flags
160 * indexes to be used with query generating functions.
162 * @param string $column
163 * @return array
165 public function to_db_data($column)
167 $this->set_meta();
168 return array($column => $this->text, $column . '_meta' => $this->meta, $column . '_flags' => $this->flags);
172 * Enable automatic database update on
174 * @param string $table
175 * @param string $column
176 * @param string $where
178 public function set_auto_update($table, $column, $where = '1')
180 $this->update_table = $table;
181 $this->update_column = $column;
182 $this->update_where = $where;
186 * Sets $meta if not set.
188 private function set_meta()
190 if (strlen($this->meta))
192 return;
195 $parser = new phpbb_bbcode_parser;
196 $this->meta = $parser->first_pass($this->text);
200 * Sets $compiled if not set.
202 private function set_compiled()
204 $this->set_meta();
206 if (strlen($this->compiled))
208 return;
211 $parser = new phpbb_bbcode_parser;
212 $parser->set_flags($this->flags);
213 $this->compiled = $parser->second_pass($this->meta);
217 * Sets $text.
219 * @param string $text
221 public function set_text($text)
223 if ($this->text != $text)
225 $this->text = (string) $text;
226 $this->meta = '';
227 $this->compiled = '';
232 * Sets $flags.
234 * @param int $flags
236 public function set_flags($flags)
238 $flags = (int) $flags;
239 if ($this->flags != $flags)
241 $this->flags = $flags;
242 $this->compiled = '';
247 * Returns the current text.
249 * @return string
251 public function get_text()
253 return $this->text;
257 * Returns the current(!!) metadata.
259 * @return string
261 public function get_meta()
263 return $this->meta;
267 * Returns the current flags.
269 * @return int
271 public function get_flags()
273 return $this->flags;
277 * Returns true if $this is equal to $other.
278 * Objects are only equal if $text and $flags are equal.
280 * @param formatted_text $other
281 * @return bool
283 public function eq(formatted_text $other)
285 return $this->flags == $other->get_flags() && $this->text == $other->get_text();
289 * Cast to string. Object is represented by the formatted version of $text with respect to $flags.
291 * @return string
293 public function __toString()
295 return $this->to_display();