|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This software package is licensed under AGPL or Commercial license. |
|
5
|
|
|
* |
|
6
|
|
|
* @package maslosoft/mangan |
|
7
|
|
|
* @licence AGPL or Commercial |
|
8
|
|
|
* @copyright Copyright (c) Piotr Masełkowski <[email protected]> |
|
9
|
|
|
* @copyright Copyright (c) Maslosoft |
|
10
|
|
|
* @copyright Copyright (c) Others as mentioned in code |
|
11
|
|
|
* @link https://maslosoft.com/mangan/ |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Maslosoft\Mangan\Transformers; |
|
15
|
|
|
|
|
16
|
|
|
use Dmtx\Reader; |
|
17
|
|
|
use Dmtx\Writer; |
|
18
|
|
|
use Maslosoft\Cli\Shared\Os; |
|
19
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
|
20
|
|
|
use Maslosoft\Addendum\Utilities\ClassChecker; |
|
21
|
|
|
use Maslosoft\Mangan\AspectManager; |
|
22
|
|
|
use Maslosoft\Mangan\Exceptions\ManganException; |
|
23
|
|
|
use Maslosoft\Mangan\Exceptions\TransformatorException; |
|
24
|
|
|
use Maslosoft\Mangan\Interfaces\Transformators\TransformatorInterface; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Datamatrix |
|
28
|
|
|
* |
|
29
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
|
30
|
|
|
*/ |
|
31
|
|
|
class Datamatrix implements TransformatorInterface |
|
32
|
|
|
{ |
|
33
|
|
|
const AspectDatamatrixFromModel = 'AspectDatamatrixFromModel'; |
|
34
|
|
|
const AspectDatamatrixToModel = 'AspectDatamatrixToModel'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Returns the given object as an associative array |
|
38
|
|
|
* @param AnnotatedInterface|object $model |
|
39
|
|
|
* @param string[] $fields Fields to transform |
|
40
|
|
|
* @return array an associative array of the contents of this object |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public static function fromModel(AnnotatedInterface $model, $fields = []) |
|
43
|
|
|
{ |
|
44
|
1 |
|
if(!ClassChecker::exists(Writer::class)) |
|
45
|
|
|
{ |
|
46
|
1 |
|
throw new ManganException('Missing php-dmtx library'); |
|
47
|
|
|
} |
|
48
|
|
|
assert(Os::commandExists('dmtxwrite')); |
|
49
|
|
|
AspectManager::addAspect($model, self::AspectDatamatrixFromModel); |
|
50
|
|
|
$data = YamlString::fromModel($model, $fields, 1, 1); |
|
51
|
|
|
AspectManager::removeAspect($model, self::AspectDatamatrixFromModel); |
|
52
|
|
|
return (new Writer())->encode($data)->dump(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Create document from datamatrix code |
|
57
|
|
|
* |
|
58
|
|
|
* @param mixed[] $data |
|
59
|
|
|
* @param string|object $className |
|
60
|
|
|
* @param AnnotatedInterface $instance |
|
61
|
|
|
* @return AnnotatedInterface |
|
62
|
|
|
* @throws TransformatorException |
|
63
|
|
|
*/ |
|
64
|
|
|
public static function toModel($data, $className = null, AnnotatedInterface $instance = null) |
|
65
|
|
|
{ |
|
66
|
|
|
if(!ClassChecker::exists(Reader::class)) |
|
67
|
|
|
{ |
|
68
|
|
|
throw new ManganException('Missing php-dmtx library'); |
|
69
|
|
|
} |
|
70
|
|
|
assert(Os::commandExists('dmtxread')); |
|
71
|
|
|
$data = (new Reader())->decode($data); |
|
72
|
|
|
AspectManager::addAspect($instance, self::AspectDatamatrixToModel); |
|
73
|
|
|
$model = YamlString::toModel($data, $className, $instance); |
|
74
|
|
|
AspectManager::removeAspect($instance, self::AspectDatamatrixToModel); |
|
75
|
|
|
AspectManager::removeAspect($model, self::AspectDatamatrixToModel); |
|
76
|
|
|
return $model; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|