BasicSerializer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * @copyright  Daniel Król <[email protected]>
4
 * @license MIT
5
 * @package Mleko\Alchemist
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
declare(strict_types=1);
11
12
namespace Mleko\Alchemist\Serializer;
13
14
15
use Mleko\Alchemist\Encoder;
16
use Mleko\Alchemist\Normalizer;
17
use Mleko\Alchemist\Serializer;
18
use Mleko\Alchemist\Type;
19
20
class BasicSerializer implements Serializer
21
{
22
    /** @var Normalizer */
23
    private $normalizer;
24
    /** @var Encoder */
25
    private $encoder;
26
27
    /**
28
     * BaseSerializer constructor.
29
     * @param Normalizer $normalizer
30
     * @param Encoder $encoder
31
     */
32 2
    public function __construct(Normalizer $normalizer, Encoder $encoder) {
33 2
        $this->normalizer = $normalizer;
34 2
        $this->encoder = $encoder;
35 2
    }
36
37 1
    public function serialize($value, string $format, array $context = []): string {
38 1
        return $this->encoder->encode(
39 1
            $this->normalizer->normalize($value, $format, $context),
40 1
            $format,
41 1
            $context
42
        );
43
    }
44
45 1
    public function unserialize(string $data, Type $type, string $format, array $context = []) {
46 1
        return $this->normalizer->denormalize(
47 1
            $this->encoder->decode($data, $format, $context),
48 1
            $type,
49 1
            $format,
50 1
            $context
51
        );
52
    }
53
}
54