fsmdev / constants-collection
Constants collection class
Requires
- php: ^7.1.0
This package is auto-updated.
Last update: 2025-03-14 13:48:14 UTC
README
A simple PHP class that allows you to create collections of constants, assign them properties, get arrays of properties and constants.
Installation
composer require fsmdev/constants-collection
Usage
Constants collection
Inherit your class from Fsmdev\ConstantsCollection\ConstantsCollection and define constants.
use Fsmdev\ConstantsCollection\ConstantsCollection; class PostStatus extends ConstantsCollection { const EDITED = 1; const PUBLISHED = 2; const DELETED = 3; }
Examples:
$post = new Post(); $post->status = PostStatus::PUBLISHED;
class Post { public function isPublished() { return $this->status == PostStatus::PUBLISHED; } }
To get an array of constants use the method valuesArray
valuesArray () : array
Properties
Named properties can be set for each constant. To do this, it is necessary to define a function in the class that is built according to the mask: properties + PropertyName (camel case). The method must return an array whose keys are the values of the constants, and the values are the values of the properties.
# class PostStatus protected static function propertiesName() { return [ self::EDITED => 'Edited', self::PUBLISHED => 'Published', self::DELETED => 'Deleted', ]; } protected static function propertiesIndicatorClass() { return [ self::EDITED => 'text-warning', self::PUBLISHED => 'text-success', self::DELETED => 'text-danger', ]; }
Properties can be obtained using the static method property
property ( mixed $value [, string $property = 'name' ] ) : mixed
Example (Blade):
Status: <span class="{{ PostStatus::property($post->status, 'indicator_class') }}"> {{ PostStatus::property($post->status) }} </span>
You can get an array of properties using the static method propertiesArray
propertiesArray ( [ string $property = 'name' ] ) : array
Getting value by property
value ( mixed $value [, string $property = 'name' ] ) : mixed
$status = PostStatus::value('Edited');