From a156d85ebb1ed92c7efaed85c8f9bfefebc1a3dd Mon Sep 17 00:00:00 2001 From: Ade Attwood Date: Thu, 22 Jun 2017 23:09:28 +0100 Subject: [PATCH] Add docs and array helper class --- src/ArrayHelper.php | 45 +++++++++++++++++++++++++++ src/ArrayObject.php | 57 ++++++++++++++++++++++++++++++++-- src/Enum.php | 13 +++++--- tests/unit/ArrayHelperCest.php | 25 +++++++++++++++ 4 files changed, 134 insertions(+), 6 deletions(-) create mode 100644 src/ArrayHelper.php create mode 100644 tests/unit/ArrayHelperCest.php diff --git a/src/ArrayHelper.php b/src/ArrayHelper.php new file mode 100644 index 0000000..05614e1 --- /dev/null +++ b/src/ArrayHelper.php @@ -0,0 +1,45 @@ + + * @copyright 2017 adeattwood.co.uk + * @license BSD-2-Clause http://adeattwood.co.uk/license.html + * @link adeattwood.co.uk + * @since v0.1 + */ +class ArrayHelper extends \yii\helpers\ArrayHelper +{ + /** + * Creates a array of numbers + * + * ~~~php + * ArrayHelper::numberArray( 5, 2, 2 ); + * + * array(5) { + * [2] => int(2) + * [4] => int(4) + * [6] => int(6) + * [8] => int(8) + * [10]=> int(10) + * } + * ~~~ + * + * @param int $max The max number in the array + * @param int $min The min mumber in the array + * @param int $step The step for the itarator + * + * @return array + */ + public static function numberArray( $max, $min = 0, $step = 1 ) + { + $array = []; + for( $i = $min; $i <= $max; $i += $step ) { + $array[ $i ] = $i; + } + return $array; + } +} diff --git a/src/ArrayObject.php b/src/ArrayObject.php index d3b6d35..2094c10 100644 --- a/src/ArrayObject.php +++ b/src/ArrayObject.php @@ -6,8 +6,26 @@ use Yii; use yii\helpers\Html; use adeattwood\helpers\Enum; +/** + * @category PHP + * @package adeattwood\yii-helpers + * @author Ade Attwood + * @copyright 2017 adeattwood.co.uk + * @license BSD-2-Clause http://adeattwood.co.uk/license.html + * @link adeattwood.co.uk + * @since v0.1 + */ class ArrayObject extends \ArrayObject { + /** + * __construct + * + * @param mixed $array + * @param mixed $flags + * @param string $iterator_class + * + * @return void + */ public function __construct( $array = [], $flags = null, $iterator_class = 'ArrayIterator' ) { if( $flags === null ) { @@ -18,9 +36,11 @@ class ArrayObject extends \ArrayObject } /** - * undocumented function + * Test that the ArrayObject contains an item * - * @return void + * @param $item The item to test + * + * @return bool */ public function contains( $item ) { @@ -28,6 +48,39 @@ class ArrayObject extends \ArrayObject return isset( $flipped[ $item ] ); } + /** + * Gets a random value from the array + * + * @param int $count + * + * @return int|array + */ + public function randomValue( $count = 1) + { + $arr = ( array )$this; + + shuffle($arr); + + $r = array(); + for ($i = 0; $i < $count; $i++) { + $r[] = $arr[$i]; + } + return $count == 1 ? $r[0] : $r; + } + + /** + * Converts the ArrayObject into a table + * + * @param mixed $transpose + * @param mixed $recursive + * @param mixed $typeHint + * @param string $tableOptions + * @param mixed $keyOptions + * @param string $valueOptions + * @param string $null + * + * @return string + */ public function toTable( $transpose = false, $recursive = false, diff --git a/src/Enum.php b/src/Enum.php index 217df41..ede276f 100644 --- a/src/Enum.php +++ b/src/Enum.php @@ -3,8 +3,13 @@ namespace adeattwood\helpers; /** - * Class Enum - * @author Ade Attwood + * @category PHP + * @package adeattwood\yii-helpers + * @author Ade Attwood + * @copyright 2017 adeattwood.co.uk + * @license BSD-2-Clause http://adeattwood.co.uk/license.html + * @link adeattwood.co.uk + * @since v0.1 */ class Enum extends \yii\base\Object { @@ -72,7 +77,7 @@ class Enum extends \yii\base\Object * * Example: * - * ~~~ + * ~~~php * if (Enum::isEmpty([]) { * echo 'Not empty'; * } @@ -92,7 +97,7 @@ class Enum extends \yii\base\Object * * Example: * - * ~~~ + * ~~~php * echo Enum::formatBytes(120.32); // returns: 1.17 KB * echo Enum::formatBytes(28434322.25); // returns: 27.12 MB * echo Enum::formatBytes(17328347842.25, 3); // returns: 16.138 GB diff --git a/tests/unit/ArrayHelperCest.php b/tests/unit/ArrayHelperCest.php new file mode 100644 index 0000000..7a85209 --- /dev/null +++ b/tests/unit/ArrayHelperCest.php @@ -0,0 +1,25 @@ +assertEquals( $array[ 0 ], 0 ); + $I->assertEquals( $array[ 10 ], 10 ); + $array = ArrayHelper::numberArray( 10, 2, 2 ); + var_dump( $array ); + } +}