1 module ithox.qrcode.qrcodewriter;
2 
3 import ithox.qrcode.renderer.rendererinterface;
4 
5 import ithox.qrcode.encoder.encoder;
6 import ithox.qrcode.common.errorcorrectionlevel;
7 
8 /**
9 * QR code writer.
10 */
11 class QrCodeWriter
12 {
13     /**
14 	* Renderer instance.
15 	*
16 	* @var RendererInterface
17 	*/
18     protected RendererInterface renderer;
19 
20     this(RendererInterface irender)
21     {
22         this.renderer = irender;
23     }
24 
25     /**
26 	* Sets the renderer used to create a byte stream.
27 	*
28 	* @param  RendererInterface $renderer
29 	* @return Writer
30 	*/
31     public QrCodeWriter setRenderer(RendererInterface renderer)
32     {
33         this.renderer = renderer;
34         return this;
35     }
36     /**
37 	* Gets the renderer used to create a byte stream.
38 	*
39 	* @return RendererInterface
40 	*/
41     public RendererInterface getRenderer()
42     {
43         return this.renderer;
44     }
45 
46     /**
47 	* Writes QR code and returns it as string.
48 	*
49 	* Content is a string which *should* be encoded in UTF-8, in case there are
50 	* non ASCII-characters present.
51 	*
52 	* @param  string  $content
53 	* @param  string  $encoding
54 	* @param  integer $ecLevel
55 	* @return string
56 	* @throws Exception\InvalidArgumentException
57 	*/
58     public string writeString(string content, string encoding = Encoder.DEFAULT_BYTE_MODE_ECODING,
59             ErrorCorrectionLevel ecLevel = ErrorCorrectionLevel.L)
60     {
61         if (content.length == 0)
62         {
63             throw new Exception("Found empty contents");
64         }
65         auto qrCode = Encoder.encode(content, ecLevel, encoding);
66         return this.getRenderer().render(qrCode);
67     }
68 
69     /**
70 	* Writes QR code to a file.
71 	*
72 	* @see    Writer::writeString()
73 	* @param  string  $content
74 	* @param  string  $filename
75 	* @param  string  $encoding
76 	* @param  integer $ecLevel
77 	* @return void
78 	*/
79     public void writeFile(string content, string filename, string encoding = Encoder.DEFAULT_BYTE_MODE_ECODING,
80             ErrorCorrectionLevel ecLevel = ErrorCorrectionLevel.L)
81     {
82         import std.file;
83 
84         write(filename, this.writeString(content, encoding, ecLevel));
85         //file_put_contents($filename, $this->writeString($content, $encoding, $ecLevel));
86     }
87 }