|
| 1 | +<?php |
| 2 | + |
| 3 | +final class PhabricatorBoolConfigType |
| 4 | + extends PhabricatorTextConfigType { |
| 5 | + |
| 6 | + const TYPEKEY = 'bool'; |
| 7 | + |
| 8 | + protected function newCanonicalValue( |
| 9 | + PhabricatorConfigOption $option, |
| 10 | + $value) { |
| 11 | + |
| 12 | + if (!preg_match('/^(true|false)\z/', $value)) { |
| 13 | + throw $this->newException( |
| 14 | + pht( |
| 15 | + 'Value for option "%s" of type "%s" must be either '. |
| 16 | + '"true" or "false".', |
| 17 | + $option->getKey(), |
| 18 | + $this->getTypeKey())); |
| 19 | + } |
| 20 | + |
| 21 | + return ($value === 'true'); |
| 22 | + } |
| 23 | + |
| 24 | + public function newDisplayValue( |
| 25 | + PhabricatorConfigOption $option, |
| 26 | + $value) { |
| 27 | + |
| 28 | + if ($value) { |
| 29 | + return 'true'; |
| 30 | + } else { |
| 31 | + return 'false'; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + public function validateStoredValue( |
| 36 | + PhabricatorConfigOption $option, |
| 37 | + $value) { |
| 38 | + |
| 39 | + if (!is_bool($value)) { |
| 40 | + throw $this->newException( |
| 41 | + pht( |
| 42 | + 'Option "%s" is of type "%s", but the configured value is not '. |
| 43 | + 'a boolean.', |
| 44 | + $option->getKey(), |
| 45 | + $this->getTypeKey())); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + protected function newControl(PhabricatorConfigOption $option) { |
| 50 | + $bool_map = $option->getBoolOptions(); |
| 51 | + |
| 52 | + $map = array( |
| 53 | + '' => pht('(Use Default)'), |
| 54 | + ) + array( |
| 55 | + 'true' => idx($bool_map, 0), |
| 56 | + 'false' => idx($bool_map, 1), |
| 57 | + ); |
| 58 | + |
| 59 | + return id(new AphrontFormSelectControl()) |
| 60 | + ->setOptions($map); |
| 61 | + } |
| 62 | +} |
0 commit comments