Add docs and array helper class

This commit is contained in:
Ade Attwood 2017-06-22 23:09:28 +01:00
parent 44cc86c51a
commit a156d85ebb
4 changed files with 134 additions and 6 deletions

45
src/ArrayHelper.php Normal file
View file

@ -0,0 +1,45 @@
<?php
namespace adeattwood\helpers;
/**
* @category PHP
* @package adeattwood\yii-helpers
* @author Ade Attwood <attwood16@googlemail.com>
* @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;
}
}

View file

@ -6,8 +6,26 @@ use Yii;
use yii\helpers\Html; use yii\helpers\Html;
use adeattwood\helpers\Enum; use adeattwood\helpers\Enum;
/**
* @category PHP
* @package adeattwood\yii-helpers
* @author Ade Attwood <attwood16@googlemail.com>
* @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 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' ) public function __construct( $array = [], $flags = null, $iterator_class = 'ArrayIterator' )
{ {
if( $flags === null ) { 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 ) public function contains( $item )
{ {
@ -28,6 +48,39 @@ class ArrayObject extends \ArrayObject
return isset( $flipped[ $item ] ); 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( public function toTable(
$transpose = false, $transpose = false,
$recursive = false, $recursive = false,

View file

@ -3,8 +3,13 @@
namespace adeattwood\helpers; namespace adeattwood\helpers;
/** /**
* Class Enum * @category PHP
* @author Ade Attwood * @package adeattwood\yii-helpers
* @author Ade Attwood <attwood16@googlemail.com>
* @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 class Enum extends \yii\base\Object
{ {
@ -72,7 +77,7 @@ class Enum extends \yii\base\Object
* *
* Example: * Example:
* *
* ~~~ * ~~~php
* if (Enum::isEmpty([]) { * if (Enum::isEmpty([]) {
* echo 'Not empty'; * echo 'Not empty';
* } * }
@ -92,7 +97,7 @@ class Enum extends \yii\base\Object
* *
* Example: * Example:
* *
* ~~~ * ~~~php
* echo Enum::formatBytes(120.32); // returns: 1.17 KB * echo Enum::formatBytes(120.32); // returns: 1.17 KB
* echo Enum::formatBytes(28434322.25); // returns: 27.12 MB * echo Enum::formatBytes(28434322.25); // returns: 27.12 MB
* echo Enum::formatBytes(17328347842.25, 3); // returns: 16.138 GB * echo Enum::formatBytes(17328347842.25, 3); // returns: 16.138 GB

View file

@ -0,0 +1,25 @@
<?php
use adeattwood\helpers\ArrayHelper;
class ArrayHelperCest
{
public function _before(UnitTester $I)
{
}
public function _after(UnitTester $I)
{
}
public function testNumberArray(UnitTester $I)
{
$array = ArrayHelper::numberArray( 11 );
$I->assertEquals( $array[ 0 ], 0 );
$I->assertEquals( $array[ 10 ], 10 );
$array = ArrayHelper::numberArray( 10, 2, 2 );
var_dump( $array );
}
}