Completed
Push — master ( da6ff3...4f4aae )
by Vitaly
02:49
created

Query::createDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 10
rs 9.4285
cc 1
eloc 7
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
/**
10
 * Entity Query class generator.
11
 *
12
 * @package samsoncms\api\generator
13
 */
14
class Query extends OOP
15
{
16
    /**
17
     * Class uses generation part.
18
     *
19
     * @param Metadata $metadata Entity metadata
20
     */
21
    protected function createUses(Metadata $metadata)
22
    {
23
        $this->generator
24
            ->newLine('use samsonframework\orm\ArgumentInterface;')
25
            ->newLine();
26
    }
27
28
    /**
29
     * Class definition generation part.
30
     *
31
     * @param Metadata $metadata Entity metadata
32
     */
33
    protected function createDefinition(Metadata $metadata)
34
    {
35
        $this->generator
36
            ->multiComment(array(
37
                'Class for querying and fetching "' . $metadata->entityRealName . '" instances from database',
38
                '@method ' . $metadata->entity . ' first();',
39
                '@method ' . $metadata->entity . '[] find();',
40
            ))
41
            ->defClass($metadata->entity . 'Query', '\\'. \samsoncms\api\query\Entity::class);
42
    }
43
44
    /**
45
     * Class static fields generation part.
46
     *
47
     * @param Metadata $metadata Entity metadata
48
     */
49
    protected function createStaticFields(Metadata $metadata)
50
    {
51
        $this->generator
52
            ->commentVar('array', 'Collection of real additional field names')
53
            ->defClassVar('$fieldRealNames', 'public static', $metadata->realNames)
54
            ->commentVar('array', 'Collection of additional field names')
55
            ->defClassVar('$fieldNames', 'public static', $metadata->allFieldNames)
56
            // TODO: two above fields should be protected
57
            ->commentVar('array', 'Collection of navigation identifiers')
58
            ->defClassVar('$navigationIDs', 'protected static', array($metadata->entityID))
59
            ->commentVar('string', 'Entity full class name')
60
            ->defClassVar('$identifier', 'protected static', $metadata->entityClassName)
61
            ->commentVar('array', 'Collection of localized additional fields identifiers')
62
            ->defClassVar('$localizedFieldIDs', 'protected static', $metadata->localizedFieldIDs)
63
            ->commentVar('array', 'Collection of NOT localized additional fields identifiers')
64
            ->defClassVar('$notLocalizedFieldIDs', 'protected static', $metadata->notLocalizedFieldIDs)
65
            ->commentVar('array', 'Collection of localized additional fields identifiers')
66
            ->defClassVar('$fieldIDs', 'protected static', $metadata->allFieldIDs)
67
            ->commentVar('array', 'Collection of additional fields value column names')
68
            ->defClassVar('$fieldValueColumns', 'protected static', $metadata->allFieldValueColumns);
69
    }
70
71
    /**
72
     * Class methods generation part.
73
     *
74
     * @param Metadata $metadata Entity metadata
75
     */
76
    protected function createMethods(Metadata $metadata)
77
    {
78
        $methods = [];
79
        // TODO: Add different method generation depending on their field type
80
        // Generate Query::where() analog for specific field.
81
        foreach ($metadata->allFieldIDs as $fieldID => $fieldName) {
82
            $code = "\n\t" . '/**';
83
            $code .= "\n\t" . ' * Add '.$fieldName.'(#' . $fieldID . ') field query condition.';
84
            $code .= "\n\t" . ' * @see Generic::where()';
85
            $code .= "\n\t" . ' * @param ' . $metadata->allFieldTypes[$fieldID] . ' $value Field value';
86
            $code .= "\n\t" . ' * @param string $relation Field to value condition relation';
87
            $code .= "\n\t" . ' *';
88
            $code .= "\n\t" . ' * @return $this Chaining';
89
            $code .= "\n\t" . ' */';
90
            $code .= "\n\t" . 'public function ' . $fieldName . '($value, $relation = ArgumentInterface::EQUAL)';
91
            $code .= "\n\t" . '{';
92
            $code .= "\n\t\t" . 'return $this->where(\'' . $fieldName . '\', $value, $relation);';
93
            $code .= "\n\t" . '}';
94
95
            $methods[] = $code;
96
        }
97
98
        // Add method text to generator
99
        $this->generator->text(implode("\n", $methods));
100
    }
101
}
102
//[PHPCOMPRESSOR(remove,end)]
103