Skip to content

Commit

Permalink
🔖 Release 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
koenhendriks committed Mar 30, 2023
0 parents commit ac67456
Show file tree
Hide file tree
Showing 13 changed files with 369 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
root = true

[*]
charset = utf-8
indent_size = 4
indent_style = space
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.{yml,yaml}]
indent_size = 2
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Please see the documentation for all configuration options:
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:

- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
labels:
- "dependencies"
32 changes: 32 additions & 0 deletions .github/workflows/dependabot-auto-merge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: dependabot-auto-merge
on: pull_request_target

permissions:
pull-requests: write
contents: write

jobs:
dependabot:
runs-on: ubuntu-latest
if: ${{ github.actor == 'dependabot[bot]' }}
steps:

- name: Dependabot metadata
id: metadata
uses: dependabot/[email protected]
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"

- name: Auto-merge Dependabot PRs for semver-minor updates
if: ${{steps.metadata.outputs.update-type == 'version-update:semver-minor'}}
run: gh pr merge --auto --merge "$PR_URL"
env:
PR_URL: ${{github.event.pull_request.html_url}}
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}

- name: Auto-merge Dependabot PRs for semver-patch updates
if: ${{steps.metadata.outputs.update-type == 'version-update:semver-patch'}}
run: gh pr merge --auto --merge "$PR_URL"
env:
PR_URL: ${{github.event.pull_request.html_url}}
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
48 changes: 48 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: run-tests

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest]
php: [ 8.1 ]
laravel: [10.*]
stability: [prefer-stable]
include:
- laravel: 10.*
testbench: 8.*

name: Php${{ matrix.php }} - Laravel${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
coverage: none

- name: Setup problem matchers
run: |
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
- name: Execute tests
run: composer test

15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.idea
.php_cs
.php_cs.cache
.phpunit.result.cache
.phpunit.cache
build
composer.lock
coverage
docs
phpstan.neon
testbench.yaml
vendor
node_modules
.php-cs-fixer.cache
*.cache
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Changelog

All notable changes to `LaravelStrAcronym` will be documented in this file.

## 1.0.0 - 2023-03-29

- Initial release
25 changes: 25 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
The MIT License (MIT)
=====================

Copyright © koenhendriks <[email protected]>

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the “Software”), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
73 changes: 73 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Laravel Acronym Package

This package provides a macro for generating acronyms from strings in Laravel projects using the `Str` helper and supports the `Stringable` class.

It originates from [this Pull Request](https://github.com/laravel/framework/pull/46615)

## Installation

You can install the package using Composer:

`composer require koenhendriks/laravel-str-acronym`

After installing the package Laravel should automatically discover the package.

If you want you can register the service provider in your Laravel application manually. You can do this by adding the service provider to the `providers` array in your `config/app.php` file:

```php
'providers' => [
KoenHendriks\StrAcronym\StrServiceProvider::class,
],
```
Once you've registered the service provider, you can start using the acronym macro in your Laravel application.

## Usage
To generate an acronym from a string, you can call the acronym method on the Str helper:

```php
use Illuminate\Support\Str;

$acronym = Str::acronym('Hello World'); // Returns 'HW'
```

If you prefer to use the Fluent Strings, you can call the acronym method on a Stringable object:

```php
use Illuminate\Support\Str;

$acronym = Str::of('hello world')->headline()->acronym(); // Returns 'HW'
```

You can also provide a delimiter string as an optional parameter to separate the acronym letters:

```php
use Illuminate\Support\Str;

$acronym = Str::acronym('Hello World', '.'); // Returns 'H.W.'
$acronym = Str::of('hello world')->headline()->acronym(); // Returns 'H.W.'

```

## Testing

This package is using PhpUnit to unit test the macros. A simple alias has been created with composer to run the tests.

```bash
composer test
```

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

## Credits

- [Koen Hendriks](https://github.com/koenhendriks)
- [All Contributors](../../contributors)


## License
This package is licensed under the MIT License. See the LICENSE file for more information.

## Contributing
If you find any issues with the package or have suggestions for improvements, feel free to open an issue or pull request on the GitHub repository.
43 changes: 43 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "koenhendriks/laravel-str-acronym",
"description": "A package to be able to generate acronyms from strings in Laravel projects using the Str helper and supports the Stringable class.",
"type": "library",
"license": "MIT",
"keywords": [
"koenhendriks",
"laravel",
"acronym",
"str-acronym",
"laravel-str-acronym"
],
"homepage": "https://github.com/koenhendriks/laravel-str-acronym",
"autoload": {
"psr-4": {
"KoenHendriks\\StrAcronym\\": "src/"
}
},
"authors": [
{
"name": "Koen Hendriks",
"email": "[email protected]",
"role": "Developer"
}
],
"require": {
"illuminate/support": "^10.5"
},
"extra": {
"laravel": {
"providers": [
"KoenHendriks\\StrAcronym\\StrServiceProvider"
]
}
},
"scripts": {
"test": "./vendor/bin/phpunit"
},
"require-dev": {
"orchestra/testbench": "^8.1",
"phpunit/phpunit": "^10.0"
}
}
17 changes: 17 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
executionOrder="random"
failOnWarning="true"
failOnRisky="true"
failOnEmptyTestSuite="true"
beStrictAboutOutputDuringTests="true"
cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="UnitLocal">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
</phpunit>
32 changes: 32 additions & 0 deletions src/StrServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace KoenHendriks\StrAcronym;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;

class StrServiceProvider extends ServiceProvider
{
public function boot()
{
Str::macro('acronym', function ($string, $delimiter = '') {
if (empty($string)) {
return '';
}

$acronym = '';
foreach (preg_split('/[^a-zA-Z]+/', $string) as $word) {
$acronym .= $word[0] . $delimiter;
}

return $acronym;
});

Stringable::macro('acronym', function (string $delimiter = '') {
return new Stringable (Str::acronym($this->value, $delimiter));
});
}
}
24 changes: 24 additions & 0 deletions tests/AcronymStrTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use Illuminate\Support\Str;
use KoenHendriks\StrAcronym\StrServiceProvider;
use Orchestra\Testbench\TestCase;

class AcronymStrTest extends TestCase
{
protected function getPackageProviders($app)
{
return [
StrServiceProvider::class,
];
}

public function testAcronym()
{
$this->assertSame('lidsa', Str::acronym('lorem ipsum dolor sit amet'));
$this->assertSame('Life', Str::acronym('Laravel is freaking everywhere'));
$this->assertSame('fB', Str::acronym('foo Bar'));
$this->assertSame('lpf', Str::acronym("laravel\t\tphp\n\nframework"));
$this->assertSame('HW', (string) Str::of('hello world')->headline()->acronym());
}
}
26 changes: 26 additions & 0 deletions tests/AcronymStringableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use KoenHendriks\StrAcronym\StrServiceProvider;
use Orchestra\Testbench\TestCase;
use Illuminate\Support\Stringable;

class AcronymStringableTest extends TestCase
{
protected function getPackageProviders($app)
{
return [
StrServiceProvider::class,
];
}

protected function stringable(string $string = ''): Stringable
{
return new Stringable($string);
}

public function testAcronym()
{
$this->assertSame('lidsa', (string) $this->stringable('lorem ipsum dolor sit amet')->acronym());
$this->assertSame('l.i.d.s.a', (string) $this->stringable('lorem ipsum dolor sit amet')->acronym('.')->rtrim('.'));
}
}

0 comments on commit ac67456

Please sign in to comment.