1
|
|
|
<?php |
2
|
|
|
/****************************************************************************** |
3
|
|
|
* An implementation of dicto (scg.unibe.ch/dicto) in and for PHP. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016, 2015 Richard Klees <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This software is licensed under The MIT License. You should have received |
8
|
|
|
* a copy of the license along with the code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Lechimp\Dicto\Variables; |
12
|
|
|
|
13
|
|
|
use Lechimp\Dicto\Definition\ArgumentParser; |
14
|
|
|
use Doctrine\DBAL\Query\Expression\ExpressionBuilder; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Name is a property, right? |
18
|
|
|
*/ |
19
|
|
|
class Name extends Property { |
20
|
|
|
/** |
21
|
|
|
* Name of the property. |
22
|
|
|
* |
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
34 |
|
public function name() { |
|
|
|
|
26
|
34 |
|
return "name"; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Fetch arguments for the Property from a stream of tokens during parsing. |
31
|
|
|
* |
32
|
|
|
* @param ArgumentParser $parser |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
7 |
|
public function fetch_arguments(ArgumentParser $parser) { |
36
|
7 |
|
$regexp = $parser->fetch_string(); |
37
|
7 |
|
return array($regexp); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Check if the given arguments are valid for the property. |
42
|
|
|
* |
43
|
|
|
* @param array $arguments |
44
|
|
|
* @return bool |
45
|
|
|
*/ |
46
|
36 |
View Code Duplication |
public function arguments_are_valid(array &$arguments) { |
|
|
|
|
47
|
36 |
|
if (count($arguments) != 1) { |
48
|
|
|
return false; |
49
|
|
|
} |
50
|
36 |
|
$regexp = $arguments[0]; |
51
|
36 |
|
if (!is_string($regexp) || @preg_match("%$regexp%", "") === false) { |
52
|
|
|
return false; |
53
|
|
|
} |
54
|
36 |
|
return true; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Compile the property to an SQL expression. |
59
|
|
|
* |
60
|
|
|
* @param array $argument |
|
|
|
|
61
|
|
|
* @param ExpressionBuilder $builder |
62
|
|
|
* @param string $table_name |
63
|
|
|
* @param bool $negate |
64
|
|
|
* @return string|CompositeExpression |
65
|
|
|
*/ |
66
|
29 |
|
public function compile(array &$arguments, ExpressionBuilder $builder, $table_name, $negate = false) { |
67
|
29 |
|
assert('$this->arguments_are_valid($arguments)'); |
68
|
29 |
|
if (!$negate) { |
69
|
11 |
|
return "$table_name.name REGEXP ".$builder->literal('^'.$arguments[0].'$'); |
70
|
|
|
} |
71
|
|
|
else { |
72
|
18 |
|
return "$table_name.name NOT REGEXP ".$builder->literal('^'.$arguments[0].'$'); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|