Skip to content

Commit

Permalink
feat: string reverse helper
Browse files Browse the repository at this point in the history
  • Loading branch information
craigAtCD committed Jul 12, 2021
1 parent 9b9cdef commit 06b03ef
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Collection of helpers for re-use accross a few of our projects
- [Helpers](#helpers)
- [Delayed notifications blocking:](#delayed-notifications-blocking)
- [DB Macros](#db-macros)
- [String Macros](#string-macros)
- [Credits](#credits)

## Installation
Expand Down Expand Up @@ -115,6 +116,9 @@ Model::whereNullOrEmpty('column_name'); //generates select * where 1=1 and (colu
Model::orWhereNullOrEmpty('column_name'); //generates select * where 1=1 or (column_name is null or column_name = '')
Model::whereNotNullOrEmpty('column_name'); //generates select * where 1=1 and (column_name is not null and column_name != '')
Model::orWhereNotNullOrEmpty('column_name'); //generates select * where 1=1 or (column_name is not null and column_name != '')
```
## String Macros
`Str::reverse(string)` - to safely reverse a string that is multibyte safe.

## Credits

Expand Down
11 changes: 11 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CustomD\LaravelHelpers;

use Illuminate\Support\Str;
use Illuminate\Support\Facades\Event;
use Illuminate\Database\Query\Builder;
use CustomD\LaravelHelpers\Database\Query\Mixins\NullOrEmptyMixin;
Expand All @@ -15,6 +16,7 @@ public function boot()
{
$this->registerEvents();
$this->registerDbMacros();
$this->registerStringMacros();
}

public function register()
Expand All @@ -34,4 +36,13 @@ protected function registerDbMacros()
{
Builder::mixin(new NullOrEmptyMixin());
}

protected function registerStringMacros()
{
/** @macro \Illuminate\Support\Str */
Str::macro('reverse', function ($string, $encoding = null) {
$chars = mb_str_split($string, 1, $encoding ?? mb_internal_encoding());
return implode('', array_reverse($chars));
});
}
}
34 changes: 34 additions & 0 deletions tests/StringMacrosTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace CustomD\LaravelHelpers\Tests;

use Illuminate\Support\Str;
use Orchestra\Testbench\TestCase;
use CustomD\LaravelHelpers\ServiceProvider;
use CustomD\LaravelHelpers\Facades\LaravelHelpers;

class StringMacrosTest extends TestCase
{
protected function getPackageProviders($app)
{
return [ServiceProvider::class];
}

protected function getPackageAliases($app)
{
return [
'laravel-helpers' => LaravelHelpers::class,
];
}

public function testReverseStringMacro()
{
$str = "hello World";
$rev = 'dlroW olleh';
$this->assertEquals($rev, Str::reverse($str));

$encodedStr = 'te reo māori';
$encodedRev = 'iroām oer et';
$this->assertEquals($encodedRev, Str::reverse($encodedStr));
}
}

0 comments on commit 06b03ef

Please sign in to comment.