1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
<?php
namespace Catapult;
class BaMLVerb extends BaMLGeneric {
public static $valid = array(
BAML_VERBS::BAML_SPEAK_SENTENCE,
BAML_VERBS::BAML_PLAY_AUDIO,
BAML_VERBS::BAML_TRANSFER,
BAML_VERBS::BAML_GATHER,
BAML_VERBS::BAML_RECORD,
BAML_VERBS::BAML_SEND_MESSAGE,
BAML_VERBS::BAML_REDIRECT,
BAML_VERBS::BAML_HANGUP,
);
public $constraints = array(
);
public static $params = array(
);
private $parsed = null;
private $name = "";
public $text = null;
public $verbs = array(
);
private $attributes = array(
);
public function __construct() {
$params = func_get_args();
$verb = preg_replace("/Verb/", "", preg_replace("/Catapult\\\BaML/", "", get_class($this)));
$this->name = $verb;
if (!(in_array($verb, self::$valid) || in_array("Verb" . $verb, self::$valid)))
Throw new \CatapultApiException($verb . " is not a valid verb in baML v" . BAML_SETTINGS::BAML_VERSION);
if ($params !== null) {
$cl = get_class($this);
foreach ($params as $cnt => $param) {
$this->addAttribute($cl::$params[$cnt], $param);
}
}
}
public function fromString($verb) {
$verb_class = "Catapult\\BaMLVerb" . $verb;
if (!(in_array($verb, self::$valid)))
Throw new \CatapultApiException($verb . " is not a valid verb in baML v" . BAML_SETTINGS::BAML_VERSION);
return new $verb_class;
}
public function __toString() {
$name = $this->name . "{";
foreach ($this->attributes as $attr) {
$name .= $attr->getKey() . "='" . $attr->getValue() . "',";
}
return substr($name, 0, strlen($name) - 1) . "}";
}
public function create($arg) {
$data = Ensure::Input($arg);
$args = $data->get();
foreach ($args as $k => $arg) {
if (is_string($arg)) {
$this->addAttribute($k, $arg);
}
if ($arg instanceof BaMLVerb) {
$this->addVerb($arg);
}
if ($arg instanceof BaMLAttribute) {
$this->addAttribute($arg);
}
if ($arg instanceof BaMLText) {
$this->addText($arg);
}
}
}
public function hasVerbs() {
if (count($this->verbs) > 0)
return TRUE;
return FALSE;
}
public function getVerbs() {
return $this->verbs;
}
public function getName() {
return $this->name;
}
public function getAttributes() {
return $this->attributes;
}
public function getAttributesString() {
$str = "";
foreach ($this->attributes as $attr) {
$str .= $attr->getKey() . "='" . $attr->getValue() . "',";
}
return substr($str, 0, strlen($str) - 1);
}
public function getText() {
return $this->text;
}
public function addVerb($verb) {
if (is_array($verb)) {
$verb = BaMLVerbResource::Make($verb);
}
if (!($verb instanceof BaMLVerb)) {
throw new \CatapultApiException("You can only add type BaMLVerb..");
}
if (isset($this->constraints['verbs']) && sizeof($this->verbs) >= $this->constraints['verbs']) {
throw new \CatapultApiException($this->getName() . " verb can only take " . $this->constraints['verbs'] . " verbs");
}
if (BaseUtilities::is_ref($verb, $this)) {
$this->verbs[] = clone $verb;
} else {
$this->verbs[] = $verb;
}
}
public function addNestedVerb($index, $verb) {
if (!($index <= sizeof($this->verbs))) {
throw new \CatapultApiException("This verb does not have $index nested verbs");
}
$this->verbs[$index]->addVerb($verb);
}
public function addNestedText($index, $text) {
if (!($index <= sizeof($this->verbs))) {
throw new \CatapultApiException("This verb does not have $index nested verbs");
}
$this->verbs[$index]->addText($text);
}
public function setNestedText($index, $text) {
if (!($index <= sizeof($this->verbs))) {
throw new \CatapultApiException("This verb does not have $index nested verbs");
}
$this->verbs[$index]->setText($text);
}
public function addNestedAttribute($index, $attribute) {
if (!($index <= sizeof($this->verbs))) {
throw new \CatapultApiException("This verb does not have $index nested verbs");
}
$this->verbs[$index]->addAttribute($attribute);
}
public function addNestedAttributes($index, $attribute) {
if (!($index <= sizeof($this->verbs))) {
throw new \CatapultApiException("This verb does not have $index nested verbs");
}
$this->verbs[$index]->addAttributes($attribute);
}
public function addAttribute($attribute) {
$args = func_get_args();
$cl = get_class($this);
if (is_array($attribute))
$attribute = new BaMLAttribute($attribute[0], $attribute[1]);
if (is_string($attribute))
$attribute = new BaMLAttribute($args[0], $args[1]);
if (!($attribute instanceof BaMLAttribute))
throw new \CatapultApiException("You can only add type BaMLAttribute or array..");
if (isset($cl::$params) && !(in_array($attribute->getKey(), $cl::$params)) && sizeof($cl::$params) > 0)
throw new \CatapultApiException("attribute '" . $attribute->getKey() . "' is not a valid attribute for verb " . $this->getName() . "" . " please use any of the following: " . $this->printAttributes());
$this->attributes[] = $attribute;
}
public function printAttributes() {
$cl = get_class($this);
$attrs = $cl::$params;
$str = "";
foreach ($attrs as $attr) {
$str.="$attr,";
}
return substr($str, 0, strlen($str) - 1);
}
public function addAttributes($attributes) {
foreach ($attributes as $attr) {
$this->addAttribute($attr);
}
}
public function addText($text) {
if (is_string($text))
$text = new BaMLText($text);
if (!($text instanceof BaMLText))
throw new \CatapultApiException("You can only add type BaMLAttribute or array..");
$this->text = $this->text . $text;
}
public function countVerbs() {
return sizeof($this->verbs);
}
public function getNestedVerb($index) {
return $this->verbs[$index];
}
public function getLevel() {
return $this->level;
}
public function getNestedNestedVerb($index) {
return $this->verbs[$index];
}
public function setText($text) {
if (is_string($text))
$text = new BaMLText($text);
if (!($text instanceof BaMLText))
throw new \CatapultApiException("You can only add type BaMLAttribute or array..");
$this->text = $text;
}
}