TranslationDTODataTransformer::translationClass()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 1
1
<?php
2
3
/*
4
 * This file is part of the CMS Kernel package.
5
 *
6
 * Copyright (c) 2016-present LIN3S <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LIN3S\CMSKernel\Application\DataTransformer\Translation;
13
14
use LIN3S\CMSKernel\Domain\Model\Translation\Translation;
15
16
/**
17
 * @author Beñat Espiña <[email protected]>
18
 */
19
abstract class TranslationDTODataTransformer implements TranslationDataTransformer
20
{
21
    protected $translation;
22
23
    abstract protected function translationClass();
24
25
    abstract protected function serialize();
26
27
    final public function write(Translation $translation)
28
    {
29
        $translationClass = $this->translationClass();
30
        if (!$translation instanceof $translationClass) {
31
            throw new \InvalidArgumentException(
32
                sprintf('Expected instance of %s, %s given', $translationClass, get_class($translation))
33
            );
34
        }
35
        $this->translation = $translation;
36
    }
37
38
    final public function read()
39
    {
40
        if (null === $this->translation) {
41
            return [];
42
        }
43
44
        return $this->serialize();
45
    }
46
}
47