/** * Parses a string into a SQL expression and it's alias * * @param String $name The SQL string to parse * @return array Returns an array where the first element is the * SQL expression and the second is the alias */ public static function parseSQLAlias($string) { $string = (string) $string; // If there is no obvious alias, take an easy out if (!\r8\str\contains(" AS ", $string)) { $alias = null; } else { if (!\r8\str\contains("`", $string)) { list($string, $alias) = explode(" AS ", $string, 2); $alias = trim($alias); } else { $parser = new \r8\Quoter(); list($string, $alias) = $parser->clearQuotes()->setQuote("`")->parse($string)->setIncludeQuoted(FALSE)->explode(" AS "); $alias = trim($alias); } } $string = trim($string); if (\r8\IsEmpty($string)) { $string = null; } $alias = \r8\str\stripW($alias); if (\r8\IsEmpty($alias)) { $alias = null; } return array($string, $alias); }
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()); }