/** * Performs some special handling if the operator requires a range or a * list. * * @param string, array $operand */ protected function _validateRightOperand($operand) { if ($this->_operator == self::OP_BETWEEN || $this->_operator == self::OP_IN) { /* This logic doesn't attempt to verify that the ends of a range are valid for range comparison, as that's sort of a pain. */ if (is_array($operand)) { if ($this->_operator == self::OP_BETWEEN) { if (count($operand) != 2) { throw new InvalidArgumentException('When passing an array as an operand to a range ' . 'operator, it must contain exactly two values.'); } $operand = implode('_', $operand); } else { foreach ($operand as &$opComponent) { $opComponent = \PFXUtils::escape($opComponent, '|'); } $operand = implode('|', $operand); } } elseif ($this->_operator == self::OP_BETWEEN) { $components = explode('_', $operand); if (count($components) != 2) { throw new InvalidArgumentException('Operands used with the <> operator must have ' . 'exactly two boundaries separated by an underscore.'); } } } return parent::_validateRightOperand($operand); }
/** * Tests PFXUtils::escape(). */ public function testEscape() { $str = <<<EOF "Robert's" house EOF; $expected = <<<EOF \\"Robert\\'s\\" house EOF; $escapeChars = <<<EOF "' EOF; $this->assertEquals($expected, PFXUtils::escape($str, $escapeChars)); // Maybe we just want to escape the o $expected = <<<EOF "R\\obert's" h\\ouse EOF; $this->assertEquals($expected, PFXUtils::escape($str, 'o')); /* The escape character itself is implicitly escaped, as long as it's not already escaping an escapable character. */ $str = <<<EOF "R\\obert\\'s" house EOF; /* Note that this isn't literally what we expect...we are escaping the escape character for PHP's benefit. */ $expected = <<<EOF "R\\obert\\\\'s" h\\ouse EOF; $this->assertEquals($expected, PFXUtils::escape($str, 'o')); // We can use an alternate escape character if we want $str = <<<EOF "Robert's" house is a real dump. EOF; $expected = <<<EOF '"Robert''s'" house is a real dump'. EOF; $this->assertEquals($expected, PFXUtils::escape($str, '".', "'")); // Multiple-character escape sequences are not allowed $this->assertThrows('InvalidArgumentException', array('PFXUtils', 'escape'), array($str, '".', "''")); }
/** * Validates the right operand and returns it if valid. * * @param string $operand * @return string */ protected function _validateRightOperand($operand) { if (!is_scalar($operand)) { throw new InvalidArgumentException('Operands must be passed as scalar values.'); } // This does nothing other than escaping ANDs and ORs return \PFXUtils::escape($operand, GaDataLogicalCollection::OP_AND . GaDataLogicalCollection::OP_OR); }