mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-09-28 20:29:15 +02:00
parent
6a6cf2f9eb
commit
ef58799103
@ -32,45 +32,46 @@ class DBEnum extends DBString
|
||||
private static $default_search_filter_class = 'ExactMatchFilter';
|
||||
|
||||
/**
|
||||
* Create a new Enum field.
|
||||
* Create a new Enum field, which is a value within a defined set, with an optional default.
|
||||
*
|
||||
* Example usage in {@link DataObject::$db} with comma-separated string
|
||||
* notation ('Val1' is default)
|
||||
* Example field specification strings:
|
||||
*
|
||||
* <code>
|
||||
* "MyField" => "Enum('Val1, Val2, Val3', 'Val1')"
|
||||
* </code>
|
||||
*
|
||||
* Example usage in in {@link DataObject::$db} with array notation
|
||||
* ('Val1' is default)
|
||||
*
|
||||
* <code>
|
||||
* "MyField" => "Enum(array('Val1', 'Val2', 'Val3'), 'Val1')"
|
||||
* "MyField" => "Enum('Val1, Val2, Val3')" // First item 'Val1' is default implicitly
|
||||
* "MyField" => "Enum('Val1, Val2, Val3', 'Val2')" // 'Val2' is default explicitly
|
||||
* "MyField" => "Enum('Val1, Val2, Val3', null)" // Force empty (no) default
|
||||
* "MyField" => "Enum(array('Val1', 'Val2', 'Val3'), 'Val1')" // Supports array notation as well
|
||||
* </code>
|
||||
*
|
||||
* @param string $name
|
||||
* @param string|array $enum A string containing a comma separated list of options or an array of Vals.
|
||||
* @param string $default The default option, which is either NULL or one of the items in the enumeration.
|
||||
* @param string|int|null $default The default option, which is either NULL or one of the items in the enumeration.
|
||||
* If passing in an integer (non-string) it will default to the index of that item in the list.
|
||||
* Set to null or empty string to allow empty values
|
||||
* @param array $options Optional parameters for this DB field
|
||||
*/
|
||||
public function __construct($name = null, $enum = null, $default = null, $options = [])
|
||||
public function __construct($name = null, $enum = null, $default = 0, $options = [])
|
||||
{
|
||||
if ($enum) {
|
||||
$this->setEnum($enum);
|
||||
$enum = $this->getEnum();
|
||||
|
||||
// If there's a default, then
|
||||
if ($default) {
|
||||
if (in_array($default, $this->getEnum())) {
|
||||
// If there's a default, then use this
|
||||
if ($default && !is_int($default)) {
|
||||
if (in_array($default, $enum)) {
|
||||
$this->setDefault($default);
|
||||
} else {
|
||||
user_error("Enum::__construct() The default value '$default' does not match any item in the"
|
||||
. " enumeration", E_USER_ERROR);
|
||||
user_error(
|
||||
"Enum::__construct() The default value '$default' does not match any item in the enumeration",
|
||||
E_USER_ERROR
|
||||
);
|
||||
}
|
||||
|
||||
// By default, set the default value to the first item
|
||||
} elseif (is_int($default) && $default < count($enum)) {
|
||||
// Set to specified index if given
|
||||
$this->setDefault($enum[$default]);
|
||||
} else {
|
||||
$enum = $this->getEnum();
|
||||
$this->setDefault(reset($enum));
|
||||
// Set to null if specified
|
||||
$this->setDefault(null);
|
||||
}
|
||||
}
|
||||
|
||||
@ -187,7 +188,7 @@ class DBEnum extends DBString
|
||||
ltrim(preg_replace('/,\s*\n\s*$/', '', $enum))
|
||||
);
|
||||
}
|
||||
$this->enum = $enum;
|
||||
$this->enum = array_values($enum);
|
||||
return $this;
|
||||
}
|
||||
|
||||
@ -210,6 +211,7 @@ class DBEnum extends DBString
|
||||
public function setDefault($default)
|
||||
{
|
||||
$this->default = $default;
|
||||
$this->setDefaultValue($default);
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
31
tests/php/ORM/DBEnumTest.php
Normal file
31
tests/php/ORM/DBEnumTest.php
Normal file
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace SilverStripe\ORM\Tests;
|
||||
|
||||
use SilverStripe\Dev\SapphireTest;
|
||||
use SilverStripe\ORM\FieldType\DBEnum;
|
||||
use SilverStripe\ORM\FieldType\DBField;
|
||||
|
||||
class DBEnumTest extends SapphireTest
|
||||
{
|
||||
public function testDefault()
|
||||
{
|
||||
/** @var DBEnum $enum1 */
|
||||
$enum1 = DBField::create_field('Enum("A, B, C, D")', null);
|
||||
/** @var DBEnum $enum2 */
|
||||
$enum2 = DBField::create_field('Enum("A, B, C, D", "")', null);
|
||||
/** @var DBEnum $enum3 */
|
||||
$enum3 = DBField::create_field('Enum("A, B, C, D", null)', null);
|
||||
/** @var DBEnum $enum4 */
|
||||
$enum4 = DBField::create_field('Enum("A, B, C, D", 1)', null);
|
||||
|
||||
$this->assertEquals('A', $enum1->getDefaultValue());
|
||||
$this->assertEquals('A', $enum1->getDefault());
|
||||
$this->assertEquals(null, $enum2->getDefaultValue());
|
||||
$this->assertEquals(null, $enum2->getDefault());
|
||||
$this->assertEquals(null, $enum3->getDefaultValue());
|
||||
$this->assertEquals(null, $enum3->getDefault());
|
||||
$this->assertEquals('B', $enum4->getDefaultValue());
|
||||
$this->assertEquals('B', $enum4->getDefault());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user