From 595ba75a50e2cb8eb0b5fc7088f398474f17785d Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Wed, 23 Aug 2017 14:16:54 +1200 Subject: [PATCH] API Make FormField::hasClass return a boolean instead of an int --- src/Forms/FormField.php | 11 +++++------ tests/php/Forms/FormFieldTest.php | 11 +++++++++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/Forms/FormField.php b/src/Forms/FormField.php index ce792983b..f77e33865 100644 --- a/src/Forms/FormField.php +++ b/src/Forms/FormField.php @@ -1247,17 +1247,16 @@ class FormField extends RequestHandler } /** + * Returns whether the current field has the given class added + * * @param string $class * - * @return int + * @return bool */ public function hasClass($class) { - $patten = '/' . strtolower($class) . '/i'; - - $subject = strtolower(static::class . ' ' . $this->extraClass()); - - return preg_match($patten, $subject); + $classes = explode(' ', strtolower($this->extraClass())); + return in_array(strtolower(trim($class)), $classes); } /** diff --git a/tests/php/Forms/FormFieldTest.php b/tests/php/Forms/FormFieldTest.php index d2ac3fa5a..07c5d618e 100644 --- a/tests/php/Forms/FormFieldTest.php +++ b/tests/php/Forms/FormFieldTest.php @@ -373,4 +373,15 @@ class FormFieldTest extends SapphireTest $schema['message']['value'] ); } + + public function testHasClass() + { + $field = new FormField('Test'); + $field->addExtraClass('foo BAr cool-banana'); + + $this->assertTrue($field->hasClass('foo')); + $this->assertTrue($field->hasClass('bAr')); + $this->assertFalse($field->hasClass('banana')); + $this->assertTrue($field->hasClass('cool-BAnana')); + } }