/** * Cleans up a SQL query for comparison * * @param String $sql The SQL to clean * @return String */ public static function cleanSQL($sql) { $quoter = new \r8\Quoter(); $quoter->setQuote('"')->setQuote("'")->setQuote("`"); $parsed = $quoter->parse($sql); $parsed->setIncludeQuoted(FALSE)->setIncludeUnquoted(TRUE); $keywords = '/\\b(?:' . implode("|", self::$keywords) . ')\\b/i'; $breaks = '/\\b(' . implode("|", self::$breaks) . ')\\b/i'; $parsed->filter(new \r8\Filter\Chain(r8(new \r8\Curry\Call('str_replace'))->setLeft(array("\n", "\r"), " "), r8(new \r8\Curry\Call('\\r8\\str\\stripRepeats'))->setRight(" "), r8(new \r8\Curry\Call('preg_replace_callback'))->setLeft($keywords, function ($value) { return strtoupper($value[0]); }), r8(new \r8\Curry\Call('preg_replace'))->setLeft($breaks, "\n\\1"))); return trim($parsed->__toString(), " ;"); }
public function testFilter() { $list = new \r8\Quoter(); $parsed = $list->parse("string 'with' quotes")->filter(new \r8\Curry\Call("strtoupper"))->__toString(); $this->assertSame("STRING 'WITH' QUOTES", $parsed); $parsed = $list->parse("string 'with' quotes")->setIncludeQuoted(FALSE)->filter(new \r8\Curry\Call("strtoupper"))->__toString(); $this->assertSame("STRING 'with' QUOTES", $parsed); $parsed = $list->parse("string 'with' quotes")->setIncludeUnquoted(FALSE)->filter(new \r8\Curry\Call("strtoupper"))->__toString(); $this->assertSame("string 'WITH' quotes", $parsed); $parsed = $list->parse("string 'with' quotes")->setIncludeUnquoted(FALSE)->setIncludeQuoted(FALSE)->filter(new \r8\Curry\Call("strtoupper"))->__toString(); $this->assertSame("string 'with' quotes", $parsed); }
public function testParse_oddQuotes() { $quoter = new \r8\Quoter(); $quoter->clearQuotes()->setQuote("<({", array("END OF QUOTE", "))")); $result = $quoter->parse("<({This isEND OF QUOTE a string <({with stuff)) in it"); $this->assertThat($result, $this->isInstanceOf("r8\\Quoter\\Parsed")); $this->assertSame(array("<({This isEND OF QUOTE", " a string ", "<({with stuff))", " in it"), array_map("strval", $result->getSections())); $offset = $result->getSections(); $offset = $offset[0]; $this->assertThat($offset, $this->isInstanceOf("r8\\Quoter\\Section\\Quoted")); $this->assertSame("This is", $offset->getContent()); $this->assertSame("<({", $offset->getOpenQuote()); $this->assertSame("END OF QUOTE", $offset->getCloseQuote()); $offset = $result->getSections(); $offset = $offset[1]; $this->assertThat($offset, $this->isInstanceOf("r8\\Quoter\\Section\\Unquoted")); $this->assertSame(" a string ", $offset->getContent()); $offset = $result->getSections(); $offset = $offset[2]; $this->assertThat($offset, $this->isInstanceOf("r8\\Quoter\\Section\\Quoted")); $this->assertSame("with stuff", $offset->getContent()); $this->assertSame("<({", $offset->getOpenQuote()); $this->assertSame("))", $offset->getCloseQuote()); $offset = $result->getSections(); $offset = $offset[3]; $this->assertThat($offset, $this->isInstanceOf("r8\\Quoter\\Section\\Unquoted")); $this->assertSame(" in it", $offset->getContent()); }