Repository navigation
Expand file tree
/
Copy pathLike.php
More file actions
90 lines (74 loc) · 2.32 KB
/
Copy pathLike.php
File metadata and controls
90 lines (74 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
declare(strict_types=1);
namespace PhpDb\Sql\Predicate;
use Override;
use PhpDb\Sql\AbstractExpression;
use PhpDb\Sql\Argument\Identifier;
use PhpDb\Sql\Argument\Value;
use PhpDb\Sql\ArgumentInterface;
use PhpDb\Sql\Exception\InvalidArgumentException;
class Like extends AbstractExpression implements PredicateInterface
{
protected string $operator = 'LIKE';
protected ?ArgumentInterface $identifier = null;
protected ?ArgumentInterface $like = null;
/**
* Constructor
*/
public function __construct(
null|string|ArgumentInterface $identifier = null,
null|bool|float|int|string|ArgumentInterface $like = null
) {
if ($identifier !== null) {
$this->setIdentifier($identifier);
}
if ($like !== null) {
$this->setLike($like);
}
}
/**
* Set identifier for comparison
*/
public function setIdentifier(string|ArgumentInterface $identifier): static
{
$this->identifier = $identifier instanceof ArgumentInterface
? $identifier
: new Identifier($identifier);
return $this;
}
public function getIdentifier(): ?ArgumentInterface
{
return $this->identifier;
}
/**
* Set like pattern for comparison
*/
public function setLike(bool|float|int|null|string|ArgumentInterface $like): static
{
$this->like = $like instanceof ArgumentInterface
? $like
: new Value($like);
return $this;
}
public function getLike(): ?ArgumentInterface
{
return $this->like;
}
/** @inheritDoc */
#[Override]
public function getExpressionData(): array
{
if (! $this->identifier instanceof ArgumentInterface) {
throw new InvalidArgumentException('Identifier must be specified');
}
if (! $this->like instanceof ArgumentInterface) {
throw new InvalidArgumentException('Like expression must be specified');
}
$identifierSpec = $this->identifier->getSpecification();
$likeSpec = $this->like->getSpecification();
return [
'spec' => $this->specification ?? "{$identifierSpec} {$this->operator} {$likeSpec}",
'values' => [$this->identifier, $this->like],
];
}
}