Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/#6773-allow-false-values-in-priority-list' into …
Browse files Browse the repository at this point in the history
…'develop'

Close #6773
Close #6768
Close #6820
Close #6834
Close #6881
Forward port #6773
Forward port #6768
  • Loading branch information
Ocramius committed Nov 20, 2014
2 parents 29304bc + 8c6ff57 commit c2e67b8
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 47 deletions.
84 changes: 41 additions & 43 deletions library/Zend/Stdlib/PriorityList.php
Expand Up @@ -12,9 +12,6 @@
use Countable;
use Iterator;

/**
* Priority list
*/
class PriorityList implements Iterator, Countable
{
const EXTR_DATA = 0x00000001;
Expand All @@ -23,7 +20,7 @@ class PriorityList implements Iterator, Countable
/**
* Internal list of all items.
*
* @var array
* @var array[]
*/
protected $items = array();

Expand Down Expand Up @@ -58,8 +55,9 @@ class PriorityList implements Iterator, Countable
* Insert a new item.
*
* @param string $name
* @param mixed $value
* @param int $priority
* @param mixed $value
* @param int $priority
*
* @return void
*/
public function insert($name, $value, $priority = 0)
Expand All @@ -74,13 +72,23 @@ public function insert($name, $value, $priority = 0)
);
}

/**
* @param string $name
* @param int $priority
*
* @return $this
*
* @throws \Exception
*/
public function setPriority($name, $priority)
{
if (!isset($this->items[$name])) {
throw new \Exception("item $name not found");
}

$this->items[$name]['priority'] = (int) $priority;
$this->sorted = false;
$this->sorted = false;

return $this;
}

Expand All @@ -92,11 +100,10 @@ public function setPriority($name, $priority)
*/
public function remove($name)
{
if (!isset($this->items[$name])) {
return;
if (isset($this->items[$name])) {
$this->count--;
}

$this->count--;
unset($this->items[$name]);
}

Expand All @@ -107,7 +114,7 @@ public function remove($name)
*/
public function clear()
{
$this->items = array();
$this->items = array();
$this->serial = 0;
$this->count = 0;
$this->sorted = false;
Expand Down Expand Up @@ -158,25 +165,26 @@ protected function compare(array $item1, array $item2)
/**
* Get/Set serial order mode
*
* @param bool $flag
* @param bool|null $flag
*
* @return bool
*/
public function isLIFO($flag = null)
{
if ($flag !== null) {
if (($flag = ($flag === true ? 1 : -1)) !== $this->isLIFO) {
$this->isLIFO = $flag;
$isLifo = $flag === true ? 1 : -1;

if ($isLifo !== $this->isLIFO) {
$this->isLIFO = $isLifo;
$this->sorted = false;
}
}
return $this->isLIFO === 1;

return 1 === $this->isLIFO;
}

/**
* rewind(): defined by Iterator interface.
*
* @see Iterator::rewind()
* @return void
* {@inheritDoc}
*/
public function rewind()
{
Expand All @@ -185,23 +193,18 @@ public function rewind()
}

/**
* current(): defined by Iterator interface.
*
* @see Iterator::current()
* @return mixed
* {@inheritDoc}
*/
public function current()
{
$this->sorted || $this->sort();
$node = current($this->items);
return ($node !== false ? $node['data'] : false);

return $node ? $node['data'] : false;
}

/**
* key(): defined by Iterator interface.
*
* @see Iterator::key()
* @return string
* {@inheritDoc}
*/
public function key()
{
Expand All @@ -210,26 +213,21 @@ public function key()
}

/**
* next(): defined by Iterator interface.
*
* @see Iterator::next()
* @return mixed
* {@inheritDoc}
*/
public function next()
{
$node = next($this->items);
return ($node !== false ? $node['data'] : false);

return $node ? $node['data'] : false;
}

/**
* valid(): defined by Iterator interface.
*
* @see Iterator::valid()
* @return bool
* {@inheritDoc}
*/
public function valid()
{
return ($this->current() !== false);
return $this->current() !== false;
}

/**
Expand All @@ -241,10 +239,7 @@ public function getIterator()
}

/**
* count(): defined by Countable interface.
*
* @see Countable::count()
* @return int
* {@inheritDoc}
*/
public function count()
{
Expand All @@ -255,16 +250,19 @@ public function count()
* Return list as array
*
* @param int $flag
*
* @return array
*/
public function toArray($flag = self::EXTR_DATA)
{
$this->sort();

if ($flag == self::EXTR_BOTH) {
return $this->items;
}

return array_map(
($flag == self::EXTR_PRIORITY)
self::EXTR_PRIORITY === $flag
? function ($item) { return $item['priority']; }
: function ($item) { return $item['data']; },
$this->items
Expand Down
13 changes: 13 additions & 0 deletions tests/ZendTest/Db/Sql/UpdateTest.php
Expand Up @@ -234,6 +234,19 @@ public function testGetSqlString()
$this->assertEquals('UPDATE "sch"."foo" SET "bar" = \'baz\', "boo" = NOW(), "bam" = NULL WHERE x = y', $this->update->getSqlString(new TrustingSql92Platform()));
}

/**
* @group 6768
* @group 6773
*/
public function testGetSqlStringForFalseUpdateValueParameter()
{
$this->update = new Update;
$this->update->table(new TableIdentifier('foo', 'sch'))
->set(array('bar' => false, 'boo' => 'test', 'bam' => true))
->where('x = y');
$this->assertEquals('UPDATE "sch"."foo" SET "bar" = \'\', "boo" = \'test\', "bam" = \'1\' WHERE x = y', $this->update->getSqlString(new TrustingSql92Platform()));
}

/**
* @covers Zend\Db\Sql\Update::__get
*/
Expand Down
28 changes: 24 additions & 4 deletions tests/ZendTest/Stdlib/PriorityListTest.php
Expand Up @@ -83,7 +83,7 @@ public function testLIFOOnly()
$this->list->insert('foobar', new \stdClass());
$this->list->insert('barbaz', new \stdClass());

$order = array();
$orders = array();

foreach ($this->list as $key => $value) {
$orders[] = $key;
Expand Down Expand Up @@ -129,7 +129,7 @@ public function testFIFOWithPriority()
$this->list->insert('bar', new \stdClass(), 0);
$this->list->insert('baz', new \stdClass(), 1);

$order = array();
$orders = array();

foreach ($this->list as $key => $value) {
$orders[] = $key;
Expand All @@ -147,7 +147,7 @@ public function testFIFOOnly()
$this->list->insert('foobar', new \stdClass());
$this->list->insert('barbaz', new \stdClass());

$order = array();
$orders = array();

foreach ($this->list as $key => $value) {
$orders[] = $key;
Expand All @@ -162,7 +162,7 @@ public function testPriorityWithNegativesAndNull()
$this->list->insert('bar', new \stdClass(), 1);
$this->list->insert('baz', new \stdClass(), -1);

$order = array();
$orders = array();

foreach ($this->list as $key => $value) {
$orders[] = $key;
Expand Down Expand Up @@ -215,4 +215,24 @@ public function testToArray()
$this->list->toArray(PriorityList::EXTR_BOTH)
);
}

/**
* @group 6768
* @group 6773
*/
public function testBooleanValuesAreValid()
{
$this->list->insert('foo', false, null);
$this->list->insert('foo', null, null);
$this->list->insert('bar', 'test', 1);
$this->list->insert('baz', true, -1);

$orders = array();

foreach ($this->list as $key => $value) {
$orders[] = $value;
}

$this->assertEquals(array('test', false, true), $orders);
}
}

0 comments on commit c2e67b8

Please sign in to comment.