Passed
Pull Request — master (#247)
by Def
27:26 queued 12:25
created

TableName::hasAlias()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
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\Exception\InvalidArgumentException;
8
use Yiisoft\Db\Expression\ExpressionInterface;
9
10
/**
11
 * Holds name of a table including schema and alias.
12
 * Usage is the following:
13
 *
14
 * (new Query)->select('col1')->from(new TableName('table1', 't', 'dbo'))
15
 *
16
 * Note: We use prefix from connection with tables which schema equals default schema or which has no schema.
17
 * Note: With `ExpressionInterface` as tablename we cannot add prefixes and quote table names.
18
 * For example, with Oracle: `(new Query)->select('*')->from(new Expression('dblink1.dbo.table'))` to build `select * from dblink1.dbo.table1`
19
 */
20
final class TableName
21
{
22
    /**
23
     * @var ExpressionInterface|string
24
     */
25
    private $name;
26
27
    /**
28
     * @var string|null
29
     */
30
    private ?string $alias;
31
32
    /**
33
     * @var ExpressionInterface|string|null
34
     */
35
    private $schema;
36
37
    /**
38
     * @param ExpressionInterface|string $name
39
     * @param string|null $alias
40
     * @param ExpressionInterface|string|null $schema
41
     */
42
    public function __construct($name, ?string $alias = null, $schema = null)
43
    {
44
        if (!is_string($name) && !$name instanceof ExpressionInterface) {
0 ignored issues
show
introduced by
$name is always a sub-type of Yiisoft\Db\Expression\ExpressionInterface.
Loading history...
45
            throw new InvalidArgumentException(
46
                'Name of the table should be a string or an instance of ExpressionInterface.'
47
            );
48
        }
49
50
        if ($schema !== null && !is_string($schema) && !($schema instanceof ExpressionInterface)) {
0 ignored issues
show
introduced by
$schema is always a sub-type of Yiisoft\Db\Expression\ExpressionInterface.
Loading history...
51
            throw new InvalidArgumentException(
52
                'Schema should be null, string, or an instance of ExpressionInterface.'
53
            );
54
        }
55
56
        $this->name = $name;
57
        $this->alias = empty($alias) ? null : $alias;
58
        $this->schema = empty($schema) ? null : $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;
85
    }
86
87
    public function hasSchema(): bool
88
    {
89
        return $this->schema !== null;
90
    }
91
}
92