RealQuery::createMethods()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 17
nc 2
nop 1
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
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 samsonframework\orm\generator;
8
9
use samsonframework\orm\generator\metadata\RealMetadata;
10
use samsonphp\generator\Generator;
11
12
/**
13
 * Real entity query class generator.
14
 *
15
 * @package samsonframework\orm\generator
16
 */
17
class RealQuery extends Generic
18
{
19
    /** @var string Query returned entity class name */
20
    protected $entityClass;
21
22
    /**
23
     * Query constructor.
24
     *
25
     * @param Generator $generator
26
     * @param           $metadata
27
     */
28
    public function __construct(Generator $generator, $metadata)
29
    {
30
        parent::__construct($generator, $metadata);
31
32
        $this->className = $metadata->entity . 'Query';
33
        $this->parentClass = '\\' . \samsonframework\orm\query\Record::class;
34
        $this->entityClass = '\samsonframework\orm\generated\\' . $metadata->entity;
35
    }
36
37
    /**
38
     * Class uses generation part.
39
     *
40
     * @param RealMetadata $metadata Entity metadata
41
     */
42
    protected function createUses($metadata)
43
    {
44
        $this->generator
45
            ->newLine('use samsonframework\orm\ArgumentInterface;')
46
            ->newLine();
47
    }
48
49
    /**
50
     * Class definition generation part.
51
     *
52
     * @param RealMetadata $metadata Entity metadata
53
     */
54
    protected function createDefinition($metadata)
55
    {
56
        $this->generator
57
            ->multiComment(array(
58
                'Class for querying and fetching "' . $metadata->entity . '" instances from database',
59
                '@method ' . $this->entityClass . ' first();',
60
                '@method ' . $this->entityClass . '[] find();',
61
            ))
62
            ->defClass($this->className, $this->parentClass);
63
    }
64
65
    /**
66
     * Class static fields generation part.
67
     *
68
     * @param RealMetadata $metadata Entity metadata
69
     */
70
    protected function createStaticFields($metadata)
71
    {
72
        $this->generator
73
            ->commentVar('string', 'Entity table primary field name')
74
            ->defClassVar('$primaryFieldName', 'public static', $metadata->primaryField)
75
            ->commentVar('string', 'Entity full class name')
76
            ->defClassVar('$identifier', 'public static', $this->entityClass)
77
            ->commentVar('string', 'Entity table name')
78
            ->defClassVar('$tableName', 'public static', $metadata->entity)
79
            ->commentVar('array', 'Collection of entity field types')
80
            ->defClassVar('$fieldTypes', 'public static', $metadata->types)
81
            ->commentVar('array', 'Collection of entity field names to field aliases')
82
            ->defClassVar('$fieldIDs', 'public static', $metadata->fields)
83
            ->commentVar('array', 'Collection of entity field database types')
84
            ->defClassVar('$fieldDataTypes', 'public static', $metadata->internalTypes)
85
            ->commentVar('array', 'Collection of entity field database default values')
86
            ->defClassVar('$fieldDefaults', 'public static', $metadata->defaults)
87
            ->commentVar('array', 'Collection of entity field database is nullable values')
88
            ->defClassVar('$fieldNullable', 'public static', $metadata->nullable)
89
            ->commentVar('array', 'Collection of entity field aliases to field names')
90
            ->defClassVar('$fieldNames', 'public static', $metadata->fieldNames);
91
    }
92
93
    /**
94
     * Class methods generation part.
95
     *
96
     * @param RealMetadata $metadata Entity metadata
97
     */
98
    protected function createMethods($metadata)
99
    {
100
        $methods = [];
101
        // TODO: Add different method generation depending on their field type
102
        // Generate Query::where() analog for specific field.
103
        foreach ($metadata->fields as $fieldID => $fieldName) {
104
            $code = "\n\t" . '/**';
105
            $code .= "\n\t" . ' * Add ' . $fieldName . '(#' . $fieldID . ') field query condition.';
106
            $code .= "\n\t" . ' * @see Generic::where()';
107
            $code .= "\n\t" . ' * @param ' . $metadata->types[$fieldID] . ' $value Field value';
108
            $code .= "\n\t" . ' * @param string $relation Field to value condition relation';
109
            $code .= "\n\t" . ' *';
110
            $code .= "\n\t" . ' * @return $this Chaining';
111
            $code .= "\n\t" . ' */';
112
            $code .= "\n\t" . 'public function ' . $fieldName . '($value, $relation = ArgumentInterface::EQUAL)';
113
            $code .= "\n\t" . '{';
114
            $code .= "\n\t\t" . 'return $this->where(' . $this->entityClass . '::F_' . strtoupper($fieldName) . ', $value, $relation);';
115
116
            $code .= "\n\t" . '}';
117
118
            $methods[] = $code;
119
        }
120
121
        // Add method text to generator
122
        $this->generator->text(implode("\n", $methods));
123
    }
124
}
125
//[PHPCOMPRESSOR(remove,end)]
126