Generate file attachment transactions for explicit Remarkup attachments on common...
[phabricator.git] / src / applications / differential / field / DifferentialCommitMessageField.php
blobebe10bcf8800b645dc40f95ec3a1780b29b6e07f
1 <?php
3 abstract class DifferentialCommitMessageField
4 extends Phobject {
6 private $viewer;
7 private $customFieldStorage;
9 final public function setViewer(PhabricatorUser $viewer) {
10 $this->viewer = $viewer;
11 return $this;
14 final public function getViewer() {
15 return $this->viewer;
18 final public function setCustomFieldStorage(array $custom_field_storage) {
19 $this->customFieldStorage = $custom_field_storage;
20 return $this;
23 final public function getCustomFieldStorage() {
24 return $this->customFieldStorage;
27 abstract public function getFieldName();
28 abstract public function getFieldOrder();
30 public function isFieldEnabled() {
31 return true;
34 public function getFieldAliases() {
35 return array();
38 public function validateFieldValue($value) {
39 return;
42 public function parseFieldValue($value) {
43 return $value;
46 public function isFieldEditable() {
47 return true;
50 public function isTemplateField() {
51 return true;
54 public function readFieldValueFromConduit($value) {
55 return $this->readStringFieldValueFromConduit($value);
58 public function readFieldValueFromObject(DifferentialRevision $revision) {
59 return null;
62 public function renderFieldValue($value) {
63 if (!strlen($value)) {
64 return null;
67 return $value;
70 public function getFieldTransactions($value) {
71 if (!$this->isFieldEditable()) {
72 return array();
74 throw new PhutilMethodNotImplementedException();
77 final public function getCommitMessageFieldKey() {
78 return $this->getPhobjectClassConstant('FIELDKEY', 64);
81 final public static function newEnabledFields(PhabricatorUser $viewer) {
82 $fields = self::getAllFields();
84 $results = array();
85 foreach ($fields as $key => $field) {
86 $field = clone $field;
87 $field->setViewer($viewer);
88 if ($field->isFieldEnabled()) {
89 $results[$key] = $field;
93 return $results;
96 final public static function getAllFields() {
97 return id(new PhutilClassMapQuery())
98 ->setAncestorClass(__CLASS__)
99 ->setUniqueMethod('getCommitMessageFieldKey')
100 ->setSortMethod('getFieldOrder')
101 ->execute();
104 protected function raiseParseException($message) {
105 throw new DifferentialFieldParseException($message);
108 protected function raiseValidationException($message) {
109 throw new DifferentialFieldValidationException($message);
112 protected function parseObjectList(
113 $value,
114 array $types,
115 $allow_partial = false,
116 array $suffixes = array()) {
117 return id(new PhabricatorObjectListQuery())
118 ->setViewer($this->getViewer())
119 ->setAllowedTypes($types)
120 ->setObjectList($value)
121 ->setAllowPartialResults($allow_partial)
122 ->setSuffixes($suffixes)
123 ->execute();
126 protected function renderHandleList(array $phids, array $suffixes = array()) {
127 if (!$phids) {
128 return null;
131 $handles = $this->getViewer()->loadHandles($phids);
133 $out = array();
134 foreach ($handles as $handle) {
135 $phid = $handle->getPHID();
137 if ($handle->getPolicyFiltered()) {
138 $token = $phid;
139 } else if ($handle->isComplete()) {
140 $token = $handle->getCommandLineObjectName();
143 $suffix = idx($suffixes, $phid);
144 $token = $token.$suffix;
146 $out[] = $token;
149 return implode(', ', $out);
152 protected function readStringFieldValueFromConduit($value) {
153 if ($value === null) {
154 return $value;
157 if (!is_string($value)) {
158 throw new Exception(
159 pht(
160 'Field "%s" expects a string value, but received a value of type '.
161 '"%s".',
162 $this->getCommitMessageFieldKey(),
163 gettype($value)));
166 return $value;
169 protected function readStringListFieldValueFromConduit($value) {
170 if (!is_array($value)) {
171 throw new Exception(
172 pht(
173 'Field "%s" expects a list of strings, but received a value of type '.
174 '"%s".',
175 $this->getCommitMessageFieldKey(),
176 gettype($value)));
179 return $value;
182 protected function isCustomFieldEnabled($key) {
183 $field_list = PhabricatorCustomField::getObjectFields(
184 new DifferentialRevision(),
185 DifferentialCustomField::ROLE_DEFAULT);
187 $fields = $field_list->getFields();
188 return isset($fields[$key]);