phpcs
This commit is contained in:
parent
8da166736b
commit
ec3e74ffb3
13 changed files with 168 additions and 100 deletions
|
|
@ -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",
|
||||
|
|
@ -15,16 +19,16 @@
|
|||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"psr-4": {
|
||||
"adeattwood\\helpers\\tests\\": "tests"
|
||||
}
|
||||
},
|
||||
"require": {
|
||||
"yiisoft/yii2": "^2.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"codeception/specify": "^0.4.6",
|
||||
"codeception/verify": "^0.3.3",
|
||||
"codeception/codeception": "^2.3"
|
||||
}
|
||||
}
|
||||
"require-dev": {
|
||||
"codeception/specify": "^0.4.6",
|
||||
"codeception/verify": "^0.3.3",
|
||||
"codeception/codeception": "^2.3"
|
||||
}
|
||||
}
|
||||
|
|
@ -22,15 +22,15 @@ class ArrayHelper extends \yii\helpers\ArrayHelper
|
|||
*
|
||||
* ~~~php
|
||||
* ArrayHelper::numberArray( 5, 2, 2 );
|
||||
*
|
||||
* array(5) {
|
||||
* [2] => int(2)
|
||||
* [4] => int(4)
|
||||
* [6] => int(6)
|
||||
* [8] => int(8)
|
||||
* [10]=> int(10)
|
||||
* }
|
||||
* ~~~
|
||||
*
|
||||
* 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
|
||||
|
|
@ -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
|
||||
]);
|
||||
] );
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,27 +20,27 @@ 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
|
||||
*/
|
||||
public function __construct( $array = [], $flags = null, $iterator_class = 'ArrayIterator' )
|
||||
{
|
||||
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
|
||||
*
|
||||
* @param $item The item to test
|
||||
*
|
||||
* @return bool
|
||||
* @return bool
|
||||
*/
|
||||
public function contains( $item )
|
||||
{
|
||||
|
|
@ -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;
|
||||
|
|
@ -71,86 +71,77 @@ class ArrayObject extends \ArrayObject
|
|||
/**
|
||||
* Converts the ArrayObject into a table
|
||||
*
|
||||
* @param mixed $transpose
|
||||
* @param mixed $recursive
|
||||
* @param mixed $typeHint
|
||||
* @param mixed $transpose
|
||||
* @param mixed $recursive
|
||||
* @param mixed $typeHint
|
||||
* @param string $tableOptions
|
||||
* @param mixed $keyOptions
|
||||
* @param mixed $keyOptions
|
||||
* @param string $valueOptions
|
||||
* @param string $null
|
||||
*
|
||||
* @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>';
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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() ) {
|
||||
|
|
|
|||
20
src/Enum.php
20
src/Enum.php
|
|
@ -13,7 +13,7 @@ namespace adeattwood\helpers;
|
|||
*/
|
||||
class Enum extends \yii\base\Object
|
||||
{
|
||||
const TYPE_ARRAY = 'array';
|
||||
const TYPE_ARRAY = 'array';
|
||||
const TYPE_NULL = 'null';
|
||||
const TYPE_BOOL = 'boolean';
|
||||
const TYPE_FLOAT = 'float';
|
||||
|
|
@ -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 === '' ) );
|
||||
}
|
||||
|
|
@ -104,18 +104,18 @@ class Enum extends \yii\base\Object
|
|||
* 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
|
||||
*
|
||||
* @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];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,14 +2,33 @@
|
|||
|
||||
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 [
|
||||
[ [ 'propOne' ], 'string' ]
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
{
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
*/
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
{
|
||||
|
||||
|
|
|
|||
|
|
@ -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
30
tests/unit/EnumCest.php
Normal 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" );
|
||||
}
|
||||
}
|
||||
|
|
@ -8,7 +8,7 @@
|
|||
* @license BSD-2-Clause http://adeattwood.co.uk/license.html
|
||||
* @link adeattwood.co.uk
|
||||
* @since v0.1
|
||||
*/
|
||||
*/
|
||||
|
||||
require __DIR__ . "/../../vendor/autoload.php";
|
||||
require __DIR__ . "/../../vendor/yiisoft/yii2/Yii.php";
|
||||
|
|
@ -19,4 +19,3 @@ $config = [
|
|||
];
|
||||
|
||||
( new yii\web\Application( $config ) );
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue