Completed
Push — master ( 0c7c34...b7f628 )
by
unknown
02:49
created

TableTrait::createDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
//[PHPCOMPRESSOR(remove,start)]
3
/**
4
 * Created by Vitaly Iegorov <[email protected]>.
5
 * on 22.03.16 at 15:46
6
 */
7
namespace samsoncms\api\generator;
8
9
use samsoncms\api\generator\metadata\GenericMetadata;
10
use samsoncms\api\generator\metadata\VirtualMetadata;
11
use samsonphp\generator\Generator;
12
13
/**
14
 * Table trait class generator. As all entities should
15
 * be able to create Table class instances for their selves
16
 * this class is a trait with methods to receive all available virtual
17
 * entity tables.
18
 *
19
 * @package samsoncms\api\generator
20
 */
21
class TableTrait extends Generic
22
{
23
    /**
24
     * Query constructor.
25
     *
26
     * @param Generator $generator
27
     * @param           $metadata
28
     */
29
    public function __construct(Generator $generator, $metadata)
30
    {
31
        parent::__construct($generator, $metadata);
32
33
        $this->className = 'TableTrait';
34
    }
35
36
    /**
37
     * Class uses generation part.
38
     *
39
     * @param VirtualMetadata $metadata Entity metadata
40
     */
41
    protected function createUses($metadata)
42
    {
43
        $this->generator
44
            ->newLine('use samsonframework\core\ViewInterface;')
45
            ->newLine();
46
    }
47
48
    /**
49
     * Class definition generation part.
50
     *
51
     * @param VirtualMetadata $metadata Entity metadata
52
     */
53
    protected function createDefinition($metadata)
54
    {
55
        $this->generator
56
            ->multiComment(array('"TableTrait database entity class'))
57
            ->defTrait($this->className);
58
    }
59
60
    /**
61
     * Class methods generation part.
62
     *
63
     * @param VirtualMetadata $metadata Entity metadata
64
     */
65
    protected function createMethods($metadata)
66
    {
67
        $methods = [];
68
        /** @var VirtualMetadata $metadataInstance Iterate all metadata entities */
69
        foreach (GenericMetadata::$instances as $metadataInstance) {
70
            if ($metadataInstance instanceof VirtualMetadata && $metadataInstance->type === VirtualMetadata::TYPE_TABLE) {
71
                // Create table virtual entity with correct name ending
72
                $replaced = preg_replace('/Table$/i', '',$metadataInstance->entity);
73
                $tableEntity = $replaced . 'TableCollection';
74
75
                $code = "\n\t" . '/**';
76
                $code .= "\n\t" . ' * Create virtual ' . $metadataInstance->entityRealName . ' table instance.';
77
                $code .= "\n\t" . ' * @param ViewInterface $renderer Renderer instance';
78
                $code .= "\n\t" . ' * @param string $locale Locale';
79
                $code .= "\n\t" . ' *';
80
                $code .= "\n\t" . ' * @return ' . $tableEntity . ' Table instance';
81
                $code .= "\n\t" . ' */';
82
                $code .= "\n\t" . 'public function create' . $tableEntity . '(ViewInterface $renderer, $locale = null)';
83
                $code .= "\n\t" . '{';
84
                $code .= "\n\t\t" . 'return new ' . $tableEntity . '($renderer, $this->id, $this->query, $locale);';
85
                $code .= "\n\t" . '}';
86
87
                $methods[] = $code;
88
89
                // Create table virtual entity with correct name ending
90
                $tableEntity = $replaced . 'TableEntity';
91
92
                $code = "\n\t" . '/**';
93
                $code .= "\n\t" . ' * Create virtual ' . $metadataInstance->entityRealName . ' table row instance.';
94
                $code .= "\n\t" . ' * @param string $locale Locale';
95
                $code .= "\n\t" . ' *';
96
                $code .= "\n\t" . ' * @return ' . $tableEntity . ' Table instance';
97
                $code .= "\n\t" . ' */';
98
                $code .= "\n\t" . 'public function create' . $tableEntity . 'Row($locale = null)';
99
                $code .= "\n\t" . '{';
100
                $code .= "\n\t\t" . '$row = new ' . $tableEntity . '();';
101
                $code .= "\n\t\t" . '$row->parent_id = $this->id;';
102
                $code .= "\n\t\t" . 'return $row;';
103
                $code .= "\n\t" . '}';
104
105
                $methods[] = $code;
106
            }
107
        }
108
109
        // Add method text to generator
110
        $this->generator->text(implode("\n", $methods));
111
    }
112
}
113
//[PHPCOMPRESSOR(remove,end)]