Commit 7c2e53a7 by seldaek

BC Break: Dwoo::isArray had to be fixed and it has been split up in 3 methods,…

BC Break: Dwoo::isArray had to be fixed and it has been split up in 3 methods, isArray (for array access), isTraversable (for foreach) and count (just a helper that counts anything). It won't affect you unless you built some plugin depending on isArray, in which case you should check all works fine still fixes #37 git-svn-id: svn://dwoo.org/dwoo/trunk@284 0598d79b-80c4-4d41-97ba-ac86fbbd088b
parent 9b4b8364
[2009--] 1.2.0
! BC Break: Dwoo::isArray had to be fixed and it has been split up in 3
methods, isArray (for array access), isTraversable (for foreach) and
count (just a helper that counts anything). It won't affect you unless
you built some plugin depending on isArray, in which case you should
check all works fine still
[2009--] 1.1.1
+ Added Dwoo::setTemplate() for testing purposes mostly
* Fixed a security issue, if you didn't use a custom compiler factory but
......
......@@ -354,7 +354,6 @@ class Dwoo
}
} else {
// no cache present
if ($doCache === true) {
$dynamicId = uniqid();
}
......@@ -875,42 +874,76 @@ class Dwoo
}
/**
* [util function] checks if the input is an array or an iterator object, optionally it can also check if it's empty
* [util function] checks if the input is an array or arrayaccess object, optionally it can also check if it's empty
*
* @param mixed $value the variable to check
* @param bool $checkIsEmpty if true, the function will also check if the array is empty,
* @param bool $checkIsEmpty if true, the function will also check if the array|arrayaccess is empty,
* and return true only if it's not empty
* @return bool true if it's an array (and not empty) or false if it's not an array (or if it's empty)
* @return int|bool true if it's an array|arrayaccess (or the item count if $checkIsEmpty is true) or false if it's not an array|arrayaccess (or 0 if $checkIsEmpty is true)
*/
public function isArray($value, $checkIsEmpty=false)
{
if (is_array($value) === true) {
if (is_array($value) === true || $value instanceof ArrayAccess) {
if ($checkIsEmpty === false) {
return true;
} else {
return count($value) > 0;
return $this->count($value);
}
} elseif ($value instanceof Iterator) {
}
}
/**
* [util function] checks if the input is an array or a traversable object, optionally it can also check if it's empty
*
* @param mixed $value the variable to check
* @param bool $checkIsEmpty if true, the function will also check if the array|traversable is empty,
* and return true only if it's not empty
* @return int|bool true if it's an array|traversable (or the item count if $checkIsEmpty is true) or false if it's not an array|traversable (or 0 if $checkIsEmpty is true)
*/
public function isTraversable($value, $checkIsEmpty=false)
{
if (is_array($value) === true) {
if ($checkIsEmpty === false) {
return true;
} elseif ($value instanceof Countable) {
return count($value) > 0;
} else {
$value->rewind();
return $value->valid();
return count($value) > 0;
}
} elseif ($value instanceof ArrayAccess) {
} elseif ($value instanceof Traversable) {
if ($checkIsEmpty === false) {
return true;
} elseif ($value instanceof Countable) {
return count($value) > 0;
} else {
return $value->offsetExists(0);
return $this->count($value);
}
}
return false;
}
/**
* [util function] counts an array or arrayaccess/traversable object
* @param mixed $value
* @return int|bool the count for arrays and objects that implement countable, true for other objects that don't, and 0 for empty elements
*/
public function count($value)
{
if (is_array($value) === true || $value instanceof Countable) {
return count($value);
} elseif ($value instanceof ArrayAccess) {
if ($value->offsetExists(0)) {
return true;
}
} elseif ($value instanceof Iterator) {
$value->rewind();
if ($value->valid()) {
return true;
}
} elseif ($value instanceof Traversable) {
foreach ($value as $dummy) {
return true;
}
}
return 0;
}
/**
* [util function] triggers a dwoo error
*
......
......@@ -71,7 +71,7 @@ class Dwoo_Plugin_for extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Bloc
"\n".'$_for'.$cnt.'_step = abs('.$step.');'.
"\n".'if (is_numeric($_for'.$cnt.'_from) && !is_numeric($_for'.$cnt.'_to)) { $this->triggerError(\'For requires the <em>to</em> parameter when using a numerical <em>from</em>\'); }'.
"\n".'$tmp_shows = $this->isArray($_for'.$cnt.'_from, true) || (is_numeric($_for'.$cnt.'_from) && (abs(($_for'.$cnt.'_from - $_for'.$cnt.'_to)/$_for'.$cnt.'_step) !== 0 || $_for'.$cnt.'_from == $_for'.$cnt.'_to));';
// adds foreach properties
// adds for properties
if ($usesAny) {
$out .= "\n".'$this->globals["for"]['.$name.'] = array'."\n(";
if ($usesIndex) $out .="\n\t".'"index" => 0,';
......@@ -79,14 +79,14 @@ class Dwoo_Plugin_for extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Bloc
if ($usesFirst) $out .="\n\t".'"first" => null,';
if ($usesLast) $out .="\n\t".'"last" => null,';
if ($usesShow) $out .="\n\t".'"show" => $tmp_shows,';
if ($usesTotal) $out .="\n\t".'"total" => $this->isArray($_for'.$cnt.'_from) ? floor(count($_for'.$cnt.'_from) / $_for'.$cnt.'_step) : (is_numeric($_for'.$cnt.'_from) ? abs(($_for'.$cnt.'_to + 1 - $_for'.$cnt.'_from)/$_for'.$cnt.'_step) : 0),';
if ($usesTotal) $out .="\n\t".'"total" => $this->isArray($_for'.$cnt.'_from) ? floor($this->count($_for'.$cnt.'_from) / $_for'.$cnt.'_step) : (is_numeric($_for'.$cnt.'_from) ? abs(($_for'.$cnt.'_to + 1 - $_for'.$cnt.'_from)/$_for'.$cnt.'_step) : 0),';
$out.="\n);\n".'$_for'.$cnt.'_glob =& $this->globals["for"]['.$name.'];';
}
// checks if for must be looped
$out .= "\n".'if ($tmp_shows)'."\n{";
// set from/to to correct values if an array was given
$out .= "\n\t".'if ($this->isArray($_for'.$cnt.'_from, true)) {
$_for'.$cnt.'_to = is_numeric($_for'.$cnt.'_to) ? $_for'.$cnt.'_to - $_for'.$cnt.'_step : count($_for'.$cnt.'_from) - 1;
$out .= "\n\t".'if ($this->isArray($_for'.$cnt.'_from'.(isset($params['hasElse']) ? ', true' : '').') == true) {
$_for'.$cnt.'_to = is_numeric($_for'.$cnt.'_to) ? $_for'.$cnt.'_to - $_for'.$cnt.'_step : $this->count($_for'.$cnt.'_from) - 1;
$_for'.$cnt.'_from = 0;
}';
......@@ -114,8 +114,7 @@ class Dwoo_Plugin_for extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Bloc
}';
}
$out .= '
for ($this->scope['.$name.'] = $_for'.$cnt.'_from; $this->scope['.$name.'] '.$condition.' $_for'.$cnt.'_to; $this->scope['.$name.'] '.$incrementer.'= $_for'.$cnt.'_step)'."\n\t{";
$out .= "\n\t".'for ($this->scope['.$name.'] = $_for'.$cnt.'_from; $this->scope['.$name.'] '.$condition.' $_for'.$cnt.'_to; $this->scope['.$name.'] '.$incrementer.'= $_for'.$cnt.'_step)'."\n\t{";
// updates properties
if ($usesIndex) {
$out .="\n\t\t".'$_for'.$cnt.'_glob["index"] = $this->scope['.$name.'];';
......
......@@ -110,11 +110,11 @@ class Dwoo_Plugin_foreach extends Dwoo_Block_Plugin implements Dwoo_ICompilable_
if ($usesFirst) $pre .="\n\t".'"first" => null,';
if ($usesLast) $pre .="\n\t".'"last" => null,';
if ($usesShow) $pre .="\n\t".'"show" => $this->isArray($_fh'.$cnt.'_data, true),';
if ($usesTotal) $pre .="\n\t".'"total" => $this->isArray($_fh'.$cnt.'_data) ? count($_fh'.$cnt.'_data) : 0,';
if ($usesTotal) $pre .="\n\t".'"total" => $this->count($_fh'.$cnt.'_data),';
$pre.="\n);\n".'$_fh'.$cnt.'_glob =& $this->globals["foreach"]['.$name.'];';
}
// checks if foreach must be looped
$pre .= "\n".'if ($this->isArray($_fh'.$cnt.'_data'.(isset($params['hasElse']) ? ', true' : '').') === true)'."\n{";
$pre .= "\n".'if ($this->isTraversable($_fh'.$cnt.'_data'.(isset($params['hasElse']) ? ', true' : '').') == true)'."\n{";
// iterates over keys
$pre .= "\n\t".'foreach ($_fh'.$cnt.'_data as '.(isset($key)?'$this->scope['.$key.']=>':'').'$this->scope['.$val.'])'."\n\t{";
// updates properties
......
......@@ -91,12 +91,12 @@ class Dwoo_Plugin_loop extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Blo
if ($usesIteration) $pre .="\n\t".'"iteration" => 1,';
if ($usesFirst) $pre .="\n\t".'"first" => null,';
if ($usesLast) $pre .="\n\t".'"last" => null,';
if ($usesShow) $pre .="\n\t".'"show" => $this->isArray($_loop'.$cnt.'_data, true),';
if ($usesTotal) $pre .="\n\t".'"total" => $this->isArray($_loop'.$cnt.'_data) ? count($_loop'.$cnt.'_data) : 0,';
if ($usesShow) $pre .="\n\t".'"show" => $this->isTraversable($_loop'.$cnt.'_data, true),';
if ($usesTotal) $pre .="\n\t".'"total" => $this->count($_loop'.$cnt.'_data),';
$pre.="\n);\n".'$_loop'.$cnt.'_glob =& $this->globals["loop"]['.$name.'];';
}
// checks if the loop must be looped
$pre .= "\n".'if ($this->isArray($_loop'.$cnt.'_data'.(isset($params['hasElse']) ? ', true' : '').') === true)'."\n{";
$pre .= "\n".'if ($this->isTraversable($_loop'.$cnt.'_data'.(isset($params['hasElse']) ? ', true' : '').') == true)'."\n{";
// iterates over keys
$pre .= "\n\t".'foreach ($_loop'.$cnt.'_data as $tmp_key => $this->scope["-loop-"])'."\n\t{";
// updates properties
......
......@@ -92,7 +92,7 @@ class Dwoo_Plugin_section extends Dwoo_Block_Plugin implements Dwoo_ICompilable_
if ($usesFirst) $output .="\n\t".'"first" => null,';
if ($usesLast) $output .="\n\t".'"last" => null,';
if ($usesShow) $output .="\n\t".'"show" => ($this->isArray($_for'.$cnt.'_from, true)) || (is_numeric($_for'.$cnt.'_from) && $_for'.$cnt.'_from != $_for'.$cnt.'_to),';
if ($usesTotal) $output .="\n\t".'"total" => $this->isArray($_for'.$cnt.'_from) ? count($_for'.$cnt.'_from) - $_for'.$cnt.'_skip : (is_numeric($_for'.$cnt.'_from) ? abs(($_for'.$cnt.'_to + 1 - $_for'.$cnt.'_from)/$_for'.$cnt.'_step) : 0),';
if ($usesTotal) $output .="\n\t".'"total" => $this->isArray($_for'.$cnt.'_from) ? $this->count($_for'.$cnt.'_from) - $_for'.$cnt.'_skip : (is_numeric($_for'.$cnt.'_from) ? abs(($_for'.$cnt.'_to + 1 - $_for'.$cnt.'_from)/$_for'.$cnt.'_step) : 0),';
$out.="\n);\n".'$_section'.$cnt.'[\'glob\'] =& $this->globals["section"]['.$name.'];'."\n\n";
}
*/
......
......@@ -39,9 +39,6 @@ function Dwoo_Plugin_include(Dwoo $dwoo, $file, $cache_time = null, $cache_id =
}
try {
if (!is_numeric($cache_time)) {
$cache_time = null;
}
$include = $dwoo->templateFactory($resource, $identifier, $cache_time, $cache_id, $compile_id);
} catch (Dwoo_Security_Exception $e) {
return $dwoo->triggerError('Include : Security restriction : '.$e->getMessage(), E_USER_WARNING);
......@@ -55,12 +52,10 @@ function Dwoo_Plugin_include(Dwoo $dwoo, $file, $cache_time = null, $cache_id =
return $dwoo->triggerError('Include : Resource "'.$resource.'" does not support includes.', E_USER_WARNING);
}
if ($dwoo->isArray($data)) {
$vars = $data;
} elseif ($dwoo->isArray($cache_time)) {
$vars = $cache_time;
} else {
if (is_string($data)) {
$vars = $dwoo->readVar($data);
} else {
$vars = $data;
}
if (count($rest)) {
......
......@@ -304,54 +304,54 @@ class CoreTests extends PHPUnit_Framework_TestCase
$dwoo = new Dwoo();
$data = array();
$this->assertEquals(true, $dwoo->isArray($data));
$this->assertEquals(false, $dwoo->isArray($data, true));
$this->assertEquals(0, $dwoo->isArray($data, true));
$data = array('moo');
$this->assertEquals(true, $dwoo->isArray($data));
$this->assertEquals(true, $dwoo->isArray($data, true));
$this->assertEquals(1, $dwoo->isArray($data, true));
}
public function testIsArrayIterator()
public function testIsArrayArrayAccess()
{
$dwoo = new Dwoo();
$data = new TestIterator(array());
$data = new TestArrayAccess(array());
$this->assertEquals(true, $dwoo->isArray($data));
$this->assertEquals(false, $dwoo->isArray($data, true));
$data = new TestIterator(array('moo'));
$this->assertEquals(0, $dwoo->isArray($data, true));
$data = new TestArrayAccess(array('moo'));
$this->assertEquals(true, $dwoo->isArray($data));
$this->assertEquals(true, $dwoo->isArray($data, true));
}
public function testIsArrayCountableIterator()
public function testIsArrayCountableArrayAccess()
{
$dwoo = new Dwoo();
$data = new TestCountableIterator(array());
$data = new TestCountableArrayAccess(array());
$this->assertEquals(true, $dwoo->isArray($data));
$this->assertEquals(false, $dwoo->isArray($data, true));
$data = new TestCountableIterator(array('moo'));
$this->assertEquals(0, $dwoo->isArray($data, true));
$data = new TestCountableArrayAccess(array('moo'));
$this->assertEquals(true, $dwoo->isArray($data));
$this->assertEquals(true, $dwoo->isArray($data, true));
$this->assertEquals(1, $dwoo->isArray($data, true));
}
public function testIsArrayArrayAccess()
public function testIsTraversableIterator()
{
$dwoo = new Dwoo();
$data = new TestArrayAccess(array());
$this->assertEquals(true, $dwoo->isArray($data));
$this->assertEquals(false, $dwoo->isArray($data, true));
$data = new TestArrayAccess(array('moo'));
$this->assertEquals(true, $dwoo->isArray($data));
$this->assertEquals(true, $dwoo->isArray($data, true));
$data = new TestIterator(array());
$this->assertEquals(true, $dwoo->isTraversable($data));
$this->assertEquals(0, $dwoo->isTraversable($data, true));
$data = new TestIterator(array('moo'));
$this->assertEquals(true, $dwoo->isTraversable($data));
$this->assertEquals(true, $dwoo->isTraversable($data, true));
}
public function testIsArrayCountableArrayAccess()
public function testIsTraversableCountableIterator()
{
$dwoo = new Dwoo();
$data = new TestCountableArrayAccess(array());
$this->assertEquals(true, $dwoo->isArray($data));
$this->assertEquals(false, $dwoo->isArray($data, true));
$data = new TestCountableArrayAccess(array('moo'));
$this->assertEquals(true, $dwoo->isArray($data));
$this->assertEquals(true, $dwoo->isArray($data, true));
$data = new TestCountableIterator(array());
$this->assertEquals(true, $dwoo->isTraversable($data));
$this->assertEquals(0, $dwoo->isTraversable($data, true));
$data = new TestCountableIterator(array('moo'));
$this->assertEquals(true, $dwoo->isTraversable($data));
$this->assertEquals(1, $dwoo->isTraversable($data, true));
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment