Standard   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 20
eloc 29
c 1
b 0
f 0
dl 0
loc 121
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A attr() 0 13 5
A transform() 0 3 1
A url() 0 11 3
A xml() 0 11 4
A html() 0 12 4
A js() 0 7 3
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2025
6
 * @package Base
7
 * @subpackage View
8
 */
9
10
11
namespace Aimeos\Base\View\Helper\Encoder;
12
13
use Aimeos\Base\Str;
14
15
16
/**
17
 * View helper class for encoding data into the output.
18
 *
19
 * @package Base
20
 * @subpackage View
21
 */
22
class Standard
23
	extends \Aimeos\Base\View\Helper\Base
24
	implements \Aimeos\Base\View\Helper\Encoder\Iface
25
{
26
	/**
27
	 * Returns the encoder.
28
	 *
29
	 * @return \Aimeos\Base\View\Helper\Encoder\Iface Encoder object
30
	 */
31
	public function transform() : Iface
32
	{
33
		return $this;
34
	}
35
36
37
	/**
38
	 * Escapes strings for HTML/XML attributes.
39
	 * All attribute values must be surrounded by " (double quote)
40
	 *
41
	 * @param mixed $value Attribute string
42
	 * @param int $trust Zero to distrust the input, one (1) if you trust in it
43
	 * @param string $replace Replace whitespace characters by given string
44
	 * @return string Escaped attribute string
45
	 */
46
	public function attr( $value, int $trust = self::TAINT, string $replace = '' ) : string
47
	{
48
		if( $value !== null && !is_scalar( $value ) ) {
49
			$value = json_encode( $value, JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_TAG );
50
		} elseif( $replace != '' ) {
51
			$value = str_replace( [" ", "\v", "\t", "\r", "\n", "\f"], $replace, $value );
52
		}
53
54
		if( $trust === self::TRUST ) {
55
			return (string) $value;
56
		}
57
58
		return str_replace( ['"', '\'', '`'], ['&quot;', '&apos;', '&#96;'], (string) $value );
59
	}
60
61
62
	/**
63
	 * Escapes strings for HTML.
64
	 *
65
	 * @param mixed $value HTML string
66
	 * @param int $trust Zero to distrust the input, one (1) if you trust in it
67
	 * @return string Escaped HTML string
68
	 */
69
	public function html( $value, int $trust = self::TAINT ) : string
70
	{
71
		if( $value !== null && !is_scalar( $value ) ) {
72
			$value = json_encode( $value, JSON_HEX_AMP );
73
		}
74
75
		if( $trust === self::TRUST ) {
76
			return (string) $value;
77
		}
78
79
		// Avoid client side template injection
80
		return str_replace( ['{', '}'], '', Str::html( strip_tags( (string) $value ), ENT_QUOTES ) );
81
	}
82
83
84
	/**
85
	 * Escapes strings for JS strings.
86
	 * All strings values must be surrounded by ' (single quote)
87
	 *
88
	 * @param mixed $value Unescaped string
89
	 * @return string Escaped string for JS
90
	 */
91
	public function js( $value ) : string
92
	{
93
		if( $value !== null && !is_scalar( $value ) ) {
94
			$value = json_encode( $value, JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_TAG );
95
		}
96
97
		return str_replace( ['"', '\'', '`'], ['&quot;', '&apos;', '\&#96;'], (string) $value );
98
	}
99
100
101
	/**
102
	 * Escapes strings for XML.
103
	 * All node values must be surrounded by <![CDATA[...]]>
104
	 *
105
	 * @param mixed $value XML string
106
	 * @param int $trust Zero to distrust the input, one (1) if you trust in it
107
	 * @return string Escaped XML string
108
	 */
109
	public function xml( $value, int $trust = self::TAINT ) : string
110
	{
111
		if( $value !== null && !is_scalar( $value ) ) {
112
			$value = json_encode( $value, JSON_HEX_AMP );
113
		}
114
115
		if( $trust === self::TRUST ) {
116
			return (string) $value;
117
		}
118
119
		return Str::html( (string) $value, ENT_XML1 );
120
	}
121
122
123
	/**
124
	 * Escapes strings for URLs.
125
	 *
126
	 * @param string $value URI/URL string
127
	 * @param int $trust Zero to distrust the input, one (1) if you trust in it
128
	 * @param bool $strip Stip HTML tags if they are part of the input
129
	 * @param string[] $replace Associative list of characters or strings that should be replaced
130
	 * @return string Escaped URI/URL string
131
	 */
132
	public function url( string $value, int $trust = self::TAINT, bool $strip = true, array $replace = [' ' => '_'] ) : string
133
	{
134
		if( $strip !== false ) {
135
			$value = strip_tags( $value );
136
		}
137
138
		foreach( $replace as $key => $val ) {
139
			$value = str_replace( $key, $val, $value );
140
		}
141
142
		return urlencode( $value );
143
	}
144
}
145