Skip to content

Commit

Permalink
Merge pull request #32 from robmilward/feature/enum-trait
Browse files Browse the repository at this point in the history
Add Enum trait
  • Loading branch information
Rob Milward authored Nov 28, 2018
2 parents e704d8a + f1e69b2 commit 7375dc6
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
70 changes: 70 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ If you have never used the Composer dependency manager before, head to the [Comp
## Helpers
- [`Models`](#models)
- [`IsRelatedTo`](#isrelatedto)
- [`Enum`](#enum)
- [`ApiResponse`](#apiresponse)
- [`Response`](#response)
- [`ResponseCache`](#responsecache)
Expand Down Expand Up @@ -177,6 +178,75 @@ $related = $user->isRelatedTo($post)
| --- | ------- |
| Parameters | [Model](https://laravel.com/docs/eloquent) or [Array](http://php.net/manual/en/language.types.array.php) |
| Returns | [Boolean](http://php.net/manual/en/language.types.boolean.php) |

---

### `Enum`
The [Enum helper](src/LangleyFoxall/Helpers/Traits/Enum.php) is a trait that provides helpers for dealing with enum classes.

#### Methods

- [`all`](#all-1)
- [`valid`](#valid)

##### `all`
Return an array of all values.

###### Example usage
```
class UserType
{
use \LangleyFoxall\Helpers\Traits\Enum;
const ADMIN = 'admin';
const USER = 'user';
}
class User extends Eloquent
{
public function getValidTypes()
{
return UserType::all();
}
}
```

| Key | Details |
| --- | ------- |
| Parameters ||
| Returns | [Array](http://php.net/manual/en/language.types.array.php) |

##### `valid`
Check if a provided value is a valid value of the enum class.

###### Example usage
```
class UserType
{
use \LangleyFoxall\Helpers\Traits\Enum;
const ADMIN = 'admin';
const USER = 'user';
}
class User extends Eloquent
{
public function setTypeAttribute(string $type)
{
if (!UserType::valid($type)) {
throw new InvalidUserType;
}
$this->type = $type;
}
}
```

| Key | Details |
| --- | ------- |
| Parameters | [String](http://php.net/manual/en/language.types.array.php)|
| Returns | [Boolean](http://php.net/manual/en/language.types.boolean.php) |

---


Expand Down
35 changes: 35 additions & 0 deletions src/LangleyFoxall/Helpers/Traits/Enum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace LangleyFoxall\Helpers\Traits;

trait Enum
{
/**
* Returns an array of all constants defined within the class.
*
* @return array
*/
public static function all(): array
{
try {
$reflection = new \ReflectionClass(self::class);

return array_values($reflection->getConstants());
} catch (\ReflectionException $e) {
// This will never happen as the function can only ever be executed in a context where self::class is valid.
return [];
}
}

/**
* Returns a bool indicating whether or not a value is present in the class.
*
* @param string $value
*
* @return bool
*/
public static function valid(string $value): bool
{
return array_search($value, self::all()) !== false;
}
}

0 comments on commit 7375dc6

Please sign in to comment.