Comments (6)
Hey,
you've already posted the solution. Just put this into your own base Enum
class and extend this with all your enums.
namespace App\Enums;
abstract class Enum extends \Spatie\Enum\Enum
{
public static function toArray(): array
{
return array_combine(static::getNames(), static::getValues());
}
}
final class SaveStatus extends \App\Enums\Enum {}
from enum.
Hi! Thank you for answering.
I tried what you said, but I got error on initialization. Constructor's 3rd argument $index
must be an int
. Overriding it is not enough because getIndex()
must also return int
.
public function __construct(?string $name = null, ?string $value = null, ?int $index = null)
self::$cache[$class][$name]['index'] = static::make($name)->getIndex();
from enum.
Good point, right now we depend on the toArray()
method in resolve logic.
This should be changed. I think that this is changed by @brendt in v3 #56
For the moment you could add your own method to achieve what you need.
from enum.
@dionysiosarvanitis I've added a PR #58 that drops the usage of toArray()
in the Enum
class itself.
from enum.
https://github.com/spatie/enum/releases/tag/2.3.7
from enum.
Works great. Thanks!
from enum.
Related Issues (20)
- Enum serialized as empty array when using spatie/laravel-event-sourcing and spatie/data-transfer-object HOT 3
- Doc link is broken HOT 2
- Strong typed enums problems HOT 2
- Unit test overridden values HOT 3
- Enums with Laravel Octane HOT 2
- initialize Enum with values coming from DB? HOT 2
- PHPUnit assertions trait changed to class HOT 6
- Too few arguments to function Spatie\Enum\Enum::__construct(), 0 passed HOT 3
- OPcache strip comments problem HOT 2
- Missing documentation for ->isXy() HOT 1
- Deprecate values HOT 1
- Localised labels aren't update after locale is changed HOT 2
- can php7.2 (or even 7.3) be supported? HOT 2
- Extend already extended HOT 2
- Creating enum using method call from within enum throws UnknownEnumMethod HOT 6
- Consider using generics for better IDE support HOT 8
- Future of this package after PHP 8.1 HOT 7
- Support standard strict comparison HOT 5
- PHP8.1 Compatibility: check properties HOT 1
- Fix "Creating an enum from a value" example in documentation HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from enum.