Differential D20161 Diff 48140 src/applications/transactions/editengine/PhabricatorEditEngineSubtype.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/transactions/editengine/PhabricatorEditEngineSubtype.php
| <?php | <?php | ||||
| final class PhabricatorEditEngineSubtype | final class PhabricatorEditEngineSubtype | ||||
| extends Phobject { | extends Phobject { | ||||
| const SUBTYPE_DEFAULT = 'default'; | const SUBTYPE_DEFAULT = 'default'; | ||||
| private $key; | private $key; | ||||
| private $name; | private $name; | ||||
| private $icon; | private $icon; | ||||
| private $tagText; | private $tagText; | ||||
| private $color; | private $color; | ||||
| private $childSubtypes = array(); | private $childSubtypes = array(); | ||||
| private $childIdentifiers = array(); | private $childIdentifiers = array(); | ||||
| private $fieldConfiguration = array(); | |||||
| public function setKey($key) { | public function setKey($key) { | ||||
| $this->key = $key; | $this->key = $key; | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| public function getKey() { | public function getKey() { | ||||
| return $this->key; | return $this->key; | ||||
| ▲ Show 20 Lines • Show All 65 Lines • ▼ Show 20 Lines | public function newTagView() { | ||||
| $color = $this->getColor(); | $color = $this->getColor(); | ||||
| if ($color) { | if ($color) { | ||||
| $view->setColor($color); | $view->setColor($color); | ||||
| } | } | ||||
| return $view; | return $view; | ||||
| } | } | ||||
| public function setSubtypeFieldConfiguration( | |||||
| $subtype_key, | |||||
| array $configuration) { | |||||
| $this->fieldConfiguration[$subtype_key] = $configuration; | |||||
| return $this; | |||||
| } | |||||
| public function getSubtypeFieldConfiguration($subtype_key) { | |||||
| return idx($this->fieldConfiguration, $subtype_key); | |||||
| } | |||||
| public static function validateSubtypeKey($subtype) { | public static function validateSubtypeKey($subtype) { | ||||
| if (strlen($subtype) > 64) { | if (strlen($subtype) > 64) { | ||||
| throw new Exception( | throw new Exception( | ||||
| pht( | pht( | ||||
| 'Subtype "%s" is not valid: subtype keys must be no longer than '. | 'Subtype "%s" is not valid: subtype keys must be no longer than '. | ||||
| '64 bytes.', | '64 bytes.', | ||||
| $subtype)); | $subtype)); | ||||
| } | } | ||||
| Show All 29 Lines | foreach ($config as $value) { | ||||
| $value, | $value, | ||||
| array( | array( | ||||
| 'key' => 'string', | 'key' => 'string', | ||||
| 'name' => 'string', | 'name' => 'string', | ||||
| 'tag' => 'optional string', | 'tag' => 'optional string', | ||||
| 'color' => 'optional string', | 'color' => 'optional string', | ||||
| 'icon' => 'optional string', | 'icon' => 'optional string', | ||||
| 'children' => 'optional map<string, wild>', | 'children' => 'optional map<string, wild>', | ||||
| 'fields' => 'optional map<string, wild>', | |||||
| )); | )); | ||||
| $key = $value['key']; | $key = $value['key']; | ||||
| self::validateSubtypeKey($key); | self::validateSubtypeKey($key); | ||||
| if (isset($map[$key])) { | if (isset($map[$key])) { | ||||
| throw new Exception( | throw new Exception( | ||||
| pht( | pht( | ||||
| Show All 28 Lines | foreach ($config as $value) { | ||||
| if ($child_subtypes && $child_forms) { | if ($child_subtypes && $child_forms) { | ||||
| throw new Exception( | throw new Exception( | ||||
| pht( | pht( | ||||
| 'Subtype configuration is invalid: subtype with key "%s" '. | 'Subtype configuration is invalid: subtype with key "%s" '. | ||||
| 'specifies both child subtypes and child forms. Specify one '. | 'specifies both child subtypes and child forms. Specify one '. | ||||
| 'or the other, but not both.')); | 'or the other, but not both.')); | ||||
| } | } | ||||
| } | } | ||||
| $fields = idx($value, 'fields'); | |||||
| if ($fields) { | |||||
| foreach ($fields as $field_key => $configuration) { | |||||
| PhutilTypeSpec::checkMap( | |||||
| $configuration, | |||||
| array( | |||||
| 'disabled' => 'optional bool', | |||||
| 'name' => 'optional string', | |||||
| )); | |||||
| } | |||||
| } | |||||
| } | } | ||||
| if (!isset($map[self::SUBTYPE_DEFAULT])) { | if (!isset($map[self::SUBTYPE_DEFAULT])) { | ||||
| throw new Exception( | throw new Exception( | ||||
| pht( | pht( | ||||
| 'Subtype configuration is invalid: there is no subtype defined '. | 'Subtype configuration is invalid: there is no subtype defined '. | ||||
| 'with key "%s". This subtype is required and must be defined.', | 'with key "%s". This subtype is required and must be defined.', | ||||
| self::SUBTYPE_DEFAULT)); | self::SUBTYPE_DEFAULT)); | ||||
| Show All 34 Lines | foreach ($config as $entry) { | ||||
| if ($child_subtypes) { | if ($child_subtypes) { | ||||
| $subtype->setChildSubtypes($child_subtypes); | $subtype->setChildSubtypes($child_subtypes); | ||||
| } | } | ||||
| if ($child_forms) { | if ($child_forms) { | ||||
| $subtype->setChildFormIdentifiers($child_forms); | $subtype->setChildFormIdentifiers($child_forms); | ||||
| } | } | ||||
| $field_configurations = idx($entry, 'fields'); | |||||
| if ($field_configurations) { | |||||
| foreach ($field_configurations as $field_key => $field_configuration) { | |||||
| $subtype->setSubtypeFieldConfiguration( | |||||
| $field_key, | |||||
| $field_configuration); | |||||
| } | |||||
| } | |||||
| $map[$key] = $subtype; | $map[$key] = $subtype; | ||||
| } | } | ||||
| return new PhabricatorEditEngineSubtypeMap($map); | return new PhabricatorEditEngineSubtypeMap($map); | ||||
| } | } | ||||
| public function newIconView() { | public function newIconView() { | ||||
| return id(new PHUIIconView()) | return id(new PHUIIconView()) | ||||
| ->setIcon($this->getIcon(), $this->getColor()); | ->setIcon($this->getIcon(), $this->getColor()); | ||||
| } | } | ||||
| } | } | ||||