From 1974e79d71d5dd232c44cd5e7d49f4804a96d78d Mon Sep 17 00:00:00 2001 From: Daniel Hensby Date: Thu, 15 Oct 2015 15:18:32 +0100 Subject: [PATCH] Allow multi-line enum declarations --- model/fieldtypes/Enum.php | 2 +- tests/forms/EnumFieldTest.php | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/model/fieldtypes/Enum.php b/model/fieldtypes/Enum.php index 7998f5bf6..c09ef3c2d 100644 --- a/model/fieldtypes/Enum.php +++ b/model/fieldtypes/Enum.php @@ -38,7 +38,7 @@ class Enum extends StringField { public function __construct($name = null, $enum = NULL, $default = NULL) { if($enum) { if(!is_array($enum)) { - $enum = preg_split("/ *, */", trim($enum)); + $enum = preg_split("/\s*,\s*/", trim($enum, ", \t\n\r\0\x0B")); } $this->enum = $enum; diff --git a/tests/forms/EnumFieldTest.php b/tests/forms/EnumFieldTest.php index 394cd5bf3..be1de7754 100644 --- a/tests/forms/EnumFieldTest.php +++ b/tests/forms/EnumFieldTest.php @@ -16,4 +16,26 @@ class EnumFieldTest extends SapphireTest { $this->assertEquals(true, $searchField->getHasEmptyDefault()); $this->assertEquals($anyText, $searchField->getEmptyString()); } + + public function testEnumParsing() { + $enum = new Enum('testField', " + Item1, + Item2, + Item 3, + Item-4, + item 5 + still 5, + trailing comma, + "); + + $this->assertEquals(ArrayLib::valuekey(array( + 'Item1', + 'Item2', + 'Item 3', + 'Item-4', + 'item 5 + still 5', + 'trailing comma' + )), $enum->enumValues()); + } }