Passed
Push — master ( b62344...459625 )
by Alexander
01:46
created

SchemaToPHP::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
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\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 2
    public function __construct(SchemaInterface $schema)
23
    {
24 2
        $this->schema = $schema;
25 2
    }
26
    public function __toString(): string
27
    {
28
        return $this->convert();
29
    }
30 2
    public function convert(): string
31
    {
32 2
        $result = "<?php\n\ndeclare(strict_types=1);\n\n";
33
        // the use block
34 2
        foreach (self::USE_LIST as $use) {
35 2
            $result .= "use {$use};\n";
36
        }
37 2
        $renderedArray = (new SchemaRenderer($this->schema))->render();
38 2
        $result .= "\nreturn {$renderedArray};\n";
39 2
        return $result;
40
    }
41
}
42