Remove product literal strings in "pht()", part 6
[phabricator.git] / src / applications / settings / setting / PhabricatorSelectSetting.php
blobfb3cb2135f62453b1a81bdb9ddd42e7c2bdaf680
1 <?php
3 abstract class PhabricatorSelectSetting
4 extends PhabricatorSetting {
6 abstract protected function getSelectOptions();
8 final protected function newCustomEditField($object) {
9 $setting_key = $this->getSettingKey();
10 $default_value = $object->getDefaultValue($setting_key);
12 $options = $this->getSelectOptions();
14 if (isset($options[$default_value])) {
15 $default_label = pht('Default (%s)', $options[$default_value]);
16 } else {
17 $default_label = pht('Default (Unknown, "%s")', $default_value);
20 if (empty($options[''])) {
21 $options = array(
22 '' => $default_label,
23 ) + $options;
26 return $this->newEditField($object, new PhabricatorSelectEditField())
27 ->setOptions($options);
30 public function assertValidValue($value) {
31 // This is a slightly stricter check than the transaction check. It's
32 // OK for empty string to go through transactions because it gets converted
33 // to null later, but we shouldn't be reading the empty string from
34 // storage.
35 if ($value === null) {
36 return;
39 if (!strlen($value)) {
40 throw new Exception(
41 pht(
42 'Empty string is not a valid setting for "%s".',
43 $this->getSettingName()));
46 $this->validateTransactionValue($value);
49 final public function validateTransactionValue($value) {
50 $value = phutil_string_cast($value);
51 if (!strlen($value)) {
52 return;
55 $options = $this->getSelectOptions();
57 if (!isset($options[$value])) {
58 throw new Exception(
59 pht(
60 'Value "%s" is not valid for setting "%s": valid values are %s.',
61 $value,
62 $this->getSettingName(),
63 implode(', ', array_keys($options))));
66 return;
69 public function getTransactionNewValue($value) {
70 $value = phutil_string_cast($value);
72 if (!strlen($value)) {
73 return null;
76 return $value;