diff --git a/Civi/FormProcessor/Type/BooleanType.php b/Civi/FormProcessor/Type/BooleanType.php new file mode 100644 index 0000000000000000000000000000000000000000..862e7ea3696d6eb4c03a366b41c7f14b4946f558 --- /dev/null +++ b/Civi/FormProcessor/Type/BooleanType.php @@ -0,0 +1,65 @@ + + * @license http://www.gnu.org/licenses/agpl-3.0.html + */ + +namespace Civi\FormProcessor\Type; + +use \Civi\FormProcessor\Config\Specification; +use \Civi\FormProcessor\Config\SpecificationBag; + +use \CRM_FormProcessor_ExtensionUtil as E; + +class BooleanType extends AbstractType { + + public function validateValue($value, $allValues = []) { + return \CRM_Utils_Rule::boolean($value); + } + + /** + * Returns the type number from CRM_Utils_Type + */ + public function getCrmType() { + return \CRM_Utils_Type::T_BOOLEAN; + } + + /** + * Normalize the input value. + * + * @param $value + * + * @return mixed + */ + public function normalizeValue($value) { + // switch-case does loose comparison, so: + if (gettype($value) === 'boolean') { + return $value; + } + switch ((string) $value) { + case '1'; + case 'True': + case 'true': + case 'Yes': + case 'yes': + case 'Y': + case 'T': + return TRUE; + } + // We've already validated this as a boolean so we don't need to enumerate the FALSE values. + return FALSE; + } + + /** + * Denormalize the input value. + * + * @param $value + * + * @return mixed + */ + public function denormalizeValue($value) { + return $this->normalizeValue($value); + } + +} diff --git a/Civi/FormProcessor/Type/Factory.php b/Civi/FormProcessor/Type/Factory.php index 6f1bcd417d6fb5061ea48096844b8755ec4fa73c..32209a9fde262ba962a3c6a94e8981f61b04175d 100644 --- a/Civi/FormProcessor/Type/Factory.php +++ b/Civi/FormProcessor/Type/Factory.php @@ -24,7 +24,7 @@ class Factory { $this->addType(new GenericType('Text', E::ts('Long text'))); $this->addType(new DateType(E::ts('Date'))); $this->addType(new TimeType(E::ts('Time'))); - $this->addType(new GenericType('Boolean', E::ts('Yes/No'))); + $this->addType(new BooleanType('Boolean', E::ts('Yes/No'))); $this->addType(new YesNoOptionListType('YesNoOptionList', E::ts('Yes/No as Option List'))); $this->addType(new OptionGroupType('OptionGroup', E::ts('Option Group'))); $this->addType(new ContactSubTypeType('ContactSubType', E::ts('Contact Sub Type')));