Skip to content

Commit 30fdd07

Browse files
committed
Added remove() method for OptionsArray.
Fixed undefined variables introduced by previous renaming. Some refactoring.
1 parent 80cc0cd commit 30fdd07

7 files changed

Lines changed: 85 additions & 31 deletions

File tree

src/Components/AlterOperation.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class AlterOperation extends Component
9191
/**
9292
* Unparsed tokens.
9393
*
94-
* @var Token[]
94+
* @var Token[]|string
9595
*/
9696
public $unknown = array();
9797

@@ -194,9 +194,7 @@ public static function build($component)
194194
if (!empty($component->field)) {
195195
$ret .= Expression::build($component->field) . ' ';
196196
}
197-
foreach ($component->unknown as $token) {
198-
$ret .= $token->token;
199-
}
197+
$ret .= TokensList::build($component->unknown);
200198
return $ret;
201199
}
202200
}

src/Components/Condition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public static function build($component)
201201
{
202202
$ret = array();
203203
foreach ($component as $c) {
204-
$ret[] = $f->expr;
204+
$ret[] = $c->expr;
205205
}
206206
return implode(' ', $ret);
207207
}

src/Components/FieldDefinition.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -230,36 +230,38 @@ public static function parse(Parser $parser, TokensList $list, array $options =
230230
*/
231231
public static function build($component)
232232
{
233-
$ret = array();
234-
235-
foreach ($component as $c) {
233+
if (is_array($component)) {
234+
$ret = array();
235+
foreach ($component as $c) {
236+
$ret[] = static::build($c);
237+
}
238+
return "(\n" . implode(",\n", $ret) . "\n)";
239+
} else {
236240
$tmp = '';
237241

238-
if ($f->isConstraint) {
242+
if ($component->isConstraint) {
239243
$tmp .= 'CONSTRAINT ';
240244
}
241245

242-
if (!empty($f->name)) {
243-
$tmp .= Context::escape($f->name) . ' ';
246+
if (!empty($component->name)) {
247+
$tmp .= Context::escape($component->name) . ' ';
244248
}
245249

246-
if (!empty($f->type)) {
247-
$tmp .= DataType::build($f->type) . ' ';
250+
if (!empty($component->type)) {
251+
$tmp .= DataType::build($component->type) . ' ';
248252
}
249253

250-
if (!empty($f->key)) {
251-
$tmp .= Key::build($f->key) . ' ';
254+
if (!empty($component->key)) {
255+
$tmp .= Key::build($component->key) . ' ';
252256
}
253257

254-
if (!empty($f->references)) {
255-
$tmp .= 'REFERENCES ' . Reference::build($f->references) . ' ';
258+
if (!empty($component->references)) {
259+
$tmp .= 'REFERENCES ' . Reference::build($component->references) . ' ';
256260
}
257261

258-
$tmp .= OptionsArray::build($f->options);
262+
$tmp .= OptionsArray::build($component->options);
259263

260-
$ret[] = trim($tmp);
264+
return trim($tmp);
261265
}
262-
263-
return "(\n" . implode(",\n", $ret) . "\n)";
264266
}
265267
}

src/Components/OptionsArray.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,29 @@ public static function build($component)
182182
public function has($key)
183183
{
184184
foreach ($this->options as $option) {
185-
if ((is_array($option)) && ($key === $option['name'])) {
185+
if ($key === $option) {
186+
return true;
187+
} elseif ((is_array($option)) && ($key === $option['name'])) {
186188
return $option['value'];
187-
} elseif ($key === $option) {
189+
}
190+
}
191+
return false;
192+
}
193+
194+
/**
195+
* Removes the option from the array.
196+
*
197+
* @param string $key The key to be removed.
198+
*
199+
* @return bool Whether the key was found and deleted or not.
200+
*/
201+
public function remove($key)
202+
{
203+
foreach ($this->options as $idx => $option) {
204+
if (($key === $option)
205+
|| ((is_array($option)) && ($key === $option['name']))
206+
) {
207+
unset($this->options[$idx]);
188208
return true;
189209
}
190210
}

src/Components/ParameterDefinition.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,13 @@ public static function build($component)
147147
$ret = array();
148148
foreach ($component as $c) {
149149
$tmp = '';
150-
if (!empty($f->inOut)) {
151-
$tmp .= $f->inOut . ' ';
150+
if (!empty($c->inOut)) {
151+
$tmp .= $c->inOut . ' ';
152152
}
153153

154154
$ret[] = trim(
155-
$tmp . Context::escape($f->name) . ' ' .
156-
DataType::build($f->type)
155+
$tmp . Context::escape($c->name) . ' ' .
156+
DataType::build($c->type)
157157
);
158158
}
159159
return '(' . implode(', ', $ret) . ')';

src/Context.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ abstract class Context
123123
* https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html
124124
*/
125125

126+
// Compatibility mode for Microsoft's SQL server.
127+
// This is the equivalent of ANSI_QUOTES.
128+
const COMPAT_MYSQL = 2;
129+
126130
// https://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_allow_invalid_dates
127131
const ALLOW_INVALID_DATES = 1;
128132

@@ -446,22 +450,24 @@ public static function setMode($mode = '')
446450
/**
447451
* Escapes the symbol by adding surrounding backticks.
448452
*
449-
* @param array|string $str The string to be escaped.
453+
* @param array|string $str The string to be escaped.
454+
* @param string $quote Quote to be used when escaping.
450455
*
451456
* @return string
452457
*/
453-
public static function escape($str)
458+
public static function escape($str, $quote = '`')
454459
{
455460
if (is_array($str)) {
456461
foreach ($str as $key => $value) {
457462
$str[$key] = static::escape($value);
458463
}
459464
return $str;
460465
}
466+
461467
if (static::$MODE & Context::ANSI_QUOTES) {
462-
return '"' . str_replace('"', '""', $str) . '"';
468+
$quote = '"';
463469
}
464-
return '`' . str_replace('`', '``', $str) . '`';
470+
return $quote . str_replace($quote, $quote . $quote, $str) . $quote;
465471
}
466472
}
467473

tests/Components/OptionsArrayTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,34 @@ public function testParse()
3636
);
3737
}
3838

39+
public function testHas()
40+
{
41+
$component = new OptionsArray(
42+
array(
43+
1 => 'A',
44+
2 => array(
45+
'name' => 'B',
46+
'value' => 'test',
47+
'value_' => 'test',
48+
'equal' => false,
49+
),
50+
3 => 'C'
51+
)
52+
);
53+
$this->assertTrue($component->has('A'));
54+
$this->assertEquals('test', $component->has('B'));
55+
$this->assertTrue($component->has('C'));
56+
$this->assertFalse($component->has('D'));
57+
}
58+
59+
public function testRemove()
60+
{
61+
$component = new OptionsArray(array('a', 'b', 'c'));
62+
$this->assertTrue($component->remove('b'));
63+
$this->assertFalse($component->remove('d'));
64+
$this->assertEquals($component->options, array(0 => 'a', 2 => 'c'));
65+
}
66+
3967
public function testMerge()
4068
{
4169
$component = new OptionsArray(array('a'));

0 commit comments

Comments
 (0)