Skip to content

Commit

Permalink
Merge branch 'master' of github.com:cocur/chain
Browse files Browse the repository at this point in the history
* 'master' of github.com:cocur/chain:
  Fix code style
  Fix code style
  Add AbstractChain
  Applied fixes from StyleCI
  • Loading branch information
Florian Eckerstorfer committed Nov 6, 2015
2 parents 50678ed + 418354a commit 81c646f
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 63 deletions.
66 changes: 66 additions & 0 deletions src/AbstractChain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Cocur\Chain;

use ArrayAccess;
use ArrayIterator;
use IteratorAggregate;

/**
* Chain.
*
* @author Florian Eckerstorfer
* @copyright 2015 Florian Eckerstorfer
*/
abstract class AbstractChain implements ArrayAccess, IteratorAggregate
{
/**
* @var array
*/
public $array = [];

/**
* @return ArrayIterator
*/
public function getIterator()
{
return new ArrayIterator($this->array);
}

/**
* @param mixed $offset
*
* @return bool
*/
public function offsetExists($offset)
{
return isset($this->array[$offset]);
}

/**
* @param mixed $offset
*
* @return mixed
*/
public function offsetGet($offset)
{
return $this->array[$offset];
}

/**
* @param mixed $offset
* @param mixed $value
*/
public function offsetSet($offset, $value)
{
$this->array[$offset] = $value;
}

/**
* @param mixed $offset
*/
public function offsetUnset($offset)
{
unset($this->array[$offset]);
}
}
55 changes: 1 addition & 54 deletions src/Chain.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Cocur\Chain;

use ArrayAccess;
use ArrayIterator;
use Cocur\Chain\Link\ChangeKeyCase;
use Cocur\Chain\Link\Combine;
use Cocur\Chain\Link\Count;
Expand Down Expand Up @@ -34,15 +32,14 @@
use Cocur\Chain\Link\Sum;
use Cocur\Chain\Link\Unique;
use Cocur\Chain\Link\Unshift;
use IteratorAggregate;

/**
* Chain.
*
* @author Florian Eckerstorfer
* @copyright 2015 Florian Eckerstorfer
*/
class Chain implements ArrayAccess, IteratorAggregate
class Chain extends AbstractChain
{
use ChangeKeyCase,
Combine,
Expand Down Expand Up @@ -75,11 +72,6 @@ class Chain implements ArrayAccess, IteratorAggregate
Unique,
Unshift;

/**
* @var array
*/
public $array = [];

/**
* @param array $array
*
Expand All @@ -92,49 +84,4 @@ public static function create(array $array = [])

return $chain;
}

/**
* @return ArrayIterator
*/
public function getIterator()
{
return new ArrayIterator($this->array);
}

/**
* @param mixed $offset
*
* @return bool
*/
public function offsetExists($offset)
{
return isset($this->array[$offset]);
}

/**
* @param mixed $offset
*
* @return mixed
*/
public function offsetGet($offset)
{
return $this->array[$offset];
}

/**
* @param mixed $offset
* @param mixed $value
*/
public function offsetSet($offset, $value)
{
$this->array[$offset] = $value;
}

/**
* @param mixed $offset
*/
public function offsetUnset($offset)
{
unset($this->array[$offset]);
}
}
7 changes: 3 additions & 4 deletions src/Link/Sort.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
trait Sort
{
/**
* Sort a Chain
* Sort a Chain.
*
* @param int|callable $sortBy
* @param array $options
Expand Down Expand Up @@ -55,13 +55,12 @@ private function sortWithFlags($sortFlags = SORT_REGULAR, array $options = [])
{
if (!empty($options['assoc']) && !empty($options['reverse'])) {
arsort($this->array, $sortFlags);
} else if (!empty($options['assoc'])) {
} elseif (!empty($options['assoc'])) {
asort($this->array, $sortFlags);
} else if (!empty($options['reverse'])) {
} elseif (!empty($options['reverse'])) {
rsort($this->array, $sortFlags);
} else {
sort($this->array, $sortFlags);
}
}
}

10 changes: 5 additions & 5 deletions tests/ChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function chainHasTraits()

/**
* @test
* @covers Cocur\Chain\Chain::getIterator()
* @covers Cocur\Chain\AbstractChain::getIterator()
*/
public function chainIsTraversable()
{
Expand All @@ -79,10 +79,10 @@ public function chainIsTraversable()

/**
* @test
* @covers Cocur\Chain\Chain::offsetExists()
* @covers Cocur\Chain\Chain::offsetGet()
* @covers Cocur\Chain\Chain::offsetSet()
* @covers Cocur\Chain\Chain::offsetUnset()
* @covers Cocur\Chain\AbstractChain::offsetExists()
* @covers Cocur\Chain\AbstractChain::offsetGet()
* @covers Cocur\Chain\AbstractChain::offsetSet()
* @covers Cocur\Chain\AbstractChain::offsetUnset()
*/
public function chainAllowsArrayAccess()
{
Expand Down

0 comments on commit 81c646f

Please sign in to comment.