Completed
Push — master ( 1a2d4b...99084f )
by smiley
03:12
created

QRString   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 6
Bugs 0 Features 2
Metric Value
wmc 14
c 6
b 0
f 2
lcom 1
cbo 3
dl 0
loc 76
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
C dump() 0 47 11
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
use chillerlan\QRCode\Data\QRCodeDataException;
15
use chillerlan\QRCode\QRCode;
16
17
/**
18
 *
19
 */
20
class QRString extends QROutputBase implements QROutputInterface{
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
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
46
	 */
47
	public function dump(){
48
49
		if($this->options->type === QRCode::OUTPUT_STRING_JSON){
50
			return json_encode($this->matrix);
51
		}
52
53
		else if($this->options->type === QRCode::OUTPUT_STRING_TEXT){
54
			$text = '';
55
56
			foreach($this->matrix as $row){
57
				foreach($row as $col){
58
					$text .= $col
59
						? $this->options->textDark
60
						: $this->options->textLight;
61
				}
62
63
				$text .= $this->options->textNewline;
64
			}
65
66
			return $text;
67
		}
68
69
		else if($this->options->type === QRCode::OUTPUT_STRING_HTML){
70
			$html = '';
71
72
			foreach($this->matrix as $row){
73
				// in order to not bloat the output too much, we use the shortest possible valid HTML tags
74
				$html .= '<'.$this->options->htmlRowTag.'>';
75
				foreach($row as $col){
76
					$tag = $col
77
						? 'b'  // dark
78
						: 'i'; // light
79
80
					$html .= '<'.$tag.'></'.$tag.'>';
81
				}
82
83
				if(!(bool)$this->options->htmlOmitEndTag){
84
					$html .= '</'.$this->options->htmlRowTag.'>';
85
				}
86
87
				$html .= PHP_EOL;
88
			}
89
90
			return $html;
91
		}
92
93
	}
94
95
}
96