Completed
Push — master ( dcfcc4...3c3c44 )
by Niels
02:20
created

DmplBuilder::setMeasuringUnit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Nielsiano\DmplBuilder;
4
5
/**
6
 * Class DmplBuilder
7
 *
8
 * @package Nielsiano\DmplBuilder
9
 */
10
class DmplBuilder
11
{
12
13
    /**
14
     * Generated DM/PL command instructions.
15
     *
16
     * @var array
17
     */
18
    protected $instructions = [];
19
20
    /**
21
     * @var bool
22
     */
23
    protected $cutOff = false;
24
25
    /**
26
     * @var bool
27
     */
28
    protected $flipAxes = false;
29
30
    /**
31
     * @var mixed
32
     */
33
    protected $measuringUnit = 'M';
34
35
    const KISS_CUT = 50;
36
    const FLEXCUT_PEN = 6;
37
    const REGULAR_PEN = 0;
38
    const CUT_THROUGH = 100;
39
40
    /**
41
     * Adds a new plot of x and y to machine instructions.
42
     *
43
     * @param int $x
44
     * @param int $y
45
     * @return $this
46
     */
47
    public function plot(int $x, int $y)
48
    {
49
        array_map([$this, 'pushCommand'], $this->flipAxes ? [$y, $x] : [$x, $y]);
50
51
        return $this;
52
    }
53
54
    /**
55
     * Changes the pen of the plotter.
56
     *
57
     * @param int $pen
58
     * @return $this
59
     */
60
    public function changePen(int $pen)
61
    {
62
        if (! in_array($pen, range(0, 6))) {
63
            throw new \InvalidArgumentException(sprintf('[%d] is not a valid Pen.', $pen));
64
        }
65
66
        return $this->pushCommand(sprintf('P%d;', $pen));
67
    }
68
69
    /**
70
     * Compiles a string in DMPL format with machine instructions.
71
     *
72
     * @return string
73
     */
74
    public function compileDmpl(): string
75
    {
76
        $init = sprintf(';: EC%s,U H L0,P0;A100,100,R,', $this->measuringUnit);
77
78
        $this->pushCommand($this->cutOff ? ';:c,e' : 'e');
79
80
        return $init . implode(',', $this->instructions);
81
    }
82
83
    /**
84
     * Pushes a command to the instructions.
85
     *
86
     * @param string $command
87
     * @return $this
88
     */
89
    public function pushCommand(string $command)
90
    {
91
        $this->instructions[] = $command;
92
93
        return $this;
94
    }
95
96
    /**
97
     * Lifts the pen up.
98
     *
99
     * @return $this
100
     */
101
    public function penUp()
102
    {
103
        return $this->pushCommand('U');
104
    }
105
106
    /**
107
     * Pushes the pen down on paper.
108
     *
109
     * @return $this
110
     */
111
    public function penDown()
112
    {
113
        return $this->pushCommand('D');
114
    }
115
116
    /**
117
     * Changes the plotter pen to use flexcut.
118
     *
119
     * @return $this
120
     */
121
    public function flexCut()
122
    {
123
        return $this->changePen(self::FLEXCUT_PEN);
124
    }
125
126
    /**
127
     * Change to the regular plotter pen.
128
     *
129
     * @return $this
130
     */
131
    public function regularCut()
132
    {
133
        return $this->changePen(self::REGULAR_PEN);
134
    }
135
136
    /**
137
     * Changes the pen pressure in gram.
138
     *
139
     * @param int $gramPressure
140
     * @return $this
141
     */
142
    public function pressure(int $gramPressure)
143
    {
144
        return $this->pushCommand(sprintf('BP%d;', $gramPressure));
145
    }
146
147
    /**
148
     * Specifies measuring unit.
149
     * 1 selects 0.001 inch
150
     * 5 selects 0.005 inch
151
     * M selects 0.1 mm
152
     *
153
     * @param $unit
154
     * @return $this
155
     */
156
    public function setMeasuringUnit($unit)
157
    {
158
        $this->measuringUnit = $unit;
159
160
        return $this;
161
    }
162
163
    /**
164
     * Changes the plotter velocity.
165
     *
166
     * @param int $velocity
167
     * @return $this
168
     */
169
    public function velocity(int $velocity)
170
    {
171
        return $this->pushCommand(sprintf('V%d;', $velocity));
172
    }
173
174
    /**
175
     * Flips the x, y coordinates.
176
     *
177
     * @return $this
178
     */
179
    public function flipAxes()
180
    {
181
        $this->flipAxes = true;
182
183
        return $this;
184
    }
185
186
    /**
187
     * Cuts off paper when a operation finishes.
188
     *
189
     * @return $this
190
     */
191
    public function cutOff()
192
    {
193
        $this->cutOff = true;
194
195
        return $this;
196
    }
197
198
}
199