Skip to content

Commit

Permalink
Rename Str methods
Browse files Browse the repository at this point in the history
  • Loading branch information
akalongman committed Sep 6, 2024
1 parent 808d9ba commit e5f89f7
Show file tree
Hide file tree
Showing 17 changed files with 88 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/Lodash/Commands/DbClear.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function handle(): void
foreach ($table as $value) {
$stm = 'DROP TABLE IF EXISTS `' . $value . '`';
if ($pretend) {
$this->line("{$stm}");
$this->line($stm);
} else {
$connection->statement($stm);
$this->comment('Table `' . $value . '` dropped');
Expand Down
14 changes: 8 additions & 6 deletions src/Lodash/Eloquent/ManyToManyPreload.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ public function scopeLimitPerGroupViaSubQuery(Builder $query, int $limit = 10):
$numAlias = $table . '_rn';

// Apply mysql variables
$newQuery->addSelect($connection->raw(
"@num := if(@group = {$this->quoteColumn($queryKeyColumn)}, @num+1, 1) as `{$numAlias}`, @group := {$this->quoteColumn($queryKeyColumn)} as `{$groupAlias}`",
));
$newQuery->addSelect(
$connection->raw(
"@num := if(@group = " . $this->quoteColumn($queryKeyColumn) . ", @num+1, 1) as `" . $numAlias . "`, @group := " . $this->quoteColumn($queryKeyColumn) . " as `" . $groupAlias . "`",
),
);

// Make sure first order clause is the group order
$newQuery->getQuery()->orders = (array) $query->getQuery()->orders;
Expand All @@ -73,14 +75,14 @@ public function scopeLimitPerGroupViaSubQuery(Builder $query, int $limit = 10):

if ($join) {
$leftKey = explode('.', $queryKeyColumn)[1];
$leftKeyColumn = "`{$table}`.`{$leftKey}`";
$leftKeyColumn = "`" . $table . "`.`" . $leftKey . "`";
$newQuery->addSelect($queryKeyColumn);
$newQuery->mergeBindings($query->getQuery());
$newQuery->getQuery()->joins = (array) $query->getQuery()->joins;
$query->whereRaw("{$leftKeyColumn} = {$this->quoteColumn($queryKeyColumn)}");
$query->whereRaw($leftKeyColumn . ' = ' . $this->quoteColumn($queryKeyColumn));
}

$query->from($connection->raw("({$newQuery->toSql()}) as `{$table}`"))
$query->from($connection->raw("(" . $newQuery->toSql() . ") as `" . $table . "`"))
->where($numAlias, '<=', $limit);

return $this;
Expand Down
4 changes: 2 additions & 2 deletions src/Lodash/Http/Resources/TransformsData.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ private static function parseKeyValue(string $internalField, $transformValue, Tr
}
} else {
// Try to find getter for external field
$method = 'get' . Str::snakeCaseToCamelCase($transformValue);
$method = 'get' . Str::snakeCaseToPascalCase($transformValue);
if (method_exists($model, $method)) {
$key = $transformValue;
$value = $model->$method();
} else {
// Call getter for internal field
$method = 'get' . Str::snakeCaseToCamelCase($internalField);
$method = 'get' . Str::snakeCaseToPascalCase($internalField);
if (! method_exists($model, $method)) {
throw new LogicException('Field ' . $internalField . ' getter (' . $method . ') does not available for model ' . $model::class);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Lodash/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ protected function registerBladeDirectives(): void

// Display relative time
app('blade.compiler')->directive('datetime', static function ($expression) {
return "<?php echo '<time datetime=\'' . with({$expression})->toIso8601String()
return "<?php echo '<time datetime=\'' . with($expression)->toIso8601String()
. '\' title=\'' . $expression . '\'>'
. with({$expression})->diffForHumans() . '</time>' ?>";
. with($expression)->diffForHumans() . '</time>' ?>";
});

// Pluralization helper
Expand Down
17 changes: 16 additions & 1 deletion src/Lodash/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use function is_array;
use function is_null;
use function is_object;
use function lcfirst;
use function max;
use function mb_substr;
use function number_format;
Expand Down Expand Up @@ -53,13 +54,27 @@ public static function formatBalance(?int $amount = 0, int $d = 2): string
return $amount;
}

public static function snakeCaseToCamelCase(string $string): string
public static function snakeCaseToPascalCase(string $string): string
{
$str = str_replace('_', '', ucwords($string, '_'));

return $str;
}

public static function pascalCaseToSnakeCase(string $string): string
{
$string = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $string));

return $string;
}

public static function snakeCaseToCamelCase(string $string): string
{
$str = lcfirst(str_replace('_', '', ucwords($string, '_')));

return $str;
}

public static function camelCaseToSnakeCase(string $string): string
{
$string = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $string));
Expand Down
5 changes: 3 additions & 2 deletions tests/Unit/Eloquent/UsesUuidAsPrimaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
namespace Tests\Unit\Eloquent;

use Longman\LaravelLodash\Eloquent\UsesUuidAsPrimary;
use PHPUnit\Framework\Attributes\Test;
use Ramsey\Uuid\Uuid;
use Tests\Unit\TestCase;

class UsesUuidAsPrimaryTest extends TestCase
{
/** @test */
#[Test]
public function it_should_check_uuid_is_valid_binary(): void
{
$mock = $this->getMockForTrait(UsesUuidAsPrimary::class);
Expand All @@ -22,7 +23,7 @@ public function it_should_check_uuid_is_valid_binary(): void
$this->assertTrue($mock->isUuidBinary($uuidBinary));
}

/** @test */
#[Test]
public function it_should_check_uuid_is_invalid_binary(): void
{
$mock = $this->getMockForTrait(UsesUuidAsPrimary::class);
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Http/Requests/RestrictsExtraAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

class RestrictsExtraAttributesTest extends TestCase
{
#[Test()]
#[Test]
#[DataProvider('provideData')]
public function it_should_throw_validation_error_on_extra_arguments(
array $rules,
Expand Down
5 changes: 3 additions & 2 deletions tests/Unit/Http/Resources/ArrayResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Longman\LaravelLodash\Http\Resources\ArrayResource;
use PHPUnit\Framework\Attributes\Test;
use Tests\Unit\TestCase;

use function app;

class ArrayResourceTest extends TestCase
{
/** @test */
#[Test]
public function it_should_transform_array(): void
{
$resource = new ArrayResource([
Expand All @@ -31,7 +32,7 @@ public function it_should_transform_array(): void
$this->assertSame($expected, $response->content());
}

/** @test */
#[Test]
public function it_should_return_unecaped_json(): void
{
$resource = new ArrayResource([
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/Http/Resources/ErrorResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Longman\LaravelLodash\Http\Resources\ErrorResource;
use PHPUnit\Framework\Attributes\Test;
use Tests\Unit\TestCase;

use function app;

class ErrorResourceTest extends TestCase
{
/** @test */
#[Test]
public function it_should_transform_array(): void
{
$resource = new ErrorResource([
Expand Down
9 changes: 5 additions & 4 deletions tests/Unit/Http/Resources/JsonResourceCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Support\Collection;
use Longman\LaravelLodash\Http\Resources\ArrayResource;
use Longman\LaravelLodash\Http\Resources\JsonResourceCollection;
use PHPUnit\Framework\Attributes\Test;
use Tests\Unit\TestCase;

use function app;
Expand All @@ -19,7 +20,7 @@ class JsonResourceCollectionTest extends TestCase
{
use WithFaker;

/** @test */
#[Test]
public function it_should_transform_collection_of_arrays(): void
{
$collection = new Collection(array_fill(0, 3, [
Expand All @@ -38,7 +39,7 @@ public function it_should_transform_collection_of_arrays(): void
$this->assertSame($expected, $response->content());
}

/** @test */
#[Test]
public function it_should_transform_collection_of_models(): void
{
$models = [];
Expand Down Expand Up @@ -78,7 +79,7 @@ public function it_should_transform_collection_of_models(): void
], $transformed);
}

/** @test */
#[Test]
public function it_should_transform_collection_of_models_with_hidden_properties(): void
{
$models = [];
Expand Down Expand Up @@ -120,7 +121,7 @@ public function it_should_transform_collection_of_models_with_hidden_properties(
], $transformed);
}

/** @test */
#[Test]
public function it_should_transform_collection_of_models_with_hidden_properties_in_output(): void
{
$models = [];
Expand Down
9 changes: 5 additions & 4 deletions tests/Unit/Middleware/SimpleBasicAuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
namespace Tests\Unit\Middleware;

use Longman\LaravelLodash\Middlewares\SimpleBasicAuth;
use PHPUnit\Framework\Attributes\Test;
use Tests\Unit\TestCase;

use function config;

class SimpleBasicAuthTest extends TestCase
{
/** @test */
#[Test]
public function it_should_return_access_denied_on_empty_credentials()
{
config()->set('auth.simple', [
Expand All @@ -24,7 +25,7 @@ public function it_should_return_access_denied_on_empty_credentials()
$response->assertStatus(401);
}

/** @test */
#[Test]
public function it_should_return_access_denied_on_wrong_credentials()
{
config()->set('auth.simple', [
Expand All @@ -37,7 +38,7 @@ public function it_should_return_access_denied_on_wrong_credentials()
$response->assertStatus(401);
}

/** @test */
#[Test]
public function it_should_return_ok_on_disabled_auth()
{
config()->set('auth.simple', [
Expand All @@ -51,7 +52,7 @@ public function it_should_return_ok_on_disabled_auth()
$response->assertSeeText('ok');
}

/** @test */
#[Test]
public function it_should_return_ok_with_credentials()
{
config()->set('auth.simple', [
Expand Down
5 changes: 3 additions & 2 deletions tests/Unit/RedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Tests\Unit;

use Longman\LaravelLodash\Redis\RedisManager;
use PHPUnit\Framework\Attributes\Test;
use Redis;
use RedisArray;

Expand All @@ -13,7 +14,7 @@

class RedisTest extends TestCase
{
/** @test */
#[Test]
public function it_should_set_custom_serializer()
{
$redis = $this->createConnection('phpredis', [
Expand Down Expand Up @@ -42,7 +43,7 @@ public function it_should_set_custom_serializer()
$this->assertEquals($redis->get('country'), $data);
}

/** @test */
#[Test]
public function it_should_set_custom_serializer_for_cluster()
{
$redis = $this->createConnection('phpredis', [
Expand Down
7 changes: 4 additions & 3 deletions tests/Unit/ServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@

use Illuminate\Contracts\Console\Kernel;
use Illuminate\Http\Request;
use PHPUnit\Framework\Attributes\Test;

use function array_keys;

class ServiceProviderTest extends TestCase
{
/** @test */
#[Test]
public function check_if_commands_registered()
{
$commands = [
Expand All @@ -31,7 +32,7 @@ public function check_if_commands_registered()
}
}

/** @test */
#[Test]
public function check_if_request_has_macros()
{
$this->assertTrue(Request::hasMacro('getInt'));
Expand All @@ -40,7 +41,7 @@ public function check_if_request_has_macros()
$this->assertTrue(Request::hasMacro('getString'));
}

/** @test */
#[Test]
public function check_blade_directives()
{
$directives = [
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Support/DeclensionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class DeclensionsTest extends TestCase
{
#[Test()]
#[Test]
#[DataProvider('dataForDeclensions')]
public function turn_words_declensions(string $word, array $declensions): void
{
Expand Down
Loading

0 comments on commit e5f89f7

Please sign in to comment.