定制 erikaraujo/php-enhanced-enums 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

erikaraujo/php-enhanced-enums

最新稳定版本:2.0.0

Composer 安装命令:

composer require erikaraujo/php-enhanced-enums

包简介

Enhanced enum methods for PHP enums

README 文档

README

Enhanced Enum methods for PHP Enums

In this package, we have 4 traits: EnhancedEnum, HasDescription, HasLabel and IsSelectArray. Here are all the methods that each trait provides:

EnhancedEnum

parse()

This method is simply an alias to the native from() method. Here's an example:

Suit::parse('hearts'); // Returns the Suit::Hearts case

Suit::parse('invalid'); // Throws an exception

tryParse()

This method is an improved alias to the tryFrom() method. The difference here, is that, instead of only having the $value parameter, it also has a $ignoreCase boolean parameter to allow the method to ignore the case of the value before parsing it. Here's an example:

Suit::tryParse('hearts'); // Returns the Suit::Hearts case

Suit::tryParse('invalid'); // Returns null

Suit::tryParse('hEaRtS'); // Returns null

Suit::tryParse('hEaRtS', ignoreCase: true); // Returns the Suit::Hearts case

tryFromIgnoringCase()

// TODO

tryParseIgnoringCase()

An alias to tryFromIgnoreCase().

getNames()

This method returns an array with all the names of the enum cases. Here's an example:

Suit::getNames(); // Returns ['Hearts', 'Diamonds', 'Clubs', 'Spades']

getValues()

This method returns an array with all the values of the enum cases. Here's an example:

Suit::getValues(); // Returns ['hearts', 'diamonds', 'clubs', 'spades']

isDefined()

This method checks if a given value is defined in the enum. Here's an example:

Suit::isDefined('hearts'); // Returns true

Suit::isDefined('invalid'); // Returns false

Suit::isDefined('hEaRtS'); // Returns false

Suit::isDefined('hEaRtS', ignoreCase: true); // Returns true

toString()

This method returns the value of the enum as string - even if it's an integer enum.

is()

TODO: describe

Suit::Hearts->is(Suit::Spades); // Returns `false`
Suit::Hearts->is('spades'); // Returns `false`

Suit::Hearts->is(Suit::Hearts); // Returns `true`
Suit::Hearts->is('hearts'); // Returns `true`
Suit::Hearts->is('HeArTs'); // Returns `false`
Suit::Hearts->is('HeArTs', ignoreCase: true); // Returns `true`

getBackingType()

// TODO: describe

Suits::getBackingType(); // returns the `ReflectionNamedType`. In this case, with `string` as a `name`.

getUnderlyingType()

An alias to getBackingType()

isIntEnum()

Returns if the enum's backing type is an integer.

Suit::isIntEnum(); // Returns false

isStringEnum()

Returns if the enum's backing type is a string.

Suit::isStringEnum(); // Returns true

getRandom()

Returns a random enum case.

getCaseByPosition()


tryGetCaseByPosition()


first()


last()


HasDescription

getDescription()

Returns the description of the given enum

Suit::Hearts->getDescription(); // Returns "The suit of hearts"

getDescriptions()

Returns an array of all the descriptions

Suit::getDescriptions();

tryFromDescription()


tryFromDescriptionIgnoringCase()


HasLabel

getLabel()


getLabels()


tryFromLabel()


tryFromLabelIgnoringCase()


shouldAutoGenerateLabelFromValue()

protected


IsSelectArray

asSelectArray()



                                    

统计信息

  • 总下载量: 13.46k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-05-07