Passed
Pull Request — master (#67)
by Aleksei
13:05
created

SchemaToPHP   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 14
c 1
b 0
f 0
dl 0
loc 27
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A convert() 0 10 2
A __toString() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Cycle\Schema\Converter;
6
7
use Cycle\ORM\Relation;
8
use Cycle\ORM\Schema;
9
use Cycle\ORM\SchemaInterface;
10
use Cycle\Schema\Relation\RelationSchema;
11
use Yiisoft\Yii\Cycle\Schema\Converter\SchemaToPHP\SchemaRenderer;
12
13
final class SchemaToPHP
14
{
15
    private SchemaInterface $schema;
16
    private const USE_LIST = [
17
        Schema::class,
18
        Relation::class,
19
        RelationSchema::class,
20
    ];
21
22
    public function __construct(SchemaInterface $schema)
23
    {
24
        $this->schema = $schema;
25
    }
26
    public function __toString(): string
27
    {
28
        return $this->convert();
29
    }
30
    public function convert(): string
31
    {
32
        $result = "<?php\n\ndeclare(strict_types=1);\n\n";
33
        // the use block
34
        foreach (self::USE_LIST as $use) {
35
            $result .= "use {$use};\n";
36
        }
37
        $renderedArray = (new SchemaRenderer($this->schema))->render();
38
        $result .= "\nreturn {$renderedArray};\n";
39
        return $result;
40
    }
41
}
42