• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

drago-ex / generator / 10071390267

24 Jul 2024 06:06AM UTC coverage: 87.111%. Remained the same
10071390267

push

github

web-flow
Delete tmp/.gitignore

196 of 225 relevant lines covered (87.11%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.0
/src/Drago/Generator/Generator/DataClassGenerator.php
1
<?php
2

3
/**
4
 * Drago Extension
5
 * Package built on Nette Framework
6
 */
7

8
declare(strict_types=1);
9

10
namespace Drago\Generator\Generator;
11

12
use Dibi\Exception;
13
use Drago\Generator\Base;
14
use Nette\PhpGenerator\ClassType;
15
use Nette\PhpGenerator\PhpFile;
16
use Nette\Utils\FileSystem;
17
use Nette\Utils\Strings;
18
use Throwable;
19

20

21
/**
22
 * Generating data class.
23
 */
24
class DataClassGenerator extends Base implements IGenerator
25
{
26
        /**
27
         * Run generate.
28
         * @throws Exception
29
         * @throws Throwable
30
         */
31
        public function runGeneration(null|string $table = null): void
1✔
32
        {
33
                if ($table !== null) {
1✔
34
                        $this->createPhpFile($table);
1✔
35
                } else {
36
                        foreach ($this->repository->getTableNames() as $table) {
1✔
37
                                $this->createPhpFile($table);
1✔
38
                        }
39
                }
40
        }
1✔
41

42

43
        /**
44
         * Creating php file.
45
         * @throws Exception
46
         * @throws Throwable
47
         */
48
        public function createPhpFile(string $table): void
1✔
49
        {
50
                // Options for generator.
51
                $options = $this->options;
1✔
52

53
                // Create filename and add suffix.
54
                $name = $this->filename($table, $options->suffixDataClass);
1✔
55

56
                // Generating a class.
57
                $class = new ClassType($name);
1✔
58

59
                // Add final keyword
60
                if ($options->finalDataClass) {
1✔
61
                        $class->setFinal();
×
62
                }
63

64
                // Add extends class.
65
                if ($options->extendsOn) {
1✔
66
                        $class->setExtends($options->extendsDataClass);
1✔
67
                }
68

69
                // Get references.
70
                $references = $this->getReferencesTable($table);
1✔
71

72
                // Get all columns names from table.
73
                foreach ($this->repository->getColumnNames($table) as $column) {
1✔
74

75
                        // Convert large characters to lowercase.
76
                        if ($options->lower) {
1✔
77
                                $column = Strings::lower($column);
×
78
                        }
79

80
                        // Check column names for parentheses.
81
                        $this->validateColumn($table, $column);
1✔
82

83
                        // Get column attribute information.
84
                        $attr = $this->repository->getColumn($table, $column);
1✔
85

86
                        // Add constants.
87
                        if ($options->constantDataClass) {
1✔
88
                                $cp = $this->options->constantDataPrefix;
1✔
89
                                $constant = $cp
1✔
90
                                        ? $cp . $this->inflector->classify($column)
×
91
                                        : $this->inflector->classify($column);
1✔
92

93
                                $class->addConstant($constant, $column)
1✔
94
                                        ->setPublic();
1✔
95

96
                                // Add to constant column length information.
97
                                if ($options->constantSizeDataClass) {
1✔
98
                                        if (!$attr->isAutoIncrement() && $attr->getSize() > 0) {
1✔
99
                                                $class->addConstant($constant . 'Size', $attr->getSize())
1✔
100
                                                        ->setPublic();
1✔
101
                                        }
102
                                }
103
                        }
104

105
                        // Detect native type.
106
                        $detectType = $this->detectType($attr->getNativeType());
1✔
107

108
                        // Add attributes to the entity.
109
                        $class->addProperty($column)
1✔
110
                                ->setType($detectType)
1✔
111
                                ->setNullable($attr->isNullable())
1✔
112
                                ->setInitialized($attr->isNullable())
1✔
113
                                ->setPublic();
1✔
114

115
                        // Add reference to table.
116
                        if ($options->referencesDataClass && isset($references[$column])) {
1✔
117
                                $filename = $this->filename($references[$column], $options->suffixDataClass);
×
118
                                $class->addProperty($references[$column])
×
119
                                        ->setType($options->namespaceDataClass . '\\' . $filename);
×
120
                        }
121
                }
122

123
                // Generate PHP file.
124
                $file = new PhpFile;
1✔
125
                $file->addComment('This file was generated by Drago Generator.')
1✔
126
                        ->setStrictTypes()
1✔
127
                        ->addNamespace($options->namespace)
1✔
128
                        ->addUse('Drago')
1✔
129
                        ->add($class);
1✔
130

131
                $path = $options->pathDataClass . '/' . $name . '.php';
1✔
132
                FileSystem::write($path, $file->__toString());
1✔
133
        }
1✔
134
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc