Skip to content

Commit

Permalink
Better naming helpers. (#653)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw authored Apr 5, 2023
1 parent c64276f commit a085501
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 98 deletions.
20 changes: 10 additions & 10 deletions src/Helper/ArrayHelper.php → src/Helper/DbArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/**
* Array manipulation methods.
*/
final class ArrayHelper
final class DbArrayHelper
{
/**
* Returns the values of a specified column in an array.
Expand All @@ -35,11 +35,11 @@ final class ArrayHelper
* ['id' => '123', 'data' => 'abc'],
* ['id' => '345', 'data' => 'def'],
* ];
* $result = ArrayHelper::getColumn($array, 'id');
* $result = DbArrayHelper::getColumn($array, 'id');
* // the result is: ['123', '345']
*
* // using anonymous function
* $result = ArrayHelper::getColumn($array, function ($element) {
* $result = DbArrayHelper::getColumn($array, function ($element) {
* return $element['id'];
* });
* ```
Expand Down Expand Up @@ -83,17 +83,17 @@ static function (array|object $element) use ($name): mixed {
*
* ```php
* // working with array
* $username = ArrayHelper::getValueByPath($_POST, 'username');
* $username = DbArrayHelper::getValueByPath($_POST, 'username');
* // working with object
* $username = ArrayHelper::getValueByPath($user, 'username');
* $username = DbArrayHelper::getValueByPath($user, 'username');
* // working with anonymous function
* $fullName = ArrayHelper::getValueByPath($user, function ($user, $defaultValue) {
* $fullName = DbArrayHelper::getValueByPath($user, function ($user, $defaultValue) {
* return $user->firstName . ' ' . $user->lastName;
* });
* // using dot format to retrieve the property of embedded object
* $street = \yii\helpers\ArrayHelper::getValue($users, 'address.street');
* $street = \yii\helpers\DbArrayHelper::getValue($users, 'address.street');
* // using an array of keys to retrieve the value
* $value = \yii\helpers\ArrayHelper::getValue($versions, ['1.0', 'date']);
* $value = \yii\helpers\DbArrayHelper::getValue($versions, ['1.0', 'date']);
* ```
*
* @param array|object $array Array or object to extract value from.
Expand Down Expand Up @@ -162,7 +162,7 @@ public static function getValueByPath(object|array $array, Closure|string $key,
* ['id' => '345', 'data' => 'def', 'device' => 'tablet'],
* ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone'],
* ];
* $result = ArrayHelper::index($array, 'id');
* $result = DbArrayHelper::index($array, 'id');
* ```
*
* The result will be an associative array, where the key is the value of `id` attribute
Expand All @@ -178,7 +178,7 @@ public static function getValueByPath(object|array $array, Closure|string $key,
* Passing `id` as a third argument will group `$array` by `id`:
*
* ```php
* $result = ArrayHelper::index($array, null, 'id');
* $result = DbArrayHelper::index($array, null, 'id');
* ```
*
* The result will be a multidimensional array grouped by `id` on the first level, by `device` on the second level
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* String manipulation methods.
*/
final class StringHelper
final class DbStringHelper
{
/**
* Returns the trailing name part of a path.
Expand Down
2 changes: 1 addition & 1 deletion src/Helper/UuidHelper.php → src/Helper/DbUuidHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use function preg_match;
use function str_replace;

final class UuidHelper
final class DbUuidHelper
{
public static function toUuid(string $blobString): string
{
Expand Down
8 changes: 4 additions & 4 deletions src/Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\Helper\ArrayHelper;
use Yiisoft\Db\Helper\DbArrayHelper;
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;

use function array_key_exists;
Expand Down Expand Up @@ -226,7 +226,7 @@ public function all(): array
return [];
}

return ArrayHelper::populate($this->createCommand()->queryAll(), $this->indexBy);
return DbArrayHelper::populate($this->createCommand()->queryAll(), $this->indexBy);
}

public function average(string $q): int|float|null|string
Expand All @@ -242,7 +242,7 @@ public function batch(int $batchSize = 100): BatchQueryResultInterface
return $this->db
->createBatchQueryResult($this)
->batchSize($batchSize)
->setPopulatedMethod(fn (array $rows, Closure|string|null $indexBy = null): array => ArrayHelper::populate($rows, $indexBy))
->setPopulatedMethod(fn (array $rows, Closure|string|null $indexBy = null): array => DbArrayHelper::populate($rows, $indexBy))
;
}

Expand Down Expand Up @@ -316,7 +316,7 @@ public function each(int $batchSize = 100): BatchQueryResultInterface
return $this->db
->createBatchQueryResult($this, true)
->batchSize($batchSize)
->setPopulatedMethod(fn (array $rows, Closure|string|null $indexBy = null): array => ArrayHelper::populate($rows, $indexBy))
->setPopulatedMethod(fn (array $rows, Closure|string|null $indexBy = null): array => DbArrayHelper::populate($rows, $indexBy))
;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Schema/AbstractColumnSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PDO;
use Yiisoft\Db\Command\Param;
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\Helper\StringHelper;
use Yiisoft\Db\Helper\DbStringHelper;

use function count;
use function gettype;
Expand Down Expand Up @@ -289,7 +289,7 @@ protected function typecast(mixed $value): mixed

if (is_float($value)) {
/** ensure type cast always has . as decimal separator in all locales */
return StringHelper::normalizeFloat($value);
return DbStringHelper::normalizeFloat($value);
}

if (is_bool($value)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Schema/Builder/AbstractColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Yiisoft\Db\Schema\Builder;

use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Helper\StringHelper;
use Yiisoft\Db\Helper\DbStringHelper;
use Yiisoft\Db\Schema\SchemaInterface;

use function gettype;
Expand Down Expand Up @@ -299,7 +299,7 @@ protected function buildDefaultValue(): string|null

return match (gettype($this->default)) {
'object', 'integer' => (string) $this->default,
'double' => StringHelper::normalizeFloat((string) $this->default),
'double' => DbStringHelper::normalizeFloat((string) $this->default),
'boolean' => $this->default ? 'TRUE' : 'FALSE',
default => "'$this->default'",
};
Expand Down
6 changes: 3 additions & 3 deletions tests/Common/CommonColumnSchemaBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use PHPUnit\Framework\TestCase;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Helper\UuidHelper;
use Yiisoft\Db\Helper\DbUuidHelper;
use Yiisoft\Db\Query\Query;
use Yiisoft\Db\Schema\SchemaInterface;
use Yiisoft\Db\Tests\Support\TestTrait;
Expand Down Expand Up @@ -56,7 +56,7 @@ public function testUuid(): void
if ($db->getDriverName() === 'oci') {
$uuidValue = new Expression('HEXTORAW(REGEXP_REPLACE(:uuid, \'-\', \'\'))', [':uuid' => $uuidValue]);
} elseif ($db->getDriverName() === 'mysql') {
$uuidValue = UuidHelper::uuidToBlob($uuidValue);
$uuidValue = DbUuidHelper::uuidToBlob($uuidValue);
}

$db->createCommand()->insert($tableName, [
Expand All @@ -71,7 +71,7 @@ public function testUuid(): void
->scalar()
;

$uuidString = strtolower(UuidHelper::toUuid($uuid));
$uuidString = strtolower(DbUuidHelper::toUuid($uuid));

$this->assertEquals($uuidSource, $uuidString);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@

use Closure;
use PHPUnit\Framework\TestCase;
use Yiisoft\Db\Helper\ArrayHelper;
use Yiisoft\Db\Helper\DbArrayHelper;

/**
* @group db
*/
final class ArrayHelperTest extends TestCase
final class DbArrayHelperTest extends TestCase
{
public function testIsAssociative(): void
{
$this->assertFalse(ArrayHelper::isAssociative([]));
$this->assertTrue(ArrayHelper::isAssociative(['test' => 1]));
$this->assertFalse(ArrayHelper::isAssociative([1]));
$this->assertFalse(DbArrayHelper::isAssociative([]));
$this->assertTrue(DbArrayHelper::isAssociative(['test' => 1]));
$this->assertFalse(DbArrayHelper::isAssociative([1]));
}

/**
* @dataProvider \Yiisoft\Db\Tests\Provider\PopulateProvider::populate
*/
public function testPopulate(array $rows): void
{
$this->assertSame($rows, ArrayHelper::populate($rows));
$this->assertSame($rows, DbArrayHelper::populate($rows));
}

/**
Expand All @@ -35,7 +35,7 @@ public function testPopulate(array $rows): void
*/
public function testPopulateWithIndexBy(Closure|string|null $indexBy, array $rows, array $populated): void
{
$this->assertSame($populated, ArrayHelper::populate($rows, $indexBy));
$this->assertSame($populated, DbArrayHelper::populate($rows, $indexBy));
}

/**
Expand All @@ -44,7 +44,7 @@ public function testPopulateWithIndexBy(Closure|string|null $indexBy, array $row
public function testPopulateWithIndexByWithObject(Closure|string|null $indexBy, array $rows, array $expectedPopulated): void
{
$rows = json_decode(json_encode($rows));
$populated = json_decode(json_encode(ArrayHelper::populate($rows, $indexBy)), true);
$populated = json_decode(json_encode(DbArrayHelper::populate($rows, $indexBy)), true);

$this->assertSame($expectedPopulated, $populated);
}
Expand All @@ -62,7 +62,7 @@ public function testPopulateWithIncorrectIndexByWithObject(Closure|string|null $

$this->expectExceptionMessageMatches('/^E_WARNING: /');

ArrayHelper::populate($rows, $indexBy);
DbArrayHelper::populate($rows, $indexBy);

restore_error_handler();
}
Expand Down
58 changes: 58 additions & 0 deletions tests/Db/Helper/DbStringHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Tests\Db\Helper;

use PHPUnit\Framework\TestCase;
use Yiisoft\Db\Helper\DbStringHelper;

/**
* @group db
*/
final class DbStringHelperTest extends TestCase
{
public function testBaseName(): void
{
$this->assertSame('TestCase', DbStringHelper::baseName('PHPUnit\Framework\TestCase'));
$this->assertSame('TestCase', DbStringHelper::baseName('TestCase'));
}

public static function pascalCaseToIdProvider(): array
{
return [
['photo\\album_controller', 'Photo\\AlbumController',],
['photo\\album\\controller', 'Photo\\Album\\Controller',],
['post_tag', 'PostTag',],
['post_tag', 'postTag'],
['foo_ybar', 'fooYBar',],
];
}

/**
* @dataProvider pascalCaseToIdProvider
*/
public function testPascalCaseToId(string $expectedResult, string $input): void
{
$this->assertEquals($expectedResult, DbStringHelper::pascalCaseToId($input));
}

public function testNormalizeFloat()
{
$this->assertSame('123', DbStringHelper::normalizeFloat(123));
$this->assertSame('-123', DbStringHelper::normalizeFloat(-123));
$this->assertSame('-2.5479E-70', DbStringHelper::normalizeFloat(-2.5479E-70));
$this->assertSame('2.5479E-70', DbStringHelper::normalizeFloat(2.5479E-70));
$this->assertSame('123.42', DbStringHelper::normalizeFloat(123.42));
$this->assertSame('-123.42', DbStringHelper::normalizeFloat(-123.42));
$this->assertSame('123.42', DbStringHelper::normalizeFloat('123.42'));
$this->assertSame('-123.42', DbStringHelper::normalizeFloat('-123.42'));
$this->assertSame('123.42', DbStringHelper::normalizeFloat('123,42'));
$this->assertSame('-123.42', DbStringHelper::normalizeFloat('-123,42'));
$this->assertSame('123123123.123', DbStringHelper::normalizeFloat('123.123.123,123'));
$this->assertSame('123123123.123', DbStringHelper::normalizeFloat('123,123,123.123'));
$this->assertSame('123123123.123', DbStringHelper::normalizeFloat('123 123 123,123'));
$this->assertSame('123123123.123', DbStringHelper::normalizeFloat('123 123 123.123'));
$this->assertSame('-123123123.123', DbStringHelper::normalizeFloat('-123 123 123.123'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@

use PHPUnit\Framework\TestCase;
use Yiisoft\Db\Exception\InvalidArgumentException;
use Yiisoft\Db\Helper\UuidHelper;
use Yiisoft\Db\Helper\DbUuidHelper;

/**
* @group db
*/
final class UuidHelperTest extends TestCase
final class DbUuidHelperTest extends TestCase
{
/**
* @dataProvider successUuids
*/
public function testConvert(string $uuid): void
{
$blobUuid = UuidHelper::uuidToBlob($uuid);
$this->assertEquals($uuid, UuidHelper::toUuid($blobUuid));
$blobUuid = DbUuidHelper::uuidToBlob($uuid);
$this->assertEquals($uuid, DbUuidHelper::toUuid($blobUuid));
}

/**
Expand All @@ -30,16 +30,16 @@ public function testToBlobIncorrectUuid(string $uuid): void
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Incorrect UUID.');

$blobUuid = UuidHelper::uuidToBlob($uuid);
$this->assertEquals($uuid, UuidHelper::toUuid($blobUuid));
$blobUuid = DbUuidHelper::uuidToBlob($uuid);
$this->assertEquals($uuid, DbUuidHelper::toUuid($blobUuid));
}

/**
* @dataProvider blobUuids
*/
public function testToUuid($blobUuid, $expected): void
{
$uuid = UuidHelper::toUuid($blobUuid);
$uuid = DbUuidHelper::toUuid($blobUuid);
$this->assertEquals($expected, $uuid);
}

Expand All @@ -51,7 +51,7 @@ public function testToUuidFailed($blobUuid, $expected): void
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Length of source data is should be 16 or 32 bytes.');

$uuid = UuidHelper::toUuid($blobUuid);
$uuid = DbUuidHelper::toUuid($blobUuid);
$this->assertEquals($expected, $uuid);
}

Expand Down
Loading

0 comments on commit a085501

Please sign in to comment.