Passed
Push — main ( 6b9f6f...a1f051 )
by smiley
02:03
created

QRMarkup::prepareModuleValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Class QRMarkup
4
 *
5
 * @created      17.12.2016
6
 * @author       Smiley <[email protected]>
7
 * @copyright    2016 Smiley
8
 * @license      MIT
9
 */
10
11
namespace chillerlan\QRCode\Output;
12
13
use function is_string, preg_match, strip_tags, trim;
14
15
/**
16
 * Abstract for markup types: HTML, SVG, ... XML anyone?
17
 */
18
abstract class QRMarkup extends QROutputAbstract{
19
20
	/**
21
	 * note: we're not necessarily validating the several values, just checking the general syntax
22
	 * note: css4 colors are not included
23
	 *
24
	 * @todo: XSS proof
25
	 *
26
	 * @see https://developer.mozilla.org/en-US/docs/Web/CSS/color_value
27
	 * @inheritDoc
28
	 */
29
	public static function moduleValueIsValid($value):bool{
30
31
		if(!is_string($value)){
32
			return false;
33
		}
34
35
		$value = trim(strip_tags($value), " '\"\r\n\t");
36
37
		// hex notation
38
		// #rgb(a)
39
		// #rrggbb(aa)
40
		if(preg_match('/^#([\da-f]{3}){1,2}$|^#([\da-f]{4}){1,2}$/i', $value)){
41
			return true;
42
		}
43
44
		// css: hsla/rgba(...values)
45
		if(preg_match('#^(hsla?|rgba?)\([\d .,%/]+\)$#i', $value)){
46
			return true;
47
		}
48
49
		// predefined css color
50
		if(preg_match('/^[a-z]+$/i', $value)){
51
			return true;
52
		}
53
54
		return false;
55
	}
56
57
	/**
58
	 * @inheritDoc
59
	 */
60
	protected function prepareModuleValue($value):string{
61
		return trim(strip_tags($value), " '\"\r\n\t");
62
	}
63
64
	/**
65
	 * @inheritDoc
66
	 */
67
	protected function getDefaultModuleValue(bool $isDark):string{
68
		return ($isDark) ? $this->options->markupDark : $this->options->markupLight;
69
	}
70
71
	/**
72
	 * @inheritDoc
73
	 */
74
	public function dump(string $file = null):string{
75
		$data = $this->createMarkup($file !== null);
76
77
		$this->saveToFile($data, $file);
78
79
		return $data;
80
	}
81
82
	/**
83
	 * returns a string with all css classes for the current element
84
	 */
85
	abstract protected function getCssClass(int $M_TYPE):string;
86
87
	/**
88
	 *
89
	 */
90
	abstract protected function createMarkup(bool $saveToFile):string;
91
}
92