GeneratorSerialiser::addCallSerialise()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 23
c 2
b 0
f 0
nc 5
nop 1
dl 0
loc 34
rs 9.2408
1
<?php
2
/**
3
 * This file is part of graze/unicontroller-client.
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/unicontroller-client/blob/master/LICENSE.md
11
 * @link https://github.com/graze/unicontroller-client
12
 */
13
namespace Graze\UnicontrollerClient\ClassGenerator\Generator;
14
15
use Graze\UnicontrollerClient\ClassGenerator\Generator\AbstractGenerator;
16
use Graze\UnicontrollerClient\ClassGenerator\Generator\GeneratorInterface;
17
use Graze\UnicontrollerClient\ClassGenerator\Definition\DefinitionProperty;
18
19
class GeneratorSerialiser extends AbstractGenerator implements GeneratorInterface
20
{
21
    const OUTPUT_PATH = 'src/Serialiser/Serialiser/Serialiser%s.php';
22
23
    /**
24
     * @var array
25
     */
26
    private $callsSerialise = [];
27
28
    /**
29
     * @param string $name
30
     * @return string
31
     */
32
    public function generateClass($name)
33
    {
34
        return sprintf(
35
            $this->getTemplate('Serialiser/SerialiserClass'),
36
            $name,
37
            implode("\n", $this->callsSerialise)
38
        );
39
    }
40
41
    /**
42
     * @param DefinitionProperty $property
43
     */
44
    public function addCallSerialise(DefinitionProperty $property)
45
    {
46
        switch ($property->getType()) {
47
            case DefinitionProperty::PROPERTY_TYPE_ARRAY:
48
                $call = sprintf(
49
                    $this->getTemplate('Serialiser/SerialiserCallSerialiseArray'),
50
                    $property->getName(),
51
                    $property->getArrayElementName()
52
                );
53
                break;
54
55
            case DefinitionProperty::PROPERTY_TYPE_STRING:
56
                $call = sprintf(
57
                    $this->getTemplate('Serialiser/SerialiserCallSerialiseString'),
58
                    $property->getName()
59
                );
60
                break;
61
62
            case DefinitionProperty::PROPERTY_TYPE_INT:
63
                $call = sprintf(
64
                    $this->getTemplate('Serialiser/SerialiserCallSerialiseInt'),
65
                    $property->getName()
66
                );
67
                break;
68
69
            case DefinitionProperty::PROPERTY_TYPE_BINARY_DATA:
70
                $call = sprintf(
71
                    $this->getTemplate('Serialiser/SerialiserCallSerialiseBinaryData'),
72
                    $property->getName()
73
                );
74
                break;
75
        }
76
77
        $this->callsSerialise[] = $call;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $call does not seem to be defined for all execution paths leading up to this point.
Loading history...
78
    }
79
80
    /**
81
     * @param string $name
82
     *
83
     * @return [type]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
84
     */
85
    public function getOutputPath($name)
86
    {
87
        return sprintf(self::OUTPUT_PATH, $name);
88
    }
89
}
90