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

Commit

Permalink
Merge branch 'hotfix/zendframework/zendframework#6773-allow-false-val…
Browse files Browse the repository at this point in the history
  • Loading branch information
Ocramius committed Nov 20, 2014
208 parents f8b53e6 + 623665c + d8f58fd + c8aca84 + 4ac816f + 7f2fed2 + b528fc5 + fb851e2 + 802bf45 + 29ea285 + c9418a6 + fdeb27b + c382628 + ee15e76 + 5f29760 + 714d1a8 + 84b0297 + cbc5f03 + 76361f8 + e7209df + 87630e0 + df43daf + f7d6cbb + 7e2b798 + 3ed1ead + 87505b6 + c229265 + eb61c8f + efcb00e + 0a0842f + b6d0c88 + 7edee62 + 60ea64c + a08bcca + b40ec3e + 63172ed + 448f428 + 92a516a + 5ecbc99 + a2df21b + 4de87f2 + 7c259ec + a22bdcb + 084ad9f + 9414e5a + 489be93 + cb39e7e + 54a28dc + c9c769e + dda791d + 70d382a + 8bbad0e + 9321185 + 7ab35a6 + b93694e + 3ea7087 + 0fe3d3a + bd5e189 + d1cba17 + 8d75392 + 3fb5b55 + 6cb0ccb + 30aa565 + 8409977 + 8074ba0 + 8f92486 + 94860d1 + 05d33c4 + 425826b + f0e91f0 + e31468f + 7d2af87 + 2e4dc80 + 19d128f + 1b9e4b2 + 1c46483 + fdda3f2 + 595fcd1 + 213395c + 8e514a8 + 2f30186 + bb4ed65 + 132d5b6 + 030ff33 + f2f20f3 + a50e133 + 4c554ee + dbfb1b8 + ccab83f + 00b350f + 78945d0 + f0e5f4b + ceb7d8c + 9e124d1 + 3de5912 + b6a974a + 10a6438 + cb6c1e7 + 18afd6c + 3baf1bd + c800904 + f52dcb8 + 126ccb2 + e7d6206 + e2d24ab + ec1abfc + 290ea90 + 9f4ca1b + edaa760 + c4c0c95 + d21f055 + 5b18029 + e6b97af + 010fb36 + 64c7b8d + 636523e + 4cc2cd6 + e34098a + 16367cd + 943c77f + 8226e5b + 0b47726 + 3cd8a03 + cc4782c + 9c653a6 + 656dbe5 + 9bce1ba + 7dc18ca + 861130d + 2d2ffbd + 4f413a5 + 2e1067a + 1d082e4 + e8aeb79 + b562091 + ff2fdc3 + 4aa72c0 + 1bb67ac + cd015c8 + 5e89910 + 0c21258 + dd54faf + 57f9063 + b88ce2e + af68643 + 06cd3b4 + 2c71b71 + ee02c35 + 9456314 + 5a77a7b + e98a077 + 738f2e6 + cb1e63c + 736df07 + d0a0154 + 990523c + 78687de + a5b6e79 + 6e9dfe9 + e201a1c + d9b45ef + 76222ad + 16d67da + 1ab2258 + b81d711 + ed2e9bc + 61efe82 + f353ea5 + 1f02519 + 58c1fe8 + ed502d9 + 2defba6 + 4885013 + 06a8384 + 17d9eed + 3b21b5d + c62101c + 909ef34 + 13d376a + 8a75367 + 98a3cf5 + 270f2c4 + 3038cfa + 1112202 + c8fb359 + 8d37cd0 + 4d868a7 + 555cb91 + 7ac5858 + 8103f1f + 9fe9c96 + a3ecac6 + 10e77c1 + e0d3e13 + 19ad608 + e0d665c + 1a5b402 + 56ae12a + 9073fc1 + 2d5b32f + a8e6198 + 83e8f34 + 30eb247 + 6204c2f + c3855a9 + 543aa17 + c6edc6e + 8d151f1 commit 83d6b1d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 47 deletions.
84 changes: 41 additions & 43 deletions src/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
28 changes: 24 additions & 4 deletions test/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 83d6b1d

Please sign in to comment.