Passed
Pull Request — master (#67)
by Aleksei
27:25 queued 12:25
created

SchemaToPHP::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 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