TextFormat   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 55
dl 0
loc 77
rs 10
c 1
b 0
f 0
wmc 18

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isCacheable() 0 2 1
F handle() 0 70 17
1
<?php
2
3
namespace Smarty\BlockHandler;
4
5
use Smarty\Smarty;
6
use Smarty\Template;
7
8
/**
9
 * Smarty {textformat}{/textformat} block plugin
10
 * Type:     block function
11
 * Name:     textformat
12
 * Purpose:  format text a certain way with preset styles
13
 *           or custom wrap/indent settings
14
 * Params:
15
 *
16
 * - style         - string (email)
17
 * - indent        - integer (0)
18
 * - wrap          - integer (80)
19
 * - wrap_char     - string ("\n")
20
 * - indent_char   - string (" ")
21
 * - wrap_boundary - boolean (true)
22
 *
23
 * @param array                    $params   parameters
24
 * @param string                   $content  contents of the block
25
 * @param Template $template template object
26
 * @param boolean                  &$repeat  repeat flag
27
 *
28
 * @return string content re-formatted
29
 * @author Monte Ohrt <monte at ohrt dot com>
30
 * @throws \Smarty\Exception
31
 */
32
class TextFormat implements BlockHandlerInterface {
33
34
	public function handle($params, $content, Template $template, &$repeat) {
35
		if (is_null($content)) {
36
			return;
37
		}
38
		$style = null;
39
		$indent = 0;
40
		$indent_first = 0;
41
		$indent_char = ' ';
42
		$wrap = 80;
43
		$wrap_char = "\n";
44
		$wrap_cut = false;
45
		$assign = null;
46
		foreach ($params as $_key => $_val) {
47
			switch ($_key) {
48
				case 'style':
49
				case 'indent_char':
50
				case 'wrap_char':
51
				case 'assign':
52
					$$_key = (string)$_val;
53
					break;
54
				case 'indent':
55
				case 'indent_first':
56
				case 'wrap':
57
					$$_key = (int)$_val;
58
					break;
59
				case 'wrap_cut':
60
					$$_key = (bool)$_val;
61
					break;
62
				default:
63
					trigger_error("textformat: unknown attribute '{$_key}'");
64
			}
65
		}
66
		if ($style === 'email') {
0 ignored issues
show
introduced by
The condition $style === 'email' is always false.
Loading history...
67
			$wrap = 72;
68
		}
69
		// split into paragraphs
70
		$_paragraphs = preg_split('![\r\n]{2}!', $content);
71
		foreach ($_paragraphs as &$_paragraph) {
72
			if (!$_paragraph) {
73
				continue;
74
			}
75
			// convert mult. spaces & special chars to single space
76
			$_paragraph =
77
				preg_replace(
78
					array(
79
						'!\s+!' . Smarty::$_UTF8_MODIFIER,
80
						'!(^\s+)|(\s+$)!' . Smarty::$_UTF8_MODIFIER
81
					),
82
					array(
83
						' ',
84
						''
85
					),
86
					$_paragraph
87
				);
88
			// indent first line
89
			if ($indent_first > 0) {
90
				$_paragraph = str_repeat($indent_char, $indent_first) . $_paragraph;
91
			}
92
			// wordwrap sentences
93
			$_paragraph = smarty_mb_wordwrap($_paragraph, $wrap - $indent, $wrap_char, $wrap_cut);
94
			// indent lines
95
			if ($indent > 0) {
96
				$_paragraph = preg_replace('!^!m', str_repeat($indent_char, $indent), $_paragraph);
97
			}
98
		}
99
		$_output = implode($wrap_char . $wrap_char, $_paragraphs);
100
		if ($assign) {
0 ignored issues
show
introduced by
$assign is of type null, thus it always evaluated to false.
Loading history...
101
			$template->assign($assign, $_output);
102
		} else {
103
			return $_output;
104
		}
105
	}
106
107
	public function isCacheable(): bool {
108
		return true;
109
	}
110
}