Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
barcodegenerator.php
1<?php
2
4
8
10{
11 private Barcode $barcode;
12
13 private const DEFAULT_W = 380;
14 private const DEFAULT_H = 380;
15 private const DEFAULT_P = 0;
16 private const DEFAULT_WQ = 0;
17
18 public function __construct(?array $options = null)
19 {
20 if ($this->includeUiModule())
21 {
22 $this->createBarcode($options);
23 }
24 }
25
26 private function createBarcode(?array $options = null): void
27 {
28 $options =
29 is_null($options)
30 ? [
31 'w' => self::DEFAULT_W,
32 'h' => self::DEFAULT_H,
33 'p' => self::DEFAULT_P,
34 'wq' => self::DEFAULT_WQ,
35 ]
36 : array_intersect_key($options, array_flip(self::getAllowedOptions()))
37 ;
38
39 $this->barcode = new Barcode();
40 $this->barcode
43 ->options($options)
44 ;
45 }
46
47 private static function getAllowedOptions(): array
48 {
49 return [
50 'w',
51 'h',
52 'p',
53 'wq',
54 ];
55 }
56
57 public function generate(string $data): ?string
58 {
59 $renderData = null;
60
61 if ($this->includeUiModule())
62 {
63 $renderData = $this->barcode->render($data);
64 }
65
66 return $renderData ?: null;
67 }
68
69 private function includeUiModule(): bool
70 {
71 return Loader::includeModule('ui');
72 }
73}