Skip to content

Commit 14f013b

Browse files
committed
made parentheses optional
1 parent a798e1c commit 14f013b

File tree

6 files changed

+52
-16
lines changed

6 files changed

+52
-16
lines changed

src/IsNotNull.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@ class IsNotNull implements BooleanType
2020
* @var AnyType The type to test against null
2121
*/
2222
private AnyType $expression;
23+
private bool $insertParentheses;
2324

2425
/**
2526
* IS NOT NULL constructor.
2627
*
2728
* @param AnyType $expression The type to test against null.
2829
*/
29-
public function __construct(AnyType $expression)
30+
public function __construct(AnyType $expression, bool $insertParentheses = true)
3031
{
3132
$this->expression = $expression;
33+
$this->insertParentheses = $insertParentheses;
3234
}
3335

3436
/**
@@ -46,6 +48,16 @@ public function getExpression(): AnyType
4648
*/
4749
public function toQuery(): string
4850
{
49-
return sprintf("(%s IS NOT NULL)", $this->expression->toQuery());
51+
return sprintf($this->insertParentheses ? "(%s IS NOT NULL)" : "%s IS NOT NULL", $this->expression->toQuery());
52+
}
53+
54+
/**
55+
* Returns whether or not the operator inserts parenthesis.
56+
*
57+
* @return bool
58+
*/
59+
public function insertsParentheses(): bool
60+
{
61+
return $this->insertParentheses;
5062
}
5163
}

src/IsNull.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@ class IsNull implements BooleanType
2020
* @var AnyType The type to test against null
2121
*/
2222
private AnyType $expression;
23+
private bool $insertParentheses;
2324

2425
/**
2526
* IS NULL constructor.
2627
*
2728
* @param AnyType $expression The type to test against null.
2829
*/
29-
public function __construct(AnyType $expression)
30+
public function __construct(AnyType $expression, bool $insertParentheses = true)
3031
{
3132
$this->expression = $expression;
33+
$this->insertParentheses = $insertParentheses;
3234
}
3335

3436
/**
@@ -46,6 +48,16 @@ public function getExpression(): AnyType
4648
*/
4749
public function toQuery(): string
4850
{
49-
return sprintf("(%s IS NULL)", $this->expression->toQuery());
51+
return sprintf($this->insertParentheses ? "(%s IS NULL)" : "%s IS NULL", $this->expression->toQuery());
52+
}
53+
54+
/**
55+
* Returns whether or not the operator inserts parenthesis.
56+
*
57+
* @return bool
58+
*/
59+
public function insertsParentheses(): bool
60+
{
61+
return $this->insertParentheses;
5062
}
5163
}

src/Traits/PropertyTypeTrait.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,16 @@ public function in(ListType $right, bool $insertParentheses = true): In
7676
/**
7777
* @inheritDoc
7878
*/
79-
public function isNull(): IsNull
79+
public function isNull(bool $insertParentheses = true): IsNull
8080
{
81-
return new IsNull($this);
81+
return new IsNull($this, $insertParentheses);
8282
}
8383

8484
/**
8585
* @inheritDoc
8686
*/
87-
public function isNotNull(): IsNotNull
87+
public function isNotNull(bool $insertParentheses = true): IsNotNull
8888
{
89-
return new IsNotNull($this);
89+
return new IsNotNull($this, $insertParentheses);
9090
}
9191
}

src/Types/PropertyTypes/PropertyType.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,18 @@ public function in(ListType $right): In;
6363
/**
6464
* Checks whether the element is null.
6565
*
66+
* @param bool $insertsParentheses whether to insert parentheses.
67+
*
6668
* @return IsNull
6769
*/
68-
public function isNull(): IsNull;
70+
public function isNull(bool $insertsParentheses = true): IsNull;
6971

7072
/**
7173
* Checks whether the element is not null.
7274
*
75+
* @param bool $insertsParentheses whether to insert parentheses.
76+
*
7377
* @return IsNotNull
7478
*/
75-
public function isNotNull(): IsNotNull;
79+
public function isNotNull(bool $insertsParentheses = true): IsNotNull;
7680
}

tests/Unit/IsNotNullTest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,16 @@ class IsNotNullTest extends TestCase
3434

3535
public function testToQuery(): void
3636
{
37-
$not = new IsNotNull($this->getQueryConvertableMock(BooleanType::class, "true"));
37+
$not = new IsNotNull($this->getQueryConvertableMock(BooleanType::class, "true"), false);
3838

39-
$this->assertSame("(true IS NOT NULL)", $not->toQuery());
39+
$this->assertFalse($not->insertsParentheses());
40+
41+
$this->assertSame("true IS NOT NULL", $not->toQuery());
4042

4143
$not = new IsNotNull($not);
4244

43-
$this->assertSame("((true IS NOT NULL) IS NOT NULL)", $not->toQuery());
45+
$this->assertSame("(true IS NOT NULL IS NOT NULL)", $not->toQuery());
46+
47+
$this->assertTrue($not->insertsParentheses());
4448
}
4549
}

tests/Unit/IsNullTest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,16 @@ class IsNullTest extends TestCase
3434

3535
public function testToQuery(): void
3636
{
37-
$not = new IsNull($this->getQueryConvertableMock(BooleanType::class, "true"));
37+
$not = new IsNull($this->getQueryConvertableMock(BooleanType::class, "true"), false);
3838

39-
$this->assertSame("(true IS NULL)", $not->toQuery());
39+
$this->assertFalse($not->insertsParentheses());
40+
41+
$this->assertSame("true IS NULL", $not->toQuery());
4042

4143
$not = new IsNull($not);
4244

43-
$this->assertSame("((true IS NULL) IS NULL)", $not->toQuery());
45+
$this->assertSame("(true IS NULL IS NULL)", $not->toQuery());
46+
47+
$this->assertTrue($not->insertsParentheses());
4448
}
4549
}

0 commit comments

Comments
 (0)