1<?php
2/**
3 * BaconQrCode
4 *
5 * @link      http://github.com/Bacon/BaconQrCode For the canonical source repository
6 * @copyright 2013 Ben 'DASPRiD' Scholzen
7 * @license   http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
8 */
9
10namespace BaconQrCode\Renderer\Text;
11
12use BaconQrCode\Exception;
13use BaconQrCode\Encoder\QrCode;
14use BaconQrCode\Renderer\RendererInterface;
15
16/**
17 * Plaintext renderer.
18 */
19class Plain implements RendererInterface
20{
21    /**
22     * Margin around the QR code, also known as quiet zone.
23     *
24     * @var integer
25     */
26    protected $margin = 1;
27
28    /**
29     * Char used for full block.
30     *
31     * UTF-8 FULL BLOCK (U+2588)
32     *
33     * @var  string
34     * @link http://www.fileformat.info/info/unicode/char/2588/index.htm
35     */
36    protected $fullBlock = "\xE2\x96\x88";
37
38    /**
39     * Char used for empty space
40     *
41     * @var string
42     */
43    protected $emptyBlock = ' ';
44
45    /**
46     * Set char used as full block (occupied space, "black").
47     *
48     * @param string $fullBlock
49     */
50    public function setFullBlock($fullBlock)
51    {
52        $this->fullBlock = $fullBlock;
53    }
54
55    /**
56     * Get char used as full block (occupied space, "black").
57     *
58     * @return string
59     */
60    public function getFullBlock()
61    {
62        return $this->fullBlock;
63    }
64
65    /**
66     * Set char used as empty block (empty space, "white").
67     *
68     * @param string $emptyBlock
69     */
70    public function setEmptyBlock($emptyBlock)
71    {
72        $this->emptyBlock = $emptyBlock;
73    }
74
75    /**
76     * Get char used as empty block (empty space, "white").
77     *
78     * @return string
79     */
80    public function getEmptyBlock()
81    {
82        return $this->emptyBlock;
83    }
84
85    /**
86     * Sets the margin around the QR code.
87     *
88     * @param  integer $margin
89     * @return AbstractRenderer
90     * @throws Exception\InvalidArgumentException
91     */
92    public function setMargin($margin)
93    {
94        if ($margin < 0) {
95            throw new Exception\InvalidArgumentException('Margin must be equal to greater than 0');
96        }
97
98        $this->margin = (int) $margin;
99
100        return $this;
101    }
102
103    /**
104     * Gets the margin around the QR code.
105     *
106     * @return integer
107     */
108    public function getMargin()
109    {
110        return $this->margin;
111    }
112
113    /**
114     * render(): defined by RendererInterface.
115     *
116     * @see    RendererInterface::render()
117     * @param  QrCode $qrCode
118     * @return string
119     */
120    public function render(QrCode $qrCode)
121    {
122        $result = '';
123        $matrix = $qrCode->getMatrix();
124        $width  = $matrix->getWidth();
125
126        // Top margin
127        for ($x = 0; $x < $this->margin; $x++) {
128            $result .= str_repeat($this->emptyBlock, $width + 2 * $this->margin)."\n";
129        }
130
131        // Body
132        $array = $matrix->getArray();
133
134        foreach ($array as $row) {
135            $result .= str_repeat($this->emptyBlock, $this->margin); // left margin
136            foreach ($row as $byte) {
137                $result .= $byte ? $this->fullBlock : $this->emptyBlock;
138            }
139            $result .= str_repeat($this->emptyBlock, $this->margin); // right margin
140            $result .= "\n";
141        }
142
143        // Bottom margin
144        for ($x = 0; $x < $this->margin; $x++) {
145            $result .= str_repeat($this->emptyBlock, $width + 2 * $this->margin)."\n";
146        }
147
148        return $result;
149    }
150}
151