Passed
Pull Request — master (#97)
by Aleksei
02:34
created

SchemaToPHP::convert()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
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\SchemaInterface;
9
use Cycle\Schema\Relation\RelationSchema;
10
use Yiisoft\Yii\Cycle\Schema\Converter\SchemaToPHP\SchemaRenderer;
11
12
final class SchemaToPHP
13
{
14
    private SchemaInterface $schema;
15
    private const USE_LIST = [
16
        SchemaInterface::class . ' as Schema',
17
        Relation::class,
18
        RelationSchema::class,
19
    ];
20
21 3
    public function __construct(SchemaInterface $schema)
22
    {
23 3
        $this->schema = $schema;
24 3
    }
25
26
    public function __toString(): string
27
    {
28
        return $this->convert();
29
    }
30
31 3
    public function convert(): string
32
    {
33 3
        $result = "<?php\n\ndeclare(strict_types=1);\n\n";
34
        // the use block
35 3
        foreach (self::USE_LIST as $use) {
36 3
            $result .= "use {$use};\n";
37
        }
38 3
        $renderedArray = (new SchemaRenderer($this->schema))->render();
39 3
        $result .= "\nreturn {$renderedArray};\n";
40 3
        return $result;
41
    }
42
}
43