Passed
Pull Request — master (#247)
by Def
16:07
created

TableName::hasSchema()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Schema;
6
7
use Yiisoft\Db\Expression\ExpressionInterface;
8
9
/**
10
 * @example
11
 * (new Query)->select('col1')->from(TableName::create('table1', 't', 'dbo'))
12
 *
13
 * Note: We must use prefix (from connection) with tables with schema equal defaultSchema or without schema and don't use with other schemas
14
 * Note: With ExpressionInterface as tablename - we cannot add prefixes and quoting of table names.
15
 * For example with Oracle: (new Query)->select('*')->from(new Expression('dblink1.dbo.table')) for build `select * from dblink1.dbo.table1`
16
 */
17
final class TableName
18
{
19
    /**
20
     * @var ExpressionInterface|string
21
     */
22
    private $name;
23
24
    /**
25
     * @var string|null
26
     */
27
    private ?string $alias;
28
29
    /**
30
     * @var ExpressionInterface|string|null
31
     */
32
    private $schema;
33
34
    /**
35
     * @param ExpressionInterface|string $name
36
     * @param string|null $alias
37
     * @param ExpressionInterface|string|null $schema
38
     */
39
    private function __construct($name, ?string $alias, $schema = null)
40
    {
41
        $this->name = $name;
42
        $this->alias = $alias;
43
        $this->schema = $schema;
44
    }
45
46
    /**
47
     * @param ExpressionInterface|string $name
48
     * @param string|null $alias
49
     * @param ExpressionInterface|string|null $schema
50
     *
51
     * @return $this
52
     */
53
    public static function create($name, ?string $alias = null, $schema = null): self
54
    {
55
        assert(is_string($name) || $name instanceof ExpressionInterface);
56
        assert(is_string($schema) || $schema instanceof ExpressionInterface || $schema === null);
57
58
        return new self($name, $alias, $schema);
59
    }
60
61
    /**
62
     * @return ExpressionInterface|string
63
     */
64
    public function getName()
65
    {
66
        return $this->name;
67
    }
68
69
    public function getAlias(): ?string
70
    {
71
        return $this->alias;
72
    }
73
74
    /**
75
     * @return ExpressionInterface|string|null
76
     */
77
    public function getSchema()
78
    {
79
        return $this->schema;
80
    }
81
82
    public function hasAlias(): bool
83
    {
84
        return !($this->alias === null || $this->alias === '');
85
    }
86
87
    public function hasSchema(): bool
88
    {
89
        return !($this->schema === null || $this->schema === '');
90
    }
91
}
92