├── .php_cs.dist.php ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── composer.json └── src ├── LaravelHelpersServiceProvider.php └── helpers.php /.php_cs.dist.php: -------------------------------------------------------------------------------- 1 | in([ 5 | __DIR__ . '/src', 6 | __DIR__ . '/tests', 7 | ]) 8 | ->name('*.php') 9 | ->notName('*.blade.php') 10 | ->ignoreDotFiles(true) 11 | ->ignoreVCS(true); 12 | 13 | return (new PhpCsFixer\Config()) 14 | ->setRules([ 15 | '@PSR12' => true, 16 | 'array_syntax' => ['syntax' => 'short'], 17 | 'ordered_imports' => ['sort_algorithm' => 'alpha'], 18 | 'no_unused_imports' => true, 19 | 'not_operator_with_successor_space' => true, 20 | 'trailing_comma_in_multiline' => true, 21 | 'phpdoc_scalar' => true, 22 | 'unary_operator_spaces' => true, 23 | 'binary_operator_spaces' => true, 24 | 'blank_line_before_statement' => [ 25 | 'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'], 26 | ], 27 | 'phpdoc_single_line_var_spacing' => true, 28 | 'phpdoc_var_without_name' => true, 29 | 'class_attributes_separation' => [ 30 | 'elements' => [ 31 | 'method' => 'one', 32 | ], 33 | ], 34 | 'method_argument_space' => [ 35 | 'on_multiline' => 'ensure_fully_multiline', 36 | 'keep_multiple_spaces_after_comma' => true, 37 | ], 38 | 'single_trait_insert_per_statement' => true, 39 | ]) 40 | ->setFinder($finder); 41 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `laravel-helpers` will be documented in this file. 4 | 5 | ## 1.0.0 - 202X-XX-XX 6 | 7 | - initial release 8 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) ryangjchandler 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # A collection of helper functions that I use across my projects. 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/ryangjchandler/laravel-helpers.svg?style=flat-square)](https://packagist.org/packages/ryangjchandler/laravel-helpers) 4 | [![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/ryangjchandler/laravel-helpers/run-tests?label=tests)](https://github.com/ryangjchandler/laravel-helpers/actions?query=workflow%3Arun-tests+branch%3Amain) 5 | [![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/ryangjchandler/laravel-helpers/Check%20&%20fix%20styling?label=code%20style)](https://github.com/ryangjchandler/laravel-helpers/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain) 6 | [![Total Downloads](https://img.shields.io/packagist/dt/ryangjchandler/laravel-helpers.svg?style=flat-square)](https://packagist.org/packages/ryangjchandler/laravel-helpers) 7 | 8 | This package includes some of the helper functions that I tend to use in all of my projects. 9 | 10 | ## Installation 11 | 12 | You can install the package via composer: 13 | 14 | ```bash 15 | composer require ryangjchandler/laravel-helpers 16 | ``` 17 | 18 | ## Usage 19 | 20 | ### `user` 21 | 22 | Returns the current user, or null depending on authentication status. 23 | 24 | > This function assumes that your `User` model is found inside of `app/Models` and will not be registered if that class doesn't exist. 25 | 26 | ```php 27 | $user = user(); 28 | ``` 29 | 30 | ### `route_is` 31 | 32 | Check if the current route name matches the provided string. 33 | 34 | ```php 35 | route_is('dashboard.index'); 36 | ``` 37 | 38 | ### `authorize` 39 | 40 | Identical to Laravel's `$this->authorize()` method provided by the `AuthorizesRequests` trait. 41 | 42 | ```php 43 | public function index() 44 | { 45 | authorize('viewAny', Post::class); 46 | } 47 | ``` 48 | 49 | ### `attributes()` and `@attributes` 50 | 51 | Laravel 9 introduces new directives for checked, disabled and selected. In some cases though, you might want to output a variety of different attributes using PHP values. 52 | 53 | `attributes()` and the `@attributes()` directive can help with that: 54 | 55 | ```blade 56 | 60 | ``` 61 | 62 | ### `mdash()` 63 | 64 | It's quite common to output an `—` in your HTML code when it isn't present. Doing this with regular Blade `{{ }}` tags can be annoying though since `—` needs to be output in "raw" mode. 65 | 66 | This function uses `HtmlString` to return a "safe" wrapper around the HTML entity which allows it to be output without being escaped. 67 | 68 | ```blade 69 | {{ $post->published_at?->format('d/m/Y') ?? mdash() }} 70 | ``` 71 | 72 | ## Testing 73 | 74 | ```bash 75 | composer test 76 | ``` 77 | 78 | ## Changelog 79 | 80 | Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. 81 | 82 | ## Contributing 83 | 84 | Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details. 85 | 86 | ## Security Vulnerabilities 87 | 88 | Please review [our security policy](../../security/policy) on how to report security vulnerabilities. 89 | 90 | ## Credits 91 | 92 | - [Ryan Chandler](https://github.com/ryangjchandler) 93 | - [All Contributors](../../contributors) 94 | 95 | ## License 96 | 97 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 98 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ryangjchandler/laravel-helpers", 3 | "description": "A collection of helper functions that I use across my projects.", 4 | "keywords": [ 5 | "ryangjchandler", 6 | "laravel", 7 | "laravel-helpers" 8 | ], 9 | "homepage": "https://github.com/ryangjchandler/laravel-helpers", 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Ryan Chandler", 14 | "email": "support@ryangjchandler.co.uk", 15 | "role": "Developer" 16 | } 17 | ], 18 | "require": { 19 | "php": "^8.2", 20 | "spatie/laravel-package-tools": "^1.4.3", 21 | "illuminate/contracts": "^11.0" 22 | }, 23 | "require-dev": { 24 | "brianium/paratest": "^7.0", 25 | "nunomaduro/collision": "^8.1", 26 | "orchestra/testbench": "^9.4", 27 | "pestphp/pest": "^2.0", 28 | "phpunit/phpunit": "^10.0", 29 | "spatie/laravel-ray": "^1.23" 30 | }, 31 | "autoload": { 32 | "psr-4": { 33 | "RyanChandler\\LaravelHelpers\\": "src" 34 | }, 35 | "files": [ 36 | "src/helpers.php" 37 | ] 38 | }, 39 | "autoload-dev": { 40 | "psr-4": { 41 | "RyanChandler\\LaravelHelpers\\Tests\\": "tests" 42 | } 43 | }, 44 | "scripts": { 45 | "test": "./vendor/bin/pest" 46 | }, 47 | "config": { 48 | "sort-packages": true, 49 | "allow-plugins": { 50 | "pestphp/pest-plugin": true 51 | } 52 | }, 53 | "extra": { 54 | "laravel": { 55 | "providers": [ 56 | "RyanChandler\\LaravelHelpers\\LaravelHelpersServiceProvider" 57 | ], 58 | "aliases": { 59 | "LaravelHelpers": "RyanChandler\\LaravelHelpers\\LaravelHelpersFacade" 60 | } 61 | } 62 | }, 63 | "minimum-stability": "dev", 64 | "prefer-stable": true 65 | } 66 | -------------------------------------------------------------------------------- /src/LaravelHelpersServiceProvider.php: -------------------------------------------------------------------------------- 1 | name('laravel-helpers'); 15 | } 16 | 17 | public function packageBooted() 18 | { 19 | Blade::directive('attributes', static function (string $expression): string { 20 | return ""; 21 | }); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/helpers.php: -------------------------------------------------------------------------------- 1 | user(); 13 | } 14 | } 15 | 16 | if (! function_exists('route_is')) { 17 | function route_is(string $route): bool 18 | { 19 | return Route::currentRouteName() === $route; 20 | } 21 | } 22 | 23 | if (! function_exists('selected')) { 24 | function selected(bool $selected, string $attribute = 'selected'): ?string 25 | { 26 | if ($selected) { 27 | return $attribute; 28 | } 29 | 30 | return null; 31 | } 32 | } 33 | 34 | if (! function_exists('attributes')) { 35 | function attributes(array $attributes = []): ComponentAttributeBag 36 | { 37 | return new ComponentAttributeBag($attributes); 38 | } 39 | } 40 | 41 | if (! function_exists('authorize')) { 42 | /** 43 | * Authorize a given action for the current user. 44 | * 45 | * @throws \Illuminate\Auth\Access\AuthorizationException 46 | */ 47 | function authorize(mixed $ability, mixed $arguments = []): Response 48 | { 49 | static $class = null; 50 | 51 | if ($class === null) { 52 | $class = new class () { 53 | use AuthorizesRequests; 54 | }; 55 | } 56 | 57 | return $class->authorize($ability, $arguments); 58 | } 59 | } 60 | 61 | if (! function_exists('mdash')) { 62 | function mdash(): HtmlString 63 | { 64 | return new HtmlString('—'); 65 | } 66 | } 67 | --------------------------------------------------------------------------------