This commit is contained in:
Ade Attwood 2017-06-24 12:36:23 +01:00
parent 8da166736b
commit ec3e74ffb3
13 changed files with 168 additions and 100 deletions

View file

@ -1,11 +1,15 @@
{ {
"name": "adeattwood/yii-helpers", "name": "adeattwood/yii-helpers",
"description": "Helpers for the yii2 framework", "description": "Helpers for the yii2 framework",
"version": "1",
"time": "2017-06-24 11:25:22",
"type": "library", "type": "library",
"authors": [ "authors": [
{ {
"name": "Ade Attwood", "name": "Ade Attwood",
"email": "attwood16@googlemail.com" "email": "attwood16@googlemail.com",
"homepage": "http:/adeattwood.co.uk",
"role": "Developer"
} }
], ],
"minimum-stability": "stable", "minimum-stability": "stable",
@ -15,16 +19,16 @@
} }
}, },
"autoload-dev": { "autoload-dev": {
"psr-4": { "psr-4": {
"adeattwood\\helpers\\tests\\": "tests" "adeattwood\\helpers\\tests\\": "tests"
} }
}, },
"require": { "require": {
"yiisoft/yii2": "^2.0" "yiisoft/yii2": "^2.0"
}, },
"require-dev": { "require-dev": {
"codeception/specify": "^0.4.6", "codeception/specify": "^0.4.6",
"codeception/verify": "^0.3.3", "codeception/verify": "^0.3.3",
"codeception/codeception": "^2.3" "codeception/codeception": "^2.3"
} }
} }

View file

@ -22,15 +22,15 @@ class ArrayHelper extends \yii\helpers\ArrayHelper
* *
* ~~~php * ~~~php
* ArrayHelper::numberArray( 5, 2, 2 ); * ArrayHelper::numberArray( 5, 2, 2 );
* *
* array(5) { * array(5) {
* [2] => int(2) * [2] => int(2)
* [4] => int(4) * [4] => int(4)
* [6] => int(6) * [6] => int(6)
* [8] => int(8) * [8] => int(8)
* [10]=> int(10) * [10]=> int(10)
* } * }
* ~~~ * ~~~
* *
* @param int $max The max number in the array * @param int $max The max number in the array
* @param int $min The min mumber in the array * @param int $min The min mumber in the array
@ -74,11 +74,11 @@ class ArrayHelper extends \yii\helpers\ArrayHelper
$value = Enum::isEmpty( $rawValue ) ? $defalts[ 'value' ] : Yii::$app->formatter->format( $rawValue, $format ); $value = Enum::isEmpty( $rawValue ) ? $defalts[ 'value' ] : Yii::$app->formatter->format( $rawValue, $format );
$lable = isset( $matches[ 5 ] ) ? $matches[ 5 ] : $model->getAttributeLabel( $matches[ 1 ] ); $lable = isset( $matches[ 5 ] ) ? $matches[ 5 ] : $model->getAttributeLabel( $matches[ 1 ] );
return new ArrayObject([ return new ArrayObject( [
'format' => $format, 'format' => $format,
'value' => $value, 'value' => $value,
'lable' => $lable, 'lable' => $lable,
'raw' => $rawValue 'raw' => $rawValue
]); ] );
} }
} }

View file

@ -20,27 +20,27 @@ class ArrayObject extends \ArrayObject
/** /**
* __construct * __construct
* *
* @param mixed $array * @param mixed $array The array to put into the array object
* @param mixed $flags * @param mixed $flags The falgs of the object
* @param string $iterator_class * @param string $iterator_class The iterator class
* *
* @return void * @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 ) {
$flags = self::ARRAY_AS_PROPS; $flags = self::ARRAY_AS_PROPS;
} }
parent::__construct( $array, $flags, $iterator_class ); parent::__construct( $array, $flags, $iterator_class );
} }
/** /**
* Test that the ArrayObject contains an item * Test that the ArrayObject contains an item
* *
* @param $item The item to test * @param $item The item to test
* *
* @return bool * @return bool
*/ */
public function contains( $item ) public function contains( $item )
{ {
@ -51,7 +51,7 @@ class ArrayObject extends \ArrayObject
/** /**
* Gets a random value from the array * Gets a random value from the array
* *
* @param int $count * @param int $count How many items to return
* *
* @return int|array * @return int|array
*/ */
@ -59,10 +59,10 @@ class ArrayObject extends \ArrayObject
{ {
$arr = ( array )$this; $arr = ( array )$this;
shuffle($arr); shuffle( $arr );
$r = array(); $r = array();
for ($i = 0; $i < $count; $i++) { for ( $i = 0; $i < $count; $i++ ) {
$r[] = $arr[$i]; $r[] = $arr[$i];
} }
return $count == 1 ? $r[0] : $r; return $count == 1 ? $r[0] : $r;
@ -71,86 +71,77 @@ class ArrayObject extends \ArrayObject
/** /**
* Converts the ArrayObject into a table * Converts the ArrayObject into a table
* *
* @param mixed $transpose * @param mixed $transpose
* @param mixed $recursive * @param mixed $recursive
* @param mixed $typeHint * @param mixed $typeHint
* @param string $tableOptions * @param string $tableOptions
* @param mixed $keyOptions * @param mixed $keyOptions
* @param string $valueOptions * @param string $valueOptions
* @param string $null * @param string $null
* *
* @return string The html for the table * @return string The html for the table
*/ */
public function toTable( public function toTable( $transpose = false, $recursive = false, $typeHint = true, $tableOptions = [ 'class' => 'table table-bordered table-striped' ], $keyOptions = [], $valueOptions = [ 'style' => 'cursor: default; border-bottom: 1px #aaa dashed;' ], $null = '<span class="not-set">(not set)</span>' )
$transpose = false, {
$recursive = false, if ( empty( ( array )$this ) ) {
$typeHint = true,
$tableOptions = ['class' => 'table table-bordered table-striped'],
$keyOptions = [],
$valueOptions = ['style' => 'cursor: default; border-bottom: 1px #aaa dashed;'],
$null = '<span class="not-set">(not set)</span>'
) {
// Sanity check
if (empty( ( array )$this)) {
return false; return false;
} }
$array = ( array )$this; $array = ( array )$this;
// Start the table // Start the table
$table = Html::beginTag('table', $tableOptions) . "\n"; $table = Html::beginTag( 'table', $tableOptions ) . "\n";
// The header // The header
$table .= "\t<tr>"; $table .= "\t<tr>";
if ($transpose) { if ( $transpose ) {
foreach ($this as $key => $value) { foreach ( $this as $key => $value ) {
if ($typeHint) { if ( $typeHint ) {
$valueOptions['title'] = Enum::getType(strtoupper($value)); $valueOptions['title'] = Enum::getType( strtoupper( $value ) );
} }
if (is_array($value)) { if ( is_array( $value ) ) {
$value = '<pre>' . print_r($value, true) . '</pre>'; $value = '<pre>' . print_r( $value, true ) . '</pre>';
} else { } else {
$value = Html::tag('span', $value, $valueOptions); $value = Html::tag( 'span', $value, $valueOptions );
} }
$table .= "\t\t<th>" . Html::tag('span', $key, $keyOptions) . "</th>" . $table .= "\t\t<th>" . Html::tag( 'span', $key, $keyOptions ) . "</th>" .
"<td>" . $value . "</td>\n\t</tr>\n"; "<td>" . $value . "</td>\n\t</tr>\n";
} }
$table .= "</table>"; $table .= "</table>";
return $table; return $table;
} }
if (!isset($array[0]) || !is_array($array[0])) { if ( !isset( $array[0] ) || !is_array( $array[0] ) ) {
$array = array($array); $array = array( $array );
} }
// Take the keys from the first row as the headings // Take the keys from the first row as the headings
foreach (array_keys($array[0]) as $heading) { foreach ( array_keys( $array[0] ) as $heading ) {
$table .= '<th>' . Html::tag('span', $heading, $keyOptions) . '</th>'; $table .= '<th>' . Html::tag( 'span', $heading, $keyOptions ) . '</th>';
} }
$table .= "</tr>\n"; $table .= "</tr>\n";
// The body // The body
foreach ($array as $row) { foreach ( $array as $row ) {
$table .= "\t<tr>"; $table .= "\t<tr>";
foreach ($row as $cell) { foreach ( $row as $cell ) {
$table .= '<td>'; $table .= '<td>';
// Cast objects // Cast objects
if (is_object($cell)) { if ( is_object( $cell ) ) {
$cell = (array)$cell; $cell = (array)$cell;
} }
if ($recursive === true && is_array($cell) && !empty($cell)) { if ( $recursive === true && is_array( $cell ) && !empty( $cell ) ) {
// Recursive mode // Recursive mode
$table .= "\n" . static::array2table($cell, true, true) . "\n"; $table .= "\n" . static::array2table( $cell, true, true ) . "\n";
} else { } else {
if (!is_null($cell) && is_bool($cell)) { if ( !is_null( $cell ) && is_bool( $cell ) ) {
$val = $cell ? 'true' : 'false'; $val = $cell ? 'true' : 'false';
$type = 'boolean'; $type = 'boolean';
} else { } else {
$chk = (strlen($cell) > 0); $chk = ( strlen( $cell ) > 0 );
$type = $chk ? Enum::getType($cell) : 'null'; $type = $chk ? Enum::getType( $cell ) : 'null';
$val = $chk ? htmlspecialchars((string)$cell) : $null; $val = $chk ? htmlspecialchars( (string)$cell ) : $null;
} }
if ($typeHint) { if ( $typeHint ) {
$valueOptions['title'] = $type; $valueOptions['title'] = $type;
} }
$table .= Html::tag('span', $val, $valueOptions); $table .= Html::tag( 'span', $val, $valueOptions );
} }
$table .= '</td>'; $table .= '</td>';
} }

View file

@ -44,15 +44,15 @@ class Dumper
); );
public static function dump( $var ) { public static function dump( $var ) {
VarDumper::setHandler(function ($var) { VarDumper::setHandler( function ( $var ) {
$cloner = new VarCloner(); $cloner = new VarCloner();
$dumper = 'cli' === PHP_SAPI ? new CliDumper() : new HtmlDumper(); $dumper = 'cli' === PHP_SAPI ? new CliDumper() : new HtmlDumper();
$dumper->setStyles( self::$styles ); $dumper->setStyles( self::$styles );
$dumper->setDisplayOptions( self::$displayOptions ); $dumper->setDisplayOptions( self::$displayOptions );
$dumper->dump( $cloner->cloneVar($var) ); $dumper->dump( $cloner->cloneVar( $var ) );
}); } );
dump( $var ); dump( $var );
} }
@ -96,7 +96,6 @@ class Dumper
foreach ( $methods as $method ) { foreach ( $methods as $method ) {
$prop = ''; $prop = '';
if ( $method->isPublic() ) { if ( $method->isPublic() ) {

View file

@ -13,7 +13,7 @@ namespace adeattwood\helpers;
*/ */
class Enum extends \yii\base\Object class Enum extends \yii\base\Object
{ {
const TYPE_ARRAY = 'array'; const TYPE_ARRAY = 'array';
const TYPE_NULL = 'null'; const TYPE_NULL = 'null';
const TYPE_BOOL = 'boolean'; const TYPE_BOOL = 'boolean';
const TYPE_FLOAT = 'float'; const TYPE_FLOAT = 'float';
@ -88,7 +88,7 @@ class Enum extends \yii\base\Object
* *
* @return boolean * @return boolean
*/ */
public static function isEmpty($var) public static function isEmpty( $var )
{ {
return !isset( $var ) ? true : ( is_array( $var ) ? empty( $var ) : ( $var === null || $var === '' ) ); return !isset( $var ) ? true : ( is_array( $var ) ? empty( $var ) : ( $var === null || $var === '' ) );
} }
@ -104,18 +104,18 @@ class Enum extends \yii\base\Object
* echo Enum::formatBytes(17328347842.25, 3); // returns: 16.138 GB * echo Enum::formatBytes(17328347842.25, 3); // returns: 16.138 GB
* ~~~ * ~~~
* *
* @param double $bytes number of bytes * @param double $bytes number of bytes
* @param integer $precision the number of decimal places to round off * @param integer $precision the number of decimal places to round off
* *
* @return string * @return string
*/ */
public static function formatBytes($bytes, $precision = 2) public static function formatBytes( $bytes, $precision = 2 )
{ {
$units = ['B', 'KB', 'MB', 'GB', 'TB']; $units = [ 'B', 'KB', 'MB', 'GB', 'TB' ];
$bytes = max($bytes, 0); $bytes = max( $bytes, 0 );
$pow = floor(($bytes ? log($bytes) : 0) / log(1024)); $pow = floor( ( $bytes ? log( $bytes ) : 0 ) / log( 1024 ) );
$pow = min($pow, count($units) - 1); $pow = min( $pow, count( $units ) - 1 );
$bytes /= pow(1024, $pow); $bytes /= pow( 1024, $pow );
return round($bytes, $precision) . ' ' . $units[$pow]; return round( $bytes, $precision ) . ' ' . $units[$pow];
} }
} }

View file

View file

@ -2,14 +2,33 @@
namespace adeattwood\helpers\tests\_data; namespace adeattwood\helpers\tests\_data;
/**
* @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 ModelOne extends \yii\base\Model class ModelOne extends \yii\base\Model
{ {
/**
* The first test proprety
*
* @var string
*/
public $propOne; public $propOne;
/**
* The validation rules for the model
*
* @return array
*/
public function rules() public function rules()
{ {
return [ return [
[ [ 'propOne' ], 'string' ] [ [ 'propOne' ], 'string' ]
]; ];
} }
} }

View file

@ -1,9 +1,15 @@
<?php <?php
namespace Helper; namespace Helper;
// here you can define custom actions /**
// all public methods declared in helper class will be available in $I * @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 Unit extends \Codeception\Module class Unit extends \Codeception\Module
{ {

View file

@ -1,7 +1,14 @@
<?php <?php
/** /**
* @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
*
* Inherited Methods * Inherited Methods
* @method void wantToTest($text) * @method void wantToTest($text)
* @method void wantTo($text) * @method void wantTo($text)
@ -19,8 +26,4 @@
class UnitTester extends \Codeception\Actor class UnitTester extends \Codeception\Actor
{ {
use _generated\UnitTesterActions; use _generated\UnitTesterActions;
/**
* Define custom actions here
*/
} }

View file

@ -3,6 +3,15 @@
use adeattwood\helpers\ArrayHelper; use adeattwood\helpers\ArrayHelper;
use adeattwood\helpers\tests\_data\ModelOne; use adeattwood\helpers\tests\_data\ModelOne;
/**
* @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 ArrayHelperCest class ArrayHelperCest
{ {

View file

@ -2,6 +2,15 @@
use adeattwood\helpers\ArrayObject; use adeattwood\helpers\ArrayObject;
/**
* @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 ArrayObjectCest class ArrayObjectCest
{ {
public $arrayObject; public $arrayObject;
@ -11,16 +20,16 @@ class ArrayObjectCest
"key_two" => "Value Two" "key_two" => "Value Two"
]; ];
public function _before(UnitTester $I) public function _before( UnitTester $I )
{ {
$this->arrayObject = new ArrayObject( $this->array ); $this->arrayObject = new ArrayObject( $this->array );
} }
public function _after(UnitTester $I) public function _after( UnitTester $I )
{ {
} }
public function testArrayAsProps(UnitTester $I) public function testArrayAsProps( UnitTester $I )
{ {
$I->assertEquals( $this->arrayObject->key_one, "Value One" ); $I->assertEquals( $this->arrayObject->key_one, "Value One" );
} }
@ -48,5 +57,4 @@ class ArrayObjectCest
$I->assertContains( '<table class="table table-bordered table-striped">', $table ); $I->assertContains( '<table class="table table-bordered table-striped">', $table );
$I->assertContains( '<span title="string" style="cursor: default; border-bottom: 1px #aaa dashed;">Value One</span>', $table ); $I->assertContains( '<span title="string" style="cursor: default; border-bottom: 1px #aaa dashed;">Value One</span>', $table );
} }
} }

30
tests/unit/EnumCest.php Normal file
View file

@ -0,0 +1,30 @@
<?php
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 EnumCest
{
public function _before( UnitTester $I )
{
}
public function _after( UnitTester $I )
{
}
public function testNumberArray( UnitTester $I )
{
$I->assertEquals( Enum::formatBytes( 28434322.25 ), "27.12 MB" );
$I->assertEquals( Enum::formatBytes( 17328347842.25, 3 ), "16.138 GB" );
}
}

View file

@ -8,7 +8,7 @@
* @license BSD-2-Clause http://adeattwood.co.uk/license.html * @license BSD-2-Clause http://adeattwood.co.uk/license.html
* @link adeattwood.co.uk * @link adeattwood.co.uk
* @since v0.1 * @since v0.1
*/ */
require __DIR__ . "/../../vendor/autoload.php"; require __DIR__ . "/../../vendor/autoload.php";
require __DIR__ . "/../../vendor/yiisoft/yii2/Yii.php"; require __DIR__ . "/../../vendor/yiisoft/yii2/Yii.php";
@ -19,4 +19,3 @@ $config = [
]; ];
( new yii\web\Application( $config ) ); ( new yii\web\Application( $config ) );