Path: blob/master/src/applications/fact/chart/PhabricatorChartFunctionArgument.php
12256 views
<?php12final class PhabricatorChartFunctionArgument3extends Phobject {45private $name;6private $type;7private $repeatable;89public function setName($name) {10$this->name = $name;11return $this;12}1314public function getName() {15return $this->name;16}1718public function setRepeatable($repeatable) {19$this->repeatable = $repeatable;20return $this;21}2223public function getRepeatable() {24return $this->repeatable;25}2627public function setType($type) {28$types = array(29'fact-key' => true,30'function' => true,31'number' => true,32'phid' => true,33);3435if (!isset($types[$type])) {36throw new Exception(37pht(38'Chart function argument type "%s" is unknown. Valid types '.39'are: %s.',40$type,41implode(', ', array_keys($types))));42}4344$this->type = $type;45return $this;46}4748public function getType() {49return $this->type;50}5152public function newValue($value) {53switch ($this->getType()) {54case 'phid':55// TODO: This could be validated better, but probably should not be56// a primitive type.57return $value;58case 'fact-key':59if (!is_string($value)) {60throw new Exception(61pht(62'Value for "fact-key" argument must be a string, got %s.',63phutil_describe_type($value)));64}6566$facts = PhabricatorFact::getAllFacts();67$fact = idx($facts, $value);68if (!$fact) {69throw new Exception(70pht(71'Fact key "%s" is not a known fact key.',72$value));73}7475return $fact;76case 'function':77// If this is already a function object, just return it.78if ($value instanceof PhabricatorChartFunction) {79return $value;80}8182if (!is_array($value)) {83throw new Exception(84pht(85'Value for "function" argument must be a function definition, '.86'formatted as a list, like: [fn, arg1, arg, ...]. Actual value '.87'is %s.',88phutil_describe_type($value)));89}9091if (!phutil_is_natural_list($value)) {92throw new Exception(93pht(94'Value for "function" argument must be a natural list, not '.95'a dictionary. Actual value is "%s".',96phutil_describe_type($value)));97}9899if (!$value) {100throw new Exception(101pht(102'Value for "function" argument must be a list with a function '.103'name; got an empty list.'));104}105106$function_name = array_shift($value);107108if (!is_string($function_name)) {109throw new Exception(110pht(111'Value for "function" argument must be a natural list '.112'beginning with a function name as a string. The first list '.113'item has the wrong type, %s.',114phutil_describe_type($function_name)));115}116117$functions = PhabricatorChartFunction::getAllFunctions();118if (!isset($functions[$function_name])) {119throw new Exception(120pht(121'Function "%s" is unknown. Valid functions are: %s',122$function_name,123implode(', ', array_keys($functions))));124}125126return id(clone $functions[$function_name])127->setArguments($value);128case 'number':129if (!is_float($value) && !is_int($value)) {130throw new Exception(131pht(132'Value for "number" argument must be an integer or double, '.133'got %s.',134phutil_describe_type($value)));135}136137return $value;138}139140throw new PhutilMethodNotImplementedException();141}142143}144145146