@ 2014-02-14T09:30:05Z <?php
class Maniple_Model
{
case CAMELIZE = 0;
case UNDERSCORE = 1;
/**
* @param array $data OPTIONAL
* @return void
*/
public function __construct(array $data = null) // {{{
{
if ($data) {
$this->setFromArray($data);
}
} // }}}
/**
* @param array $data
* @return Maniple_Model
*/
public function setFromArray(array $data) // {{{
{
foreach ($data as $key => $value) {
$this->__set($key, $value, false);
}
return $this;
} // }}}
/**
* @return array
*/
public function toArray($keyTransform = self::CAMELIZE)
{
$array = array();
foreach (get_object_vars($this) as $property) {
// include only properties starting with an underscore,
// remove the underscore before retrieving property value
if ('_' !== $property[0]) {
continue;
}
$property = substr($property, 1);
}
return $array;
}
/**
* @param string $key
* @param mixed $value
* @param bool $throw OPTIONAL
* @return Maniple_Model
* @throws InvalidArgumentException
*/
protected function _setProperty($key, $value, $throw = true) // {{{
{
$key = self::camelize($key);
$setter = 'set' . $key;
if (method_exists($this, $setter)) {
$this->{$setter}($value);
return $this;
}
$property = '_' . $key;
if (property_exists($this, $property)) {
$this->{$property} = $value;
return $this;
}
throw new InvalidArgumentException(sprintf('Invalid property: %s', $key));
return false;
} // }}}
/**
* @param string $key
* @param bool $throw OPTIONAL
* @return mixed
* @throws InvalidArgumentException
*/
protected function _getProperty($key, $throw = true) // {{{
{
$key = self::camelize($key);
$getter = 'get' . $key;
if (method_exists($this, $getter)) {
return $this->{$getter}();
}
$property = '_' . $key;
if (property_exists($this, $property)) {
return $this->{$property};
}
if ($throw) {
throw new InvalidArgumentException(sprintf('Invalid property: %s', $key));
}
} // }}}
/**
* @param string $key
* @return bool
*/
public function has($key) // {{{
{
return property_exists($this, '_' . self::camelize($key));
} // }}}
/**
* @param string $key
* @return bool
*/
public function __isset($key) // {{{
{
return isset($this->{'_' . self::camelize($key)});
} // }}}
/**
* @param string $key
* @return mixed
* @throws InvalidArgumentException
*/
public function __get($key) // {{{
{
return $this->_getProperty($key, true);
} // }}}
/**
* @param string $key
* @param mixed $value
* @return void
* @throws InvalidArgumentException
*/
public function __set($key, $value) // {{{
{
return $this->_setProperty($key, $value, true);
} // }}}
/**
* Transform given string to camel-case.
*
* @param string $str
* @return string
*/
public static function camelize($str) // {{{
{
if (is_array($str) && isset($str[1])) {
return strtoupper($str[1]);
}
return preg_replace_callback(
'/_(\w)/', array(__CLASS__, __FUNCTION__), (string) $str
);
} // }}}
/**
* Transform given string to underscore separated notation.
*
* @param string $str
* @return string
*/
public static function underscore($str)
{
if (is_array($str) && isset($str[1])) {
return substr($str[1], 0, 1) . '_' . substr($str[1], 1, 1);
}
return strtolower(preg_replace_callback(
'/([a-z][A-Z])/', array(__CLASS__, __FUNCTION__), (string) $str
));
}
/**
* @param string $string
* @param int $transform
* @return string
*/
public static function transform($string, $transform) // {{{
{
switch ($transform) {
case self::CAMELIZE:
$string = self::camelize($string);
break;
case self::UNDERSCORE:
$string = self::underscore($string);
break;
}
return (string) $string;
} // }}}
}
echo Maniple_Model::camelize('active_record'), "\n";
echo Maniple_Model::camelize('ActiveRecord'), "\n";
echo Maniple_Model::underscore('active_record'), "\n";
echo Maniple_Model::underscore('ActiveRecord'), "\n";
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 5.4.25 0.027 0.053 19.19 5.4.24 0.023 0.063 19.17 5.4.23 0.027 0.067 19.21 5.4.22 0.023 0.063 19.14 5.4.21 0.027 0.067 19.14 5.4.20 0.023 0.057 19.14 5.4.19 0.020 0.070 19.23 5.4.18 0.030 0.063 19.19 5.4.17 0.020 0.040 19.23 5.4.16 0.020 0.053 19.16 5.4.15 0.027 0.050 19.05 5.4.14 0.027 0.057 16.68 5.4.13 0.023 0.067 16.35 5.4.12 0.023 0.060 16.50 5.4.11 0.027 0.070 16.55 5.4.10 0.027 0.060 16.55 5.4.9 0.020 0.040 16.42 5.4.8 0.027 0.027 16.54 5.4.7 0.023 0.050 16.34 5.4.6 0.017 0.037 16.45 5.4.5 0.030 0.050 16.32 5.4.4 0.030 0.057 16.42 5.4.3 0.027 0.063 16.38 5.4.2 0.017 0.037 16.16 5.4.1 0.023 0.043 16.51 5.4.0 0.020 0.063 15.80 5.3.28 0.017 0.047 14.61 5.3.27 0.017 0.060 14.78 5.3.26 0.017 0.060 14.61 5.3.25 0.027 0.050 14.71 5.3.24 0.030 0.033 14.82 5.3.23 0.013 0.067 14.60 5.3.22 0.027 0.060 14.65 5.3.21 0.027 0.067 14.49 5.3.20 0.023 0.067 14.64 5.3.19 0.027 0.057 14.62 5.3.18 0.027 0.033 14.46 5.3.17 0.023 0.070 14.57 5.3.16 0.023 0.043 14.59 5.3.15 0.023 0.077 14.46 5.3.14 0.023 0.070 14.57 5.3.13 0.027 0.060 14.52 5.3.12 0.013 0.047 14.56 5.3.11 0.023 0.053 14.66 5.3.10 0.023 0.063 14.20 5.3.9 0.030 0.060 13.91 5.3.8 0.023 0.077 13.92 5.3.7 0.027 0.070 13.94 5.3.6 0.020 0.073 13.91 5.3.5 0.013 0.057 13.95 5.3.4 0.020 0.060 13.99 5.3.3 0.020 0.040 13.90 5.3.2 0.020 0.060 13.90 5.3.1 0.027 0.053 13.56 5.3.0 0.023 0.057 13.61
preferences:dark mode live preview
142.07 ms | 939 KiB | 8 Q