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",
"description": "Helpers for the yii2 framework",
"version": "1",
"time": "2017-06-24 11:25:22",
"type": "library",
"authors": [
{
"name": "Ade Attwood",
"email": "attwood16@googlemail.com"
"email": "attwood16@googlemail.com",
"homepage": "http:/adeattwood.co.uk",
"role": "Developer"
}
],
"minimum-stability": "stable",

View file

@ -74,11 +74,11 @@ class ArrayHelper extends \yii\helpers\ArrayHelper
$value = Enum::isEmpty( $rawValue ) ? $defalts[ 'value' ] : Yii::$app->formatter->format( $rawValue, $format );
$lable = isset( $matches[ 5 ] ) ? $matches[ 5 ] : $model->getAttributeLabel( $matches[ 1 ] );
return new ArrayObject([
return new ArrayObject( [
'format' => $format,
'value' => $value,
'lable' => $lable,
'raw' => $rawValue
]);
] );
}
}

View file

@ -20,9 +20,9 @@ class ArrayObject extends \ArrayObject
/**
* __construct
*
* @param mixed $array
* @param mixed $flags
* @param string $iterator_class
* @param mixed $array The array to put into the array object
* @param mixed $flags The falgs of the object
* @param string $iterator_class The iterator class
*
* @return void
*/
@ -51,7 +51,7 @@ class ArrayObject extends \ArrayObject
/**
* Gets a random value from the array
*
* @param int $count
* @param int $count How many items to return
*
* @return int|array
*/
@ -59,10 +59,10 @@ class ArrayObject extends \ArrayObject
{
$arr = ( array )$this;
shuffle($arr);
shuffle( $arr );
$r = array();
for ($i = 0; $i < $count; $i++) {
for ( $i = 0; $i < $count; $i++ ) {
$r[] = $arr[$i];
}
return $count == 1 ? $r[0] : $r;
@ -81,76 +81,67 @@ class ArrayObject extends \ArrayObject
*
* @return string The html for the table
*/
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>'
) {
// Sanity check
if (empty( ( array )$this)) {
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>' )
{
if ( empty( ( array )$this ) ) {
return false;
}
$array = ( array )$this;
// Start the table
$table = Html::beginTag('table', $tableOptions) . "\n";
$table = Html::beginTag( 'table', $tableOptions ) . "\n";
// The header
$table .= "\t<tr>";
if ($transpose) {
foreach ($this as $key => $value) {
if ($typeHint) {
$valueOptions['title'] = Enum::getType(strtoupper($value));
if ( $transpose ) {
foreach ( $this as $key => $value ) {
if ( $typeHint ) {
$valueOptions['title'] = Enum::getType( strtoupper( $value ) );
}
if (is_array($value)) {
$value = '<pre>' . print_r($value, true) . '</pre>';
if ( is_array( $value ) ) {
$value = '<pre>' . print_r( $value, true ) . '</pre>';
} 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";
}
$table .= "</table>";
return $table;
}
if (!isset($array[0]) || !is_array($array[0])) {
$array = array($array);
if ( !isset( $array[0] ) || !is_array( $array[0] ) ) {
$array = array( $array );
}
// Take the keys from the first row as the headings
foreach (array_keys($array[0]) as $heading) {
$table .= '<th>' . Html::tag('span', $heading, $keyOptions) . '</th>';
foreach ( array_keys( $array[0] ) as $heading ) {
$table .= '<th>' . Html::tag( 'span', $heading, $keyOptions ) . '</th>';
}
$table .= "</tr>\n";
// The body
foreach ($array as $row) {
foreach ( $array as $row ) {
$table .= "\t<tr>";
foreach ($row as $cell) {
foreach ( $row as $cell ) {
$table .= '<td>';
// Cast objects
if (is_object($cell)) {
if ( is_object( $cell ) ) {
$cell = (array)$cell;
}
if ($recursive === true && is_array($cell) && !empty($cell)) {
if ( $recursive === true && is_array( $cell ) && !empty( $cell ) ) {
// Recursive mode
$table .= "\n" . static::array2table($cell, true, true) . "\n";
$table .= "\n" . static::array2table( $cell, true, true ) . "\n";
} else {
if (!is_null($cell) && is_bool($cell)) {
if ( !is_null( $cell ) && is_bool( $cell ) ) {
$val = $cell ? 'true' : 'false';
$type = 'boolean';
} else {
$chk = (strlen($cell) > 0);
$type = $chk ? Enum::getType($cell) : 'null';
$val = $chk ? htmlspecialchars((string)$cell) : $null;
$chk = ( strlen( $cell ) > 0 );
$type = $chk ? Enum::getType( $cell ) : 'null';
$val = $chk ? htmlspecialchars( (string)$cell ) : $null;
}
if ($typeHint) {
if ( $typeHint ) {
$valueOptions['title'] = $type;
}
$table .= Html::tag('span', $val, $valueOptions);
$table .= Html::tag( 'span', $val, $valueOptions );
}
$table .= '</td>';
}

View file

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

View file

@ -88,7 +88,7 @@ class Enum extends \yii\base\Object
*
* @return boolean
*/
public static function isEmpty($var)
public static function isEmpty( $var )
{
return !isset( $var ) ? true : ( is_array( $var ) ? empty( $var ) : ( $var === null || $var === '' ) );
}
@ -109,13 +109,13 @@ class Enum extends \yii\base\Object
*
* @return string
*/
public static function formatBytes($bytes, $precision = 2)
public static function formatBytes( $bytes, $precision = 2 )
{
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
$bytes /= pow(1024, $pow);
return round($bytes, $precision) . ' ' . $units[$pow];
$units = [ 'B', 'KB', 'MB', 'GB', 'TB' ];
$bytes = max( $bytes, 0 );
$pow = floor( ( $bytes ? log( $bytes ) : 0 ) / log( 1024 ) );
$pow = min( $pow, count( $units ) - 1 );
$bytes /= pow( 1024, $pow );
return round( $bytes, $precision ) . ' ' . $units[$pow];
}
}

View file

View file

@ -2,10 +2,29 @@
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
{
/**
* The first test proprety
*
* @var string
*/
public $propOne;
/**
* The validation rules for the model
*
* @return array
*/
public function rules()
{
return [

View file

@ -1,9 +1,15 @@
<?php
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
{

View file

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

View file

@ -3,6 +3,15 @@
use adeattwood\helpers\ArrayHelper;
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
{

View file

@ -2,6 +2,15 @@
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
{
public $arrayObject;
@ -11,16 +20,16 @@ class ArrayObjectCest
"key_two" => "Value Two"
];
public function _before(UnitTester $I)
public function _before( UnitTester $I )
{
$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" );
}
@ -48,5 +57,4 @@ class ArrayObjectCest
$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 );
}
}

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

@ -19,4 +19,3 @@ $config = [
];
( new yii\web\Application( $config ) );