1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: robbie |
5
|
|
|
* Date: 01/12/14 |
6
|
|
|
* Time: 01:36 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace RobbieP\ZbarQrdecoder\Result; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use RobbieP\ZbarQrdecoder\Result\Parser\ParserXML; |
13
|
|
|
|
14
|
|
|
class Result { |
15
|
|
|
|
16
|
|
|
const FORMAT_QR_CODE = 'QR_CODE'; |
17
|
|
|
const FORMAT_EAN_13 = 'EAN_13'; |
18
|
|
|
const FORMAT_CODE_39 = 'CODE_39'; |
19
|
|
|
const FORMAT_CODE_128 = 'CODE_128'; |
20
|
|
|
const FORMAT_INTERLEAVED_2_5 = 'INTERLEAVED_2_5'; |
21
|
|
|
|
22
|
|
|
public $code; |
23
|
|
|
public $text; |
24
|
|
|
public $format; |
25
|
|
|
|
26
|
|
|
private static $prefix = [ |
27
|
|
|
self::FORMAT_QR_CODE => "QR-Code", |
28
|
|
|
self::FORMAT_EAN_13 => "EAN-13", |
29
|
|
|
self::FORMAT_CODE_39 => "CODE-39", |
30
|
|
|
self::FORMAT_CODE_128 => "CODE-128", |
31
|
|
|
self::FORMAT_INTERLEAVED_2_5 => "I2/5", |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
protected $parser; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Pass in the raw result from the process |
38
|
|
|
* @param $result |
39
|
|
|
* @param $parser |
40
|
|
|
*/ |
41
|
|
|
function __construct($result, $parser = null) { |
|
|
|
|
42
|
|
|
$this->parser = !is_null($parser) ? $parser : new ParserXML(); |
43
|
|
|
if(!empty($result)) |
44
|
|
|
{ |
45
|
|
|
$parsed = $this->parser->parse($result); |
46
|
|
|
// TODO: Tidy/Refactor this bit of code |
47
|
|
|
$this->text($parsed['text']); |
48
|
|
|
$this->format($parsed['format']); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Will determine what type of barcode and set the correct text response |
54
|
|
|
* @param $text |
55
|
|
|
*/ |
56
|
|
|
public function text($text) |
57
|
|
|
{ |
58
|
|
|
$this->text = $text; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Format of the bar code |
63
|
|
|
* @param $format |
64
|
|
|
*/ |
65
|
|
|
public function format($format) |
66
|
|
|
{ |
67
|
|
|
$this->format = @array_search($format, self::$prefix); |
68
|
|
|
if($this->format) { |
69
|
|
|
$this->code = 200; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Just returns the text output |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
public function __toString() |
78
|
|
|
{ |
79
|
|
|
if(!empty($this->text)) { |
80
|
|
|
return $this->text; |
81
|
|
|
} |
82
|
|
|
return 'No result'; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
} |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.