1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-11 08:06:13 +01:00
phorge-phorge/src/applications/herald/field/HeraldField.php
epriestley 59c294457f Modularize more Herald fields
Summary: Ref T8726. Continue making Herald fields more modular than they currently are.

Test Plan:
  - Created a rule using all the affected fields.
  - Ran the rule.
  - Saw reasonable object field values.

Reviewers: btrahan

Reviewed By: btrahan

Subscribers: eadler, joshuaspence, epriestley

Maniphest Tasks: T8726

Differential Revision: https://secure.phabricator.com/D13495
2015-07-06 13:15:47 -07:00

94 lines
2.4 KiB
PHP

<?php
abstract class HeraldField extends Phobject {
private $adapter;
const STANDARD_LIST = 'standard.list';
const STANDARD_BOOL = 'standard.bool';
const STANDARD_PHID = 'standard.phid';
abstract public function getHeraldFieldName();
abstract public function getHeraldFieldValue($object);
public function getHeraldFieldConditions() {
switch ($this->getHeraldFieldStandardConditions()) {
case self::STANDARD_LIST:
return array(
HeraldAdapter::CONDITION_INCLUDE_ALL,
HeraldAdapter::CONDITION_INCLUDE_ANY,
HeraldAdapter::CONDITION_INCLUDE_NONE,
HeraldAdapter::CONDITION_EXISTS,
HeraldAdapter::CONDITION_NOT_EXISTS,
);
case self::STANDARD_BOOL:
return array(
HeraldAdapter::CONDITION_IS_TRUE,
HeraldAdapter::CONDITION_IS_FALSE,
);
case self::STANDARD_PHID:
return array(
HeraldAdapter::CONDITION_IS_ANY,
HeraldAdapter::CONDITION_IS_NOT_ANY,
);
}
throw new Exception(pht('Unknown standard condition set.'));
}
protected function getHeraldFieldStandardConditions() {
throw new PhutilMethodNotImplementedException();
}
abstract public function getHeraldFieldValueType($condition);
abstract public function supportsObject($object);
public function getFieldsForObject($object) {
return array($this->getFieldConstant() => $this);
}
final public function setAdapter(HeraldAdapter $adapter) {
$this->adapter = $adapter;
return $this;
}
final public function getAdapter() {
return $this->adapter;
}
final public function getFieldConstant() {
$class = new ReflectionClass($this);
$const = $class->getConstant('FIELDCONST');
if ($const === false) {
throw new Exception(
pht(
'"%s" class "%s" must define a "%s" property.',
__CLASS__,
get_class($this),
'FIELDCONST'));
}
if (!is_string($const) || (strlen($const) > 32)) {
throw new Exception(
pht(
'"%s" class "%s" has an invalid "%s" property. Field constants '.
'must be strings and no more than 32 bytes in length.',
__CLASS__,
get_class($this),
'FIELDCONST'));
}
return $const;
}
final public static function getAllFields() {
return id(new PhutilClassMapQuery())
->setAncestorClass(__CLASS__)
->setUniqueMethod('getFieldConstant')
->execute();
}
}