Skip to content

Commit

Permalink
- Updating documentation.
Browse files Browse the repository at this point in the history
- Adding new functions like array_wrap().
- Complying with latest version of php function notations.
- Updating composer.
  • Loading branch information
hicham-saddek committed Apr 6, 2021
1 parent 82bec48 commit e3d5930
Show file tree
Hide file tree
Showing 4 changed files with 346 additions and 300 deletions.
110 changes: 67 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,61 +1,85 @@
# Milebits Helpers

A new laravel package including helper functions for every Milebits package.

# How to install
```

```bash
composer require milebits/helpers
```

# Available methods
#### Check if a constant exists within a class:
```
constExists($class, $constant): bool
```
- Example: `constExists(App\Models\User::class, "CREATED_AT")` will result in `true`
- Example: `constExists($user, "CREATED_AT")` will result in `true`
- Example: `constExists(App\Models\User::class, "RANDOM_VAR")` will result in `false`
- Example: `constExists($user, "RANDOM_VAR")` will result in `false`
#### Get the value of a constant or get default:
```
constVal($class, $constant, $default): mixed
```
- Example: `constVal(App\Models\User::class, "CREATED_AT", "JAJA")` will result in `created_at`
- Example: `constVal($user, "CREATED_AT", "JAJA")` will result in `created_at`
- Example: `constVal(App\Models\User::class, "RANDOM_VAR", "JAJA")` will result in `JAJA`
- Example: `constVal($user, "RANDOM_VAR", "JAJA")` will result in `JAJA`
- Example: `constVal(App\Models\User::class, "RANDOM_VAR")` will result in `null`
- Example: `constVal($user, "RANDOM_VAR")` will result in `null`
#### Get the value of a static property or get default:

#### Check if a constant exists within a class:

```php
use Illuminate\Support\Facades\Auth;
use function Milebits\Helpers\Helpers\constExists;

$exists = constExists("App\Models\User", 'constant');
$exists = constExists(Auth::user(), 'constant');
```
staticPropVal($class, $name, $default): mixed

#### Get the value of a constant or get default:

```php
use Illuminate\Support\Facades\Auth;
use function Milebits\Helpers\Helpers\constVal;

$value = constVal(Auth::user(), 'CONSTANT', 'default Value, can be anything');
$value = constVal("App\Models\User", 'CONSTANT', 'default Value, can be anything');
```
- Example: `staticPropVal(App\Models\User::class, "STATICPROP", "JAJA")` will result in the value of the property requested.
- Example: `staticPropVal($user, "STATICPROP", "JAJA")` will result in the value of the property requested.
- Example: `staticPropVal(App\Models\User::class, "NONSTATIC", "JAJA")` will result in `JAJA`
- Example: `staticPropVal($user, "NONSTATIC", "JAJA")` will result in `JAJA`
- Example: `staticPropVal(App\Models\User::class, "NONSTATIC")` will result in `null`
- Example: `staticPropVal($user, "NONSTATIC")` will result in `null`

#### Get the value of a property or get default:

#### Get the value of a static property or get default:

```php
use Illuminate\Support\Facades\Auth;
use function Milebits\Helpers\Helpers\staticPropVal;

$value = staticPropVal(Auth::user(), "StaticProp", 'default value');
$value = staticPropVal("App\Models\User", "StaticProp", 'default value');
```
propVal($class, $name, $default): mixed

#### Get the value of a property or get default:

```php
use Illuminate\Support\Facades\Auth;
use function Milebits\Helpers\Helpers\propVal;

$value = propVal(Auth::user(), 'Property', 'default value');
$value = propVal("App\Models\User", 'Property', 'default value');
```
- Example: `propVal(App\Models\User::class, "prop", "JAJA")` will result in the value of the property requested.
- Example: `propVal($user, "prop", "JAJA")` will result in the value of the property requested.
- Example: `propVal(App\Models\User::class, "non_prop", "JAJA")` will result in `JAJA`
- Example: `propVal($user, "non_prop", "JAJA")` will result in `JAJA`
- Example: `propVal(App\Models\User::class, "non_prop")` will result in `null`
- Example: `propVal($user, "non_prop")` will result in `null`

#### Check if a class has a certain trait

```php
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Auth;
use function Milebits\Helpers\Helpers\hasTrait;

$hasSoftDeletes = hasTrait(Auth::user(), SoftDeletes::class);
$hasSoftDeletes = hasTrait("App\Models\User", SoftDeletes::class);
```
hasTrait($object, $trait): bool

#### Wrap something in an array

```php
use function Milebits\Helpers\Helpers\array_wrap;

$value = "test";
$array = ["arrayItem"];

$wrap = array_wrap($value);
$wrap = array_wrap($array);
```
- Example: `hasTrait($user, SoftDeletes::class)` will return true if the user has the SoftDeletes Trait.
- Example: `hasTrait($user, HasPosts::class)` will return true if the user hasn't got the HasPosts Trait
- Example: `hasTrait(App\Models\User, SoftDeletes::class)` will return true if the user class has the SoftDeletes Trait.
- Example: `hasTrait(App\Models\User, HasPosts::class)` will return true if the user class hasn't got the HasPosts Trait.

# Contributions
If in any case while using this package, and you which to request a new functionality to it, please contact us at [email protected] and mention the package you are willing to contribute or suggest a new functionality.

If in any case while using this package, and you which to request a new functionality to it, please contact us at
[email protected] and mention the package you are willing to contribute or suggest a new functionality.

# Vulnerabilities
If in any case while using this package, you encounter security issues or security vulnerabilities, please do report them as soon as possible by issuing an issue here in Github or by sending an email to [email protected] with the mention **Vulnerability Report milebits/helpers** as your subject.

If in any case while using this package, you encounter security issues or security vulnerabilities, please do report
them as soon as possible by issuing an issue here in Github or by sending an email to [email protected] with the
mention **Vulnerability Report milebits/helpers** as your subject.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
}
],
"require": {
"php": ">=7.4",
"php": ">=8.0",
"laravel/framework": "^6|^7|^8"
},
"require-dev": {
Expand Down
Loading

0 comments on commit e3d5930

Please sign in to comment.