Completed
Push — master ( 3cb15c...9044bc )
by smiley
02:37
created

QRString   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 7
Bugs 0 Features 2
Metric Value
wmc 16
c 7
b 0
f 2
lcom 1
cbo 3
dl 0
loc 87
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
A dump() 0 11 4
A toString() 0 15 4
B toHTML() 0 24 5
1
<?php
2
/**
3
 * Class QRString
4
 *
5
 * @filesource   QRString.php
6
 * @created      05.12.2015
7
 * @package      chillerlan\QRCode\Output
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2015 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\QRCode\Output;
14
15
use chillerlan\QRCode\QRCode;
16
17
/**
18
 *
19
 */
20
class QRString extends QROutputAbstract{
21
22
	/**
23
	 * @var \chillerlan\QRCode\Output\QRStringOptions
24
	 */
25
	protected $options;
26
27
	/**
28
	 * @var \chillerlan\QRCode\Output\QRStringOptions $outputOptions
29
	 * @throws \chillerlan\QRCode\Output\QRCodeOutputException
30
	 */
31
	public function __construct(QRStringOptions $outputOptions = null){
32
		$this->options = $outputOptions;
33
34
		if(!$this->options instanceof QRStringOptions){
35
			$this->options = new QRStringOptions;
36
		}
37
38
		if(!in_array($this->options->type, [QRCode::OUTPUT_STRING_TEXT, QRCode::OUTPUT_STRING_JSON, QRCode::OUTPUT_STRING_HTML], true)){
39
			throw new QRCodeOutputException('Invalid string output type!');
40
		}
41
42
	}
43
44
	/**
45
	 * @return string
46
	 */
47
	public function dump(){
48
49
		switch($this->options->type){
50
			case QRCode::OUTPUT_STRING_JSON: return json_encode($this->matrix);
2 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
51
			case QRCode::OUTPUT_STRING_TEXT: return $this->toString();
2 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
52
			case QRCode::OUTPUT_STRING_HTML:
53
			default:
54
				return $this->toHTML();
55
		}
56
57
	}
58
59
	/**
60
	 * @return string
61
	 */
62
	protected function toString(){
63
		$str = '';
64
65
		foreach($this->matrix as $row){
66
			foreach($row as $col){
67
				$str .= $col
68
					? $this->options->textDark
69
					: $this->options->textLight;
70
			}
71
72
			$str .= $this->options->textNewline;
73
		}
74
75
		return $str;
76
	}
77
78
	/**
79
	 * @return string
80
	 */
81
	protected function toHTML(){
82
		$html = '';
83
84
		foreach($this->matrix as $row){
85
			// in order to not bloat the output too much, we use the shortest possible valid HTML tags
86
			$html .= '<'.$this->options->htmlRowTag.'>';
87
88
			foreach($row as $col){
89
				$tag = $col
90
					? 'b'  // dark
91
					: 'i'; // light
92
93
				$html .= '<'.$tag.'></'.$tag.'>';
94
			}
95
96
			if(!(bool)$this->options->htmlOmitEndTag){
97
				$html .= '</'.$this->options->htmlRowTag.'>';
98
			}
99
100
			$html .= PHP_EOL;
101
		}
102
103
		return $html;
104
	}
105
106
}
107