Refactoring, fixing SessionComponent test, adding String::tokenize()

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6306 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2008-01-02 03:06:57 +00:00
parent 7ea47e9893
commit 9c68a253e4
3 changed files with 63 additions and 49 deletions

View file

@ -1449,56 +1449,10 @@ class DboSource extends DataSource {
if (empty($alias)) {
$alias = $model->alias;
}
if (!is_array($fields)) {
if (!empty($fields)) {
$depth = 0;
$offset = 0;
$buffer = '';
$results = array();
$length = strlen($fields);
while ($offset <= $length) {
$tmpOffset = -1;
$offsets = array(strpos($fields, ',', $offset), strpos($fields, '(', $offset), strpos($fields, ')', $offset));
for ($i = 0; $i < 3; $i++) {
if ($offsets[$i] !== false && ($offsets[$i] < $tmpOffset || $tmpOffset == -1)) {
$tmpOffset = $offsets[$i];
}
}
if ($tmpOffset !== -1) {
$buffer .= substr($fields, $offset, ($tmpOffset - $offset));
if ($fields{$tmpOffset} == ',' && $depth == 0) {
$results[] = $buffer;
$buffer = '';
} else {
$buffer .= $fields{$tmpOffset};
}
if ($fields{$tmpOffset} == '(') {
$depth++;
}
if ($fields{$tmpOffset} == ')') {
$depth--;
}
$offset = ++$tmpOffset;
} else {
$results[] = $buffer . substr($fields, $offset);
$offset = $length + 1;
}
}
if (empty($results) && !empty($buffer)) {
$results[] = $buffer;
}
if (!empty($results)) {
$fields = array_map('trim', $results);
} else {
$fields = array();
}
}
}
if (empty($fields)) {
$fields = array_keys($model->schema());
} elseif (!is_array($fields)) {
$fields = String::tokenize($fields);
} else {
$fields = array_filter($fields);
}

View file

@ -370,6 +370,66 @@ class String extends Object {
}
return $_this->ascii($upperCase);
}
/**
* Tokenizes a string using $separator, ignoring any instance of $separator that appears between $leftBound
* and $rightBound
*
* @param string $data The data to tokenize
* @param string $separator The token to split the data on
* @return string
* @access public
* @static
*/
function tokenize($data, $separator = ',', $leftBound = '(', $rightBound = ')') {
if(empty($data) || is_array($data)) {
return $data;
}
$depth = 0;
$offset = 0;
$buffer = '';
$results = array();
$length = strlen($data);
while ($offset <= $length) {
$tmpOffset = -1;
$offsets = array(strpos($data, $separator, $offset), strpos($data, $leftBound, $offset), strpos($data, $rightBound, $offset));
for ($i = 0; $i < 3; $i++) {
if ($offsets[$i] !== false && ($offsets[$i] < $tmpOffset || $tmpOffset == -1)) {
$tmpOffset = $offsets[$i];
}
}
if ($tmpOffset !== -1) {
$buffer .= substr($data, $offset, ($tmpOffset - $offset));
if ($data{$tmpOffset} == $separator && $depth == 0) {
$results[] = $buffer;
$buffer = '';
} else {
$buffer .= $data{$tmpOffset};
}
if ($data{$tmpOffset} == $leftBound) {
$depth++;
}
if ($data{$tmpOffset} == $rightBound) {
$depth--;
}
$offset = ++$tmpOffset;
} else {
$results[] = $buffer . substr($data, $offset);
$offset = $length + 1;
}
}
if (empty($results) && !empty($buffer)) {
$results[] = $buffer;
}
if (!empty($results)) {
$data = array_map('trim', $results);
} else {
$data = array();
}
return $data;
}
/**
* Return the Code points range for Unicode characters
*

View file

@ -43,7 +43,7 @@ class SessionComponentTest extends CakeTestCase {
function testSessionAutoStart() {
$this->Session->startup(new SessionTestController());
$this->assertTrue(isset($_SESSION) && empty($_SESSION));
$this->assertTrue(isset($_SESSION));
}
function testSessionWriting() {