CodeGenerator::generate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace gossi\codegen\generator;
5
6
use gossi\codegen\config\CodeGeneratorConfig;
7
use gossi\codegen\model\GenerateableInterface;
8
9
/**
10
 * Code generator
11
 *
12
 * Generates code for any generateable model
13
 *
14
 * @author Thomas Gossmann
15
 */
16
class CodeGenerator {
17
18
	const SORT_USESTATEMENTS_DEFAULT = 'default';
19
20
	const SORT_CONSTANTS_DEFAULT = 'default';
21
22
	const SORT_PROPERTIES_DEFAULT = 'default';
23
24
	const SORT_METHODS_DEFAULT = 'default';
25
26
	/** @var CodeGeneratorConfig */
27
	protected $config;
28
29
	/** @var ModelGenerator */
30
	protected $generator;
31
32
	/**
33
	 *
34
	 * @param CodeGeneratorConfig|array $config
35
	 */
36 10
	public function __construct($config = null) {
37 10
		$this->configure($config);
38 10
		$this->generator = new ModelGenerator($this->config);
39 10
	}
40
41 2
	protected function configure($config = null) {
42 2
		if (is_array($config)) {
43 1
			$this->config = new CodeGeneratorConfig($config);
44 1
		} else if ($config instanceof CodeGeneratorConfig) {
45 1
			$this->config = $config;
46
		} else {
47 1
			$this->config = new CodeGeneratorConfig();
48
		}
49 2
	}
50
51
	/**
52
	 * Returns the used configuration
53
	 *
54
	 * @return CodeGeneratorConfig
55
	 */
56 1
	public function getConfig() {
57 1
		return $this->config;
58
	}
59
60
	/**
61
	 * Generates code from a given model
62
	 *
63
	 * @param GenerateableInterface $model
64
	 * @return string the generated code
65
	 */
66 8
	public function generate(GenerateableInterface $model): string {
67 8
		return $this->generator->generate($model);
68
	}
69
70
}
71