diff --git a/composer.json b/composer.json index c9e9fa9..fe7e6ee 100644 --- a/composer.json +++ b/composer.json @@ -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" + } +} \ No newline at end of file diff --git a/src/ArrayHelper.php b/src/ArrayHelper.php index 444931f..596065a 100644 --- a/src/ArrayHelper.php +++ b/src/ArrayHelper.php @@ -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 - ]); + ] ); } } diff --git a/src/ArrayObject.php b/src/ArrayObject.php index dfdbdd4..f3b1394 100644 --- a/src/ArrayObject.php +++ b/src/ArrayObject.php @@ -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 = '(not set)' - ) { - - // 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 = '(not set)' ) + { + 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"; - 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 = '
' . print_r($value, true) . '
'; + if ( is_array( $value ) ) { + $value = '
' . print_r( $value, true ) . '
'; } else { - $value = Html::tag('span', $value, $valueOptions); + $value = Html::tag( 'span', $value, $valueOptions ); } - $table .= "\t\t" . Html::tag('span', $key, $keyOptions) . "" . + $table .= "\t\t" . Html::tag( 'span', $key, $keyOptions ) . "" . "" . $value . "\n\t\n"; } $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 .= '' . Html::tag('span', $heading, $keyOptions) . ''; + foreach ( array_keys( $array[0] ) as $heading ) { + $table .= '' . Html::tag( 'span', $heading, $keyOptions ) . ''; } $table .= "\n"; // The body - foreach ($array as $row) { + foreach ( $array as $row ) { $table .= "\t"; - foreach ($row as $cell) { + foreach ( $row as $cell ) { $table .= ''; // 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 .= ''; } diff --git a/src/Dumper.php b/src/Dumper.php index c2522bf..1b56140 100644 --- a/src/Dumper.php +++ b/src/Dumper.php @@ -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() ) { diff --git a/src/Enum.php b/src/Enum.php index 3ba6918..bf0c8cf 100644 --- a/src/Enum.php +++ b/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]; } } diff --git a/tests/_data/.gitkeep b/tests/_data/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/tests/_data/ModelOne.php b/tests/_data/ModelOne.php index a2f0272..1433de3 100644 --- a/tests/_data/ModelOne.php +++ b/tests/_data/ModelOne.php @@ -2,14 +2,33 @@ namespace adeattwood\helpers\tests\_data; +/** + * @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 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' ] ]; } -} \ No newline at end of file +} diff --git a/tests/_support/Helper/Unit.php b/tests/_support/Helper/Unit.php index 6064d37..95a09c9 100644 --- a/tests/_support/Helper/Unit.php +++ b/tests/_support/Helper/Unit.php @@ -1,9 +1,15 @@ + * @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 { diff --git a/tests/_support/UnitTester.php b/tests/_support/UnitTester.php index 2835357..ce6c790 100644 --- a/tests/_support/UnitTester.php +++ b/tests/_support/UnitTester.php @@ -1,7 +1,14 @@ + * @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 - */ } diff --git a/tests/unit/ArrayHelperCest.php b/tests/unit/ArrayHelperCest.php index f8695d7..ecbb033 100644 --- a/tests/unit/ArrayHelperCest.php +++ b/tests/unit/ArrayHelperCest.php @@ -3,6 +3,15 @@ use adeattwood\helpers\ArrayHelper; use adeattwood\helpers\tests\_data\ModelOne; +/** + * @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 ArrayHelperCest { diff --git a/tests/unit/ArrayObjectCest.php b/tests/unit/ArrayObjectCest.php index def29e1..3dfc1f0 100644 --- a/tests/unit/ArrayObjectCest.php +++ b/tests/unit/ArrayObjectCest.php @@ -2,6 +2,15 @@ use adeattwood\helpers\ArrayObject; +/** + * @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 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 ); $I->assertContains( 'Value One', $table ); } - } diff --git a/tests/unit/EnumCest.php b/tests/unit/EnumCest.php new file mode 100644 index 0000000..5bc4522 --- /dev/null +++ b/tests/unit/EnumCest.php @@ -0,0 +1,30 @@ + + * @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" ); + } +} diff --git a/tests/unit/_bootstrap.php b/tests/unit/_bootstrap.php index 0896595..0362bc1 100644 --- a/tests/unit/_bootstrap.php +++ b/tests/unit/_bootstrap.php @@ -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 ) ); -