public function processRequest() { $request = $this->getRequest(); $user = $request->getUser(); $phid = head($request->getArr('macro')); $above = $request->getStr('above'); $below = $request->getStr('below'); $e_macro = true; $errors = array(); if ($request->isDialogFormPost()) { if (!$phid) { $e_macro = pht('Required'); $errors[] = pht('Macro name is required.'); } else { $macro = id(new PhabricatorMacroQuery())->setViewer($user)->withPHIDs(array($phid))->executeOne(); if (!$macro) { $e_macro = pht('Invalid'); $errors[] = pht('No such macro.'); } } if (!$errors) { $options = new PhutilSimpleOptions(); $data = array('src' => $macro->getName(), 'above' => $above, 'below' => $below); $string = $options->unparse($data, $escape = '}'); $result = array('text' => "{meme, {$string}}"); return id(new AphrontAjaxResponse())->setContent($result); } } $view = id(new AphrontFormView())->setUser($user)->appendControl(id(new AphrontFormTokenizerControl())->setLabel(pht('Macro'))->setName('macro')->setLimit(1)->setDatasource(new PhabricatorMacroDatasource())->setError($e_macro))->appendChild(id(new AphrontFormTextControl())->setLabel(pht('Above'))->setName('above')->setValue($above))->appendChild(id(new AphrontFormTextControl())->setLabel(pht('Below'))->setName('below')->setValue($below)); $dialog = id(new AphrontDialogView())->setUser($user)->setTitle(pht('Create Meme'))->appendForm($view)->addCancelButton('/')->addSubmitButton(pht('Llama Diorama')); return id(new AphrontDialogResponse())->setDialog($dialog); }
public function testSimpleOptionsUnparse() { $map = array('' => array(), 'legs=4' => array('legs' => '4'), 'legs=4, eyes=2' => array('legs' => '4', 'eyes' => '2'), 'eyes=2, legs=4' => array('eyes' => '2', 'legs' => '4'), 'legs=4, head' => array('legs' => '4', 'head' => true), 'eyes=2' => array('legs' => '', 'eyes' => '2')); foreach ($map as $expect => $dict) { $this->assertEqual($expect, PhutilSimpleOptions::unparse($dict), "Correct unparse of " . print_r($dict, true)); } $bogus = array(array('LEGS' => true), array('LEGS' => 4), array('!' => '!'), array('' => '2')); foreach ($bogus as $bad_input) { $caught = null; try { PhutilSimpleOptions::unparse($bad_input); } catch (Exception $ex) { $caught = $ex; } $this->assertEqual(true, $caught instanceof Exception, "Correct throw on unparse of bad input."); } }
public function testSimpleOptionsUnparse() { $map = array('' => array(), 'legs=4' => array('legs' => '4'), 'legs=4, eyes=2' => array('legs' => '4', 'eyes' => '2'), 'eyes=2, legs=4' => array('eyes' => '2', 'legs' => '4'), 'legs=4, head' => array('legs' => '4', 'head' => true), 'eyes=2' => array('legs' => '', 'eyes' => '2'), '"thousands separator"=","' => array('thousands separator' => ',')); foreach ($map as $expect => $dict) { $parser = new PhutilSimpleOptions(); $this->assertEqual($expect, $parser->unparse($dict), "Correct unparse of " . print_r($dict, true)); } $bogus = array(array('' => ''), array('' => 'x')); foreach ($bogus as $bad_input) { $caught = null; try { $parser = new PhutilSimpleOptions(); $parser->unparse($bad_input); } catch (Exception $ex) { $caught = $ex; } $this->assertTrue($caught instanceof Exception, "Correct throw on unparse of bad input."); } $parser = new PhutilSimpleOptions(); $this->assertEqual('a="\\}"', $parser->unparse(array('a' => '}'), '}'), "Unparse with extra escape."); }