@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3final class PhabricatorChartFunctionArgument
4 extends Phobject {
5
6 private $name;
7 private $type;
8 private $repeatable;
9
10 public function setName($name) {
11 $this->name = $name;
12 return $this;
13 }
14
15 public function getName() {
16 return $this->name;
17 }
18
19 public function setRepeatable($repeatable) {
20 $this->repeatable = $repeatable;
21 return $this;
22 }
23
24 public function getRepeatable() {
25 return $this->repeatable;
26 }
27
28 public function setType($type) {
29 $types = array(
30 'fact-key' => true,
31 'function' => true,
32 'number' => true,
33 'phid' => true,
34 );
35
36 if (!isset($types[$type])) {
37 throw new Exception(
38 pht(
39 'Chart function argument type "%s" is unknown. Valid types '.
40 'are: %s.',
41 $type,
42 implode(', ', array_keys($types))));
43 }
44
45 $this->type = $type;
46 return $this;
47 }
48
49 public function getType() {
50 return $this->type;
51 }
52
53 public function newValue($value) {
54 switch ($this->getType()) {
55 case 'phid':
56 // TODO: This could be validated better, but probably should not be
57 // a primitive type.
58 return $value;
59 case 'fact-key':
60 if (!is_string($value)) {
61 throw new Exception(
62 pht(
63 'Value for "fact-key" argument must be a string, got %s.',
64 phutil_describe_type($value)));
65 }
66
67 $facts = PhabricatorFact::getAllFacts();
68 $fact = idx($facts, $value);
69 if (!$fact) {
70 throw new Exception(
71 pht(
72 'Fact key "%s" is not a known fact key.',
73 $value));
74 }
75
76 return $fact;
77 case 'function':
78 // If this is already a function object, just return it.
79 if ($value instanceof PhabricatorChartFunction) {
80 return $value;
81 }
82
83 if (!is_array($value)) {
84 throw new Exception(
85 pht(
86 'Value for "function" argument must be a function definition, '.
87 'formatted as a list, like: [fn, arg1, arg, ...]. Actual value '.
88 'is %s.',
89 phutil_describe_type($value)));
90 }
91
92 if (!phutil_is_natural_list($value)) {
93 throw new Exception(
94 pht(
95 'Value for "function" argument must be a natural list, not '.
96 'a dictionary. Actual value is "%s".',
97 phutil_describe_type($value)));
98 }
99
100 if (!$value) {
101 throw new Exception(
102 pht(
103 'Value for "function" argument must be a list with a function '.
104 'name; got an empty list.'));
105 }
106
107 $function_name = array_shift($value);
108
109 if (!is_string($function_name)) {
110 throw new Exception(
111 pht(
112 'Value for "function" argument must be a natural list '.
113 'beginning with a function name as a string. The first list '.
114 'item has the wrong type, %s.',
115 phutil_describe_type($function_name)));
116 }
117
118 $functions = PhabricatorChartFunction::getAllFunctions();
119 if (!isset($functions[$function_name])) {
120 throw new Exception(
121 pht(
122 'Function "%s" is unknown. Valid functions are: %s',
123 $function_name,
124 implode(', ', array_keys($functions))));
125 }
126
127 return id(clone $functions[$function_name])
128 ->setArguments($value);
129 case 'number':
130 if (!is_float($value) && !is_int($value)) {
131 throw new Exception(
132 pht(
133 'Value for "number" argument must be an integer or double, '.
134 'got %s.',
135 phutil_describe_type($value)));
136 }
137
138 return $value;
139 }
140
141 throw new PhutilMethodNotImplementedException();
142 }
143
144}