|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * Cypher DSL |
| 5 | + * Copyright (C) 2021 Wikibase Solutions |
| 6 | + * |
| 7 | + * This program is free software; you can redistribute it and/or |
| 8 | + * modify it under the terms of the GNU General Public License |
| 9 | + * as published by the Free Software Foundation; either version 2 |
| 10 | + * of the License, or (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program; if not, write to the Free Software |
| 19 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | + */ |
| 21 | + |
| 22 | +namespace WikibaseSolutions\CypherDSL; |
| 23 | + |
| 24 | +use function sprintf; |
| 25 | +use WikibaseSolutions\CypherDSL\Traits\BooleanTypeTrait; |
| 26 | +use WikibaseSolutions\CypherDSL\Types\AnyType; |
| 27 | +use WikibaseSolutions\CypherDSL\Types\PropertyTypes\BooleanType; |
| 28 | + |
| 29 | +/** |
| 30 | + * Represents the IS NOT NULL comparison operator. |
| 31 | + * |
| 32 | + * @see https://neo4j.com/docs/cypher-manual/current/syntax/operators/#query-operators-comparison |
| 33 | + */ |
| 34 | +class IsNotNull implements BooleanType |
| 35 | +{ |
| 36 | + use BooleanTypeTrait; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var AnyType The type to test against null |
| 40 | + */ |
| 41 | + private AnyType $expression; |
| 42 | + private bool $insertParentheses; |
| 43 | + |
| 44 | + /** |
| 45 | + * IS NOT NULL constructor. |
| 46 | + * |
| 47 | + * @param AnyType $expression The type to test against null. |
| 48 | + */ |
| 49 | + public function __construct(AnyType $expression, bool $insertParentheses = true) |
| 50 | + { |
| 51 | + $this->expression = $expression; |
| 52 | + $this->insertParentheses = $insertParentheses; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Returns the expression to test against null. |
| 57 | + * |
| 58 | + * @return AnyType |
| 59 | + */ |
| 60 | + public function getExpression(): AnyType |
| 61 | + { |
| 62 | + return $this->expression; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @inheritDoc |
| 67 | + */ |
| 68 | + public function toQuery(): string |
| 69 | + { |
| 70 | + return sprintf($this->insertParentheses ? "(%s IS NOT NULL)" : "%s IS NOT NULL", $this->expression->toQuery()); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Returns whether or not the operator inserts parenthesis. |
| 75 | + * |
| 76 | + * @return bool |
| 77 | + */ |
| 78 | + public function insertsParentheses(): bool |
| 79 | + { |
| 80 | + return $this->insertParentheses; |
| 81 | + } |
| 82 | +} |
0 commit comments