Skip to content

Instantly share code, notes, and snippets.

@jonathantneal
Created April 15, 2014 00:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathantneal/10691312 to your computer and use it in GitHub Desktop.
Save jonathantneal/10691312 to your computer and use it in GitHub Desktop.
Javascript-like PHP syntax
<?php
class jObject {
// Private
private $__ = null;
function __construct() {
call_user_func_array(array($this, '__init'), func_get_args());
}
function __init() {
$this->__ = func_get_args();
}
function __toString() {
return '[object '.get_class($this).']';
}
}
function jObject() {
return new jObject();
}
class jArray extends jObject {
// Private
private $__ = array();
function __init() {
call_user_func_array(array($this, 'push'), func_get_args());
}
function __toString () {
return implode(',', $this->__);
}
function __get($property) {
if ($property === 'length') return count($this->__);
if ($property > -1) return $this->__[$property];
return $this->$property;
}
function __set($property, $value) {
if ($property > -1) return $this->__[$property] = $value;
return $this->$property = $value;
}
// Public
function concat() {
return call_user_func_array(array($this, '__construct'), array_merge($this->__, func_get_args()));
}
function each($callback) {
foreach ($this->__ as $index => $value) call_user_func_array($callback, array($value, $index, &$this->__));
return $this;
}
function filter($callback) {
$array = new self;
foreach ($this->__ as $index => $value) {
if (call_user_func_array($callback, array($value, $index, &$this->__))) {
$array->push($value);
}
}
return $array;
}
function indexOf($element, $fromIndex = 0) {
foreach ($this->__ as $index => $each) if ($index >= $fromIndex && $each == $element) return $index;
return -1;
}
function join($separator = ',') {
return implode($separator, $this->__);
}
function map($callback) {
$array = new self;
foreach ($this->__ as $index => $value) {
$array->push(call_user_func_array($callback, array($value, $index, &$this->__)));
}
return $array;
}
function pop() {
return array_splice($this->__, -1, 1, func_get_args())[0];
}
function push() {
$args = func_get_args();
foreach ($args as &$arg) {
if (is_string($arg)) {
array_push($this->__, new jString($arg));
} elseif (is_array($arg)) {
$array = new jArray();
call_user_func_array(array($array, 'push'), $arg);
array_push($this->__, $array);
} else {
array_push($this->__, $arg);
}
}
}
function shift() {
return array_splice($this->__, 0, 1, func_get_args())[0];
}
function slice() {
$array = new self;
$args = array_replace(array(0, count($this->__)), func_get_args());
$index = array_shift($args);
$end = array_shift($args);
call_user_func_array(array($array, 'push'), array_slice($this->__, $index, $end - $index));
return $array;
}
function sort() {
if (func_get_arg(0)) usort($this->__, func_get_arg(0));
else asort($this->__);
return $this;
}
function splice() {
$array = new self;
$args = array_replace(array(0, 0), func_get_args());
$index = array_shift($args);
$length = array_shift($args);
call_user_func_array(array($array, 'push'), array_splice($this->__, $index, $length, $args));
return $array;
}
function unshift() {
array_splice($this->__, 0, 0, func_get_args());
return $this->length;
}
}
function jArray() {
$array = new jArray();
return $array->concat(func_get_args());
}
class jString extends jObject {
// Private
private $__ = null;
private $__regex = '/^\/(.*)\/([gi]?)$/';
private $__regex_global = '/\/g$/';
function __init() {
$this->__ = preg_replace('/([^\\\\])\\\\n/', '$1'.PHP_EOL, (string) func_get_arg(0));
}
function __toString () {
return $this->__;
}
function __get($property) {
if ($property === 'length') return strlen($this->__);
if ($property > -1) return $this->charAt($property);
return $this->$property;
}
// Public
function charAt($index = 0) {
return new self($this->__{$index});
}
function concat() {
return new self(implode('', array_merge(array($this->__), func_get_args())));
}
function indexOf($needle = '') {
return strpos($this->__, $needle);
}
function lastIndexOf($needle = '') {
return strrpos($this->__, $needle);
}
function match($regexp = '') {
$isGlobal = preg_match($this->__regex_global, $regexp);
$hasMatch = $isGlobal ? preg_match_all(substr($regexp, 0, -1), $this->__, $matches, PREG_PATTERN_ORDER) : preg_match($regexp, $this->__, $matches);
if ($hasMatch) {
$array = new jArray();
call_user_func_array(array($array, 'push'), $isGlobal ? $matches[0] : $matches);
return $array;
}
}
function replace($substr = '', $newsubstr = '') {
if (is_callable($newsubstr)) return new self(preg_replace_callback($substr, function ($matches) use ($newsubstr) {
foreach ($matches as &$match) $match = new self($match);
return call_user_func_array($newsubstr, $matches);
}, $this->__));
if ($substr{0} !== '/') return new self(preg_replace($substr, $newsubstr, $this->__));
return new self(str_replace($substr, $newsubstr, $this->__));
}
function search($regexp = '//') {
$hasMatch = preg_match($regexp, $this->__, $match, PREG_OFFSET_CAPTURE);
return $hasMatch ? $match[0][1] : -1;
}
function slice() {
$args = array_replace(array(0, strlen($this->__)), func_get_args());
$index = array_shift($args);
$length = array_shift($args);
return new self(substr($this->__, $index, $length));
}
function split($separater = '') {
$array = new jArray();
call_user_func_array(array($array, 'push'), preg_match($this->__regex, $separater) ? preg_split($separater, $this->__) : explode($separater, $this->__));
return $array;
}
function substr() {
$args = array_replace(array(0, strlen($this->__)), func_get_args());
$index = array_shift($args);
$length = array_shift($args);
return new self(substr($this->__, $index, $length));
}
function toLowerCase() {
return new self(strtolower($this->__));
}
function toUpperCase() {
return new self(strtoupper($this->__));
}
function trim() {
return new self(trim($this->__));
}
function trimLeft() {
return new self(ltrim($this->__));
}
function trimRight() {
return new self(rtrim($this->__));
}
}
function jString($string = '') {
return new jString($string);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment