Completed
Push — master ( 2a9c6f...3cba1a )
by arto
01:54
created

DTOGeneratorExample   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 8
dl 0
loc 84
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B demonstrate() 0 78 3
1
#!/bin/php
2
<?php
3
/**
4
 * @author stev leibelt <[email protected]>
5
 * @since 2014-05-14 
6
 */
7
8
require_once 'AbstractExample.php';
9
require_once __DIR__ . '/../vendor/autoload.php';
10
11
/**
12
 * Class DTOGeneratorExample
13
 * @package Net\Bazzline\Component\CodeGenerator\Example
14
 */
15
class DTOGeneratorExample extends AbstractExample
16
{
17
    /**
18
     * @return string
19
     */
20
    function demonstrate()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
21
    {
22
        //---- begin of factories
23
        $classFactory = $this->getClassGeneratorFactory();
24
        $documentationFactory = $this->getDocumentationGeneratorFactory();
25
        $methodFactory = $this->getMethodGeneratorFactory();
26
        $propertyFactory = $this->getPropertyGeneratorFactory();
27
        //---- end of factories
28
29
        $class = $classFactory->create();
30
        $class->setDocumentation($documentationFactory->create());
31
        $class->setName('ExampleDTO');
32
33
        $properties = [
34
            [
35
                'name'      => 'foo',
36
                'typeHint'  => null,
37
                'value'     => 42
38
            ],
39
            [
40
                'name'      => 'foobar',
41
                'typeHint'  => 'array',
42
                'value'     => null
43
            ],
44
            [
45
                'name'      => 'bar',
46
                'typeHint'  => 'Bar',
47
                'value'     => null
48
            ]
49
        ];
50
51
        foreach ($properties as $value) {
52
            //---- begin of properties
53
            $property = $propertyFactory->create();
54
            $property->setDocumentation($documentationFactory->create());
55
            $property->setName($value['name']);
56
            if (!is_null($value['value'])) {
57
                $property->setValue('value');
58
            }
59
            $property->markAsPrivate();
60
            $property->setDocumentation($documentationFactory->create());
61
            //---- end of properties
62
63
            //---- begin of getter method
64
            $getterMethod = $methodFactory->create();
65
            $getterMethod->setDocumentation($documentationFactory->create());
66
            $getterMethod->setName('get' . ucfirst($value['name']));
67
            $getterMethod->setBody(
68
                [
69
                    '$this->' . $value['name'] . ' = $' . $value['name'] . ';'
70
                ], 
71
                [
72
                    $value['typeHint']
73
                ]
74
            );
75
            //---- end of getter method
76
77
            //---- begin of setter method
78
            $setterMethod = $methodFactory->create();
79
            $setterMethod->setDocumentation($documentationFactory->create());
80
            $setterMethod->addParameter($value['name'], null, $value['typeHint']);
81
            $setterMethod->setName('set' . ucfirst($value['name']));
82
            $setterMethod->setBody(
83
                [
84
                    'return $this->' . $value['name'] . ';'
85
                ]
86
            );
87
            //---- end of setter method
88
89
            $class->addProperty($property);
90
            $class->addMethod($getterMethod);
91
            $class->addMethod($setterMethod);
92
        }
93
94
        echo 'generated content' . PHP_EOL;
95
        echo '----' . PHP_EOL;
96
        echo $class->generate() . PHP_EOL;
97
    }
98
}
99
100
$example = new DTOGeneratorExample();
101
$example->demonstrate();
102