mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
BUGFIX: Update Object::parse_class_spec() to handle arrays.
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@101105 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
9395633251
commit
5a68d0f3b5
@ -150,6 +150,11 @@ abstract class Object {
|
||||
$class = null;
|
||||
$args = array();
|
||||
$passedBracket = false;
|
||||
|
||||
// Keep track of the current bucket that we're putting data into
|
||||
$bucket = &$args;
|
||||
$bucketStack = array();
|
||||
|
||||
foreach($tokens as $token) {
|
||||
$tName = is_array($token) ? $token[0] : $token;
|
||||
// Get the class naem
|
||||
@ -165,15 +170,15 @@ abstract class Object {
|
||||
case "'": $argString = str_replace(array("\\\\", "\\'"),array("\\", "'"), substr($argString,1,-1)); break;
|
||||
default: throw new Exception("Bad T_CONSTANT_ENCAPSED_STRING arg $argString");
|
||||
}
|
||||
$args[] = $argString;
|
||||
$bucket[] = $argString;
|
||||
break;
|
||||
|
||||
case T_DNUMBER:
|
||||
$args[] = (double)$token[1];
|
||||
$bucket[] = (double)$token[1];
|
||||
break;
|
||||
|
||||
case T_LNUMBER:
|
||||
$args[] = (int)$token[1];
|
||||
$bucket[] = (int)$token[1];
|
||||
break;
|
||||
|
||||
case T_STRING:
|
||||
@ -182,8 +187,21 @@ abstract class Object {
|
||||
case 'false': $args[] = false; break;
|
||||
default: throw new Exception("Bad T_STRING arg '{$token[1]}'");
|
||||
}
|
||||
|
||||
case T_ARRAY:
|
||||
// Add an empty array to the bucket
|
||||
$bucket[] = array();
|
||||
$bucketStack[] = &$bucket;
|
||||
$bucket = &$bucket[sizeof($bucket)-1];
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
if($tName == ')') {
|
||||
// Pop-by-reference
|
||||
$bucket = &$bucketStack[sizeof($bucketStack)-1];
|
||||
array_pop($bucketStack);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -337,18 +337,31 @@ class ObjectTest extends SapphireTest {
|
||||
}
|
||||
|
||||
public function testParseClassSpec() {
|
||||
// Simple case
|
||||
$this->assertEquals(
|
||||
array('Versioned',array('Stage', 'Live')),
|
||||
Object::parse_class_spec("Versioned('Stage','Live')")
|
||||
);
|
||||
// String with commas
|
||||
$this->assertEquals(
|
||||
array('Versioned',array('Stage,Live', 'Stage')),
|
||||
Object::parse_class_spec("Versioned('Stage,Live','Stage')")
|
||||
);
|
||||
// String with quotes
|
||||
$this->assertEquals(
|
||||
array('Versioned',array('Stage\'Stage,Live\'Live', 'Live')),
|
||||
Object::parse_class_spec("Versioned('Stage\'Stage,Live\'Live','Live')")
|
||||
);
|
||||
// Array
|
||||
$this->assertEquals(
|
||||
array('Enum',array(array('Accepted', 'Pending', 'Declined', 'Unsubmitted'), 'Unsubmitted')),
|
||||
Object::parse_class_spec("Enum(array('Accepted', 'Pending', 'Declined', 'Unsubmitted'), 'Unsubmitted')")
|
||||
);
|
||||
// Nested array
|
||||
$this->assertEquals(
|
||||
array('Enum',array(array('Accepted', 'Pending', 'Declined', array('UnsubmittedA','UnsubmittedB')), 'Unsubmitted')),
|
||||
Object::parse_class_spec("Enum(array('Accepted', 'Pending', 'Declined', array('UnsubmittedA','UnsubmittedB')), 'Unsubmitted')")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user