-
-
Save chandlerprall/853162 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Not truely a visitor pattern as no accept() methods are being implemented; in this case | |
* there's just no need to complicate things | |
*/ | |
interface VisitorPattern | |
{ | |
static public function visit(&$element); | |
} | |
/** | |
* Takes one or many argument(s) | |
*/ | |
class JSArgumentsVisitor implements VisitorPattern { | |
static public function visit(&$element) | |
{ | |
if (is_array($element)) | |
{ | |
foreach ($element as &$value) | |
{ | |
JSArgumentsVisitor::visit($value); | |
} | |
unset($value); | |
} else { | |
if (!is_numeric($element)) | |
{ | |
$element = (string) $element; | |
} | |
} | |
} | |
} | |
class YuiMethod | |
{ | |
protected $selector; | |
protected $helpers; | |
protected $code; | |
public function __construct($selector, $helper = null) | |
{ | |
$this->selector = $selector; | |
$this->helper = $helper; | |
$this->methods = new JavaScript_Method_Chain(); | |
$this->__call('ui', array($selector)); | |
} | |
public function plug($plugin, $config = array()) | |
{ | |
return $this->__call('plug', array($plugin, $config)); | |
} | |
public function __call($method, $args) | |
{ | |
$method = new JavaScript_Method($method); | |
$method->setArguments($args); | |
$this->methods->add($method); | |
return $this; | |
} | |
public function __toString() | |
{ | |
return (string) $this->methods; | |
} | |
} | |
class JavaScript_Collection extends ArrayIterator | |
{ | |
protected $methods = array(); | |
protected $separator = "."; | |
public function __toString() | |
{ | |
return implode($this->separator, iterator_to_array($this)); | |
} | |
} | |
class JavaScript_Methods extends JavaScript_Collection | |
{ | |
protected $separator = ";"; | |
public function add(JavaScript_Collection $chain) | |
{ | |
$this->append($chain); | |
} | |
} | |
class JavaScript_Method_Chain extends JavaScript_Collection | |
{ | |
public function add(JavaScript_Method $method) | |
{ | |
$this->append($method); | |
} | |
} | |
class JavaScript_Arguments extends JavaScript_Collection | |
{ | |
protected $separator = ", "; | |
public function current() | |
{ | |
$item = parent::current(); | |
if (!$item instanceof JavaScript_Argument) { | |
$item = new JavaScript_Argument($item); | |
$this->offsetSet($this->key(), $item); | |
} | |
return $item; | |
} | |
} | |
class JavaScript_Argument | |
{ | |
protected $value; | |
public function __construct($value) | |
{ | |
$this->value = $value; | |
} | |
public function __toString() | |
{ | |
$arg = $this->value; | |
JSArgumentsVisitor::visit($arg); | |
return json_encode($arg); | |
} | |
} | |
class JavaScript_Method | |
{ | |
protected $name; | |
protected $args; | |
protected $useConstructor; | |
public function __construct($name, $args = array()) | |
{ | |
$this->name = $name; | |
$this->args = new JavaScript_Arguments($args); | |
} | |
public function enableConstructor() | |
{ | |
$this->useConstructor = true; | |
} | |
public function disableConstructor() | |
{ | |
$this->useConstructor = false; | |
} | |
public function setArguments($args) | |
{ | |
$this->args = new JavaScript_Arguments($args); | |
} | |
public function addArgument($arg) | |
{ | |
$this->args->append($arg); | |
} | |
public function setArgument($index, $arg) | |
{ | |
$this->args[$index] = $arg; | |
} | |
public function __toString() | |
{ | |
$method = sprintf('%s(%s)', $this->name, $this->args); | |
if (true === $this->useConstructor) { | |
$method = sprintf('(new %s)', $method); | |
} | |
return $method; | |
} | |
} | |
class JavaScript_Function | |
{ | |
protected $name; | |
protected $content; | |
protected $anonymous = false; | |
public function __construct($name, $content = null) | |
{ | |
if (is_array($name)) { | |
$this->anonymous = true; | |
$this->content = new JavaScript_Methods($name); | |
} else { | |
$this->name = $name; | |
$this->content = $content; | |
} | |
} | |
public function __toString() | |
{ | |
$code = (string) $this->content; | |
if (!$this->anonymous) { | |
return sprintf('function %s() { %s }', $this->name, $code); | |
} | |
return sprintf('function() { %s }', $code); | |
} | |
} | |
/* | |
$method = new JavaScript_Method('ui'); | |
$method->setArguments(array('#selector')); | |
echo $method . PHP_EOL; | |
$ui = new YuiMethod('#mydiv'); | |
echo $ui->plug('navbar', array('visible' => false))->render() . PHP_EOL; | |
*/ | |
$loader = new JavaScript_Method('YAHOO.util.YUILoader'); | |
$loader->enableConstructor(); | |
$loader->addArgument(array( | |
'require' => array('event','containercore'), | |
'onSuccess' => new JavaScript_Function(array( | |
new JavaScript_Method('alert', array('test')) | |
)) | |
)); | |
echo $loader . PHP_EOL; | |
/* | |
$func = new JavaScript_Function(array( | |
new JavaScript_Method('alert', array('test')) | |
)); | |
echo $func . PHP_EOL; | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment