Skip to content

Commit c347a9e

Browse files
committed
Use Media object instead of array
1 parent 5b2a21a commit c347a9e

File tree

6 files changed

+47
-22
lines changed

6 files changed

+47
-22
lines changed

src/Contract/Label.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818

1919
interface Label extends Job, \IteratorAggregate
2020
{
21-
public function getMedia(): array;
21+
public function getCopies(): int;
2222

2323
public function getCharset(): ?Charset;
2424

25-
public function getCopies(): int;
25+
public function getMedia(): Media;
2626
}

src/Contract/Media.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/**
4+
* This file is part of Appwilio LabelPrinter package.
5+
*
6+
* © appwilio (https://appwilio.com)
7+
* © JhaoDa (https://github.com/jhaoda)
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
13+
namespace PhpAidc\LabelPrinter\Contract;
14+
15+
use PhpAidc\LabelPrinter\Enum\Unit;
16+
17+
interface Media
18+
{
19+
public function getUnit(): ?Unit;
20+
21+
public function getWidth(): ?float;
22+
23+
public function getHeight(): ?float;
24+
}

src/Label/Label.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@
1919
use PhpAidc\LabelPrinter\Enum\Direction;
2020
use PhpAidc\LabelPrinter\Contract\Command;
2121
use PhpAidc\LabelPrinter\Contract\Condition;
22+
use PhpAidc\LabelPrinter\Contract\Media as MediaContract;
2223
use PhpAidc\LabelPrinter\Contract\Label as LabelContract;
2324

2425
final class Label implements LabelContract
2526
{
26-
private $media;
27-
2827
/** @var int */
2928
private $copies = 1;
3029

@@ -34,6 +33,9 @@ final class Label implements LabelContract
3433
/** @var Direction|null */
3534
private $direction;
3635

36+
/** @var Media */
37+
private $media;
38+
3739
/** @var Command[]|Condition[] */
3840
private $statements = [];
3941

@@ -48,7 +50,7 @@ public function __construct(?Unit $unit = null, ?float $width = null, ?float $he
4850
throw new \InvalidArgumentException();
4951
}
5052

51-
$this->media = \compact('unit', 'width', 'height');
53+
$this->media = new Media($unit, $width, $height);
5254
}
5355

5456
public function add(Command $command)
@@ -94,7 +96,7 @@ public function getDirection(): ?Direction
9496
return $this->direction;
9597
}
9698

97-
public function getMedia(): array
99+
public function getMedia(): MediaContract
98100
{
99101
return $this->media;
100102
}

src/Label/Media.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
namespace PhpAidc\LabelPrinter\Label;
1616

1717
use PhpAidc\LabelPrinter\Enum\Unit;
18+
use PhpAidc\LabelPrinter\Contract\Media as MediaContract;
1819

19-
final class Media
20+
final class Media implements MediaContract
2021
{
2122
/** @var Unit|null */
2223
private $unit;

src/Language/Fingerprint.php

+6-7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use PhpAidc\LabelPrinter\Command\ExternalImage;
2424
use PhpAidc\LabelPrinter\Command\InternalImage;
2525
use PhpAidc\LabelPrinter\Contract\Label;
26+
use PhpAidc\LabelPrinter\Contract\Media;
2627
use PhpAidc\LabelPrinter\Contract\Command;
2728
use PhpAidc\LabelPrinter\Contract\Language;
2829
use PhpAidc\LabelPrinter\Enum\Unit;
@@ -100,16 +101,14 @@ private function translateCharset(?Charset $charset): iterable
100101
}
101102
}
102103

103-
private function translateMedia(array $media): iterable
104+
private function translateMedia(Media $media): iterable
104105
{
105-
['unit' => $unit, 'width' => $width, 'height' => $height] = $media;
106-
107-
if ($width) {
108-
yield \sprintf('SETUP "MEDIA,MEDIA SIZE,WIDTH,%d'.self::EOC, $this->valueToDots($width, $unit));
106+
if ($media->getWidth()) {
107+
yield \sprintf('SETUP "MEDIA,MEDIA SIZE,WIDTH,%d'.self::EOC, $this->valueToDots($media->getWidth(), $media->getUnit()));
109108
}
110109

111-
if ($height) {
112-
yield \sprintf('SETUP "MEDIA,MEDIA SIZE,HEIGHT,%d'.self::EOC, $this->valueToDots($height, $unit));
110+
if ($media->getHeight()) {
111+
yield \sprintf('SETUP "MEDIA,MEDIA SIZE,HEIGHT,%d'.self::EOC, $this->valueToDots($media->getHeight(), $media->getUnit()));
113112
}
114113
}
115114

src/Language/Tspl.php

+7-8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use PhpAidc\LabelPrinter\Command\TextBlock;
2323
use PhpAidc\LabelPrinter\Command\InternalImage;
2424
use PhpAidc\LabelPrinter\Contract\Label;
25+
use PhpAidc\LabelPrinter\Contract\Media;
2526
use PhpAidc\LabelPrinter\Contract\Command;
2627
use PhpAidc\LabelPrinter\Contract\Language;
2728
use PhpAidc\LabelPrinter\Enum\Unit;
@@ -83,20 +84,18 @@ private function translateCharset(?Charset $charset): iterable
8384
}
8485
}
8586

86-
private function translateMedia(array $media): iterable
87+
private function translateMedia(Media $media): iterable
8788
{
88-
['unit' => $unit, 'width' => $width, 'height' => $height] = $media;
89-
90-
if ($width && $height === null) {
91-
yield \sprintf('SIZE %s'.self::EOC, $this->valueWithUnit($width, $unit));
89+
if ($media->getWidth() && $media->getHeight() === null) {
90+
yield \sprintf('SIZE %s'.self::EOC, $this->valueWithUnit($media->getWidth(), $media->getUnit()));
9291

9392
return;
9493
}
9594

96-
if ($width && $height) {
95+
if ($media->getWidth() && $media->getHeight()) {
9796
yield \vsprintf('SIZE %s,%s'.self::EOC, [
98-
$this->valueWithUnit($width, $unit),
99-
$this->valueWithUnit($height, $unit),
97+
$this->valueWithUnit($media->getWidth(), $media->getUnit()),
98+
$this->valueWithUnit($media->getHeight(), $media->getUnit()),
10099
]);
101100
}
102101
}

0 commit comments

Comments
 (0)