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

drago-ex / generator / 12947105156

24 Jan 2025 09:42AM UTC coverage: 84.018% (-3.1%) from 87.111%
12947105156

push

github

web-flow
Update DataClassGenerator.phpt

184 of 219 relevant lines covered (84.02%)

0.84 hits per line

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

87.5
/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
 * Generates data class based on database schema.
23
 */
24
class DataClassGenerator extends Base implements IGenerator
25
{
26
        /**
27
         * Runs the generation process for a specific table or all tables.
28
         *
29
         * @param string|null $table The table name to generate for, or null for all tables.
30
         * @throws Exception
31
         * @throws Throwable
32
         */
33
        public function runGeneration(?string $table = null): void
1✔
34
        {
35
                if ($table !== null) {
1✔
36
                        $this->createPhpFile($table);
1✔
37
                } else {
38
                        foreach ($this->repository->getTableNames() as $table) {
1✔
39
                                $this->createPhpFile($table);
1✔
40
                        }
41
                }
42
        }
1✔
43

44

45
        /**
46
         * Creates a PHP file for a given table.
47
         *
48
         * @param string $table The table name to generate the class for.
49
         * @throws Exception
50
         * @throws Throwable
51
         */
52
        public function createPhpFile(string $table): void
1✔
53
        {
54
                // Get options for the generator.
55
                $options = $this->options;
1✔
56

57
                // Create the class filename and add suffix.
58
                $name = $this->filename($table, $options->suffixDataClass);
1✔
59

60
                // Generate the class.
61
                $class = new ClassType($name);
1✔
62

63
                // Add final modifier if required.
64
                if ($options->finalDataClass) {
1✔
65
                        $class->setFinal();
×
66
                }
67

68
                // Add extends class if required.
69
                if ($options->extendsOn) {
1✔
70
                        $class->setExtends($options->extendsDataClass);
1✔
71
                }
72

73
                // Get references for the table.
74
                $references = $this->getReferencesTable($table);
1✔
75

76
                // Loop through all columns in the table.
77
                foreach ($this->repository->getColumnNames($table) as $column) {
1✔
78
                        // Convert column name to lowercase if required.
79
                        if ($options->lower) {
1✔
80
                                $column = Strings::lower($column);
×
81
                        }
82

83
                        // Validate column names for parentheses.
84
                        $this->validateColumn($table, $column);
1✔
85

86
                        // Get column attribute information.
87
                        $attr = $this->repository->getColumn($table, $column);
1✔
88

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

95
                                $class->addConstant($constant, $column)
1✔
96
                                        ->setPublic();
1✔
97

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

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

108
                        // Add property for the column.
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 another table if required.
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 the PHP file content.
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
                // Write the generated PHP file to the filesystem.
132
                $path = $options->pathDataClass . '/' . $name . '.php';
1✔
133
                FileSystem::write($path, $file->__toString());
1✔
134
        }
1✔
135
}
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