Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ final readonly class ByteUnitHex extends EnumCase {
$units = EnumCollection::of(ByteUnit::class);

$decimal = $units->wrapCasesAs(ByteUnitDecimal::from(...));
$hex = $units->wrapCasesAs(ByteUnitHex::from(...));
$hex = $units->wrapCasesAs(ByteUnitHex::from(...));

$decimal->fromName('MEBIBYTE')->format(); // '1,048,576'
$hex->fromName('MEBIBYTE')->format(); // '0x100000'
Expand Down Expand Up @@ -505,7 +505,7 @@ use Notonza\PhpEnumEx\EnumCollection;

$weights = EnumCollection::of(AirmailWeight::class);

$asia = $weights->wrapCasesAs(AsiaAirmailRate::from(...));
$asia = $weights->wrapCasesAs(AsiaAirmailRate::from(...));
$europe = $weights->wrapCasesAs(EuropeAirmailRate::from(...));

calculatePostage($asia->fromValue(25)); // 90
Expand Down Expand Up @@ -956,10 +956,6 @@ enum FilePermission: int {
case WRITE = 1 << 1;
case EXECUTE = 1 << 2;

public function isGrantedBy(int $mask): bool {
return ($mask & $this->value) === $this->value;
}

/** @return array<string, self> */
public static function granted(int $mask): array {
return self::filter(
Expand All @@ -975,6 +971,10 @@ enum FilePermission: int {
=> $permission->isGrantedBy($mask),
);
}

public function isGrantedBy(int $mask): bool {
return ($mask & $this->value) === $this->value;
}
}

$mask = FilePermission::READ->value
Expand Down
8 changes: 8 additions & 0 deletions docs/coding-style.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ final readonly class CaseLabel implements Stringable {

The example illustrates method placement only; public API documentation is omitted for brevity. The ordering is a default for new and revised code, not a requirement to rearrange unrelated existing methods. The placement of trait declarations, constants, properties, non-factory static methods, protected methods, private helpers, abstract methods, interface implementations, and enum cases is not yet prescribed by a complete ordering rule. Preserve meaningful existing organization for these members. Do not let a formatter determine their semantic placement.

## Aliases and Projections

A forwarding alias exposes the same operation under another name. Keep it immediately after the original method (group multiple aliases together), even when a mechanical sort would separate them. Describe the alias and reference the original with `@see`; do not duplicate the original Example section. Retain type information, generic return shapes, exception contracts and any meaningful alias-specific notes.

A projection transforms the result of another operation and provides a distinct contract. Document that contract and give the projection its own Example section. Keep related projections with their conceptual API family.

The historical Class-based Enum is an exception to the placement rules: preserve the established order and implementation of existing methods. Newly added methods may be grouped at the end of the class instead of adjacent to their historical counterparts. Apply the alias and projection PHPDoc conventions to these new methods, without rewriting the historical documentation or implementations.

## Basic Layout

- Use four spaces for indentation, not tabs.
Expand Down
3 changes: 1 addition & 2 deletions examples/NativeExtension/Backed/ByteUnit.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Notonza\PhpEnumEx\EnumCollection;

enum ByteUnit: int {

case BYTE = 1;
case KIBIBYTE = 1 << 10;
case MEBIBYTE = 1 << 20;
Expand Down Expand Up @@ -55,7 +54,7 @@ public function format(): string {

$units = EnumCollection::of(ByteUnit::class);
$decimal = $units->wrapCasesAs(ByteUnitDecimal::from(...));
$hex = $units->wrapCasesAs(ByteUnitHex::from(...));
$hex = $units->wrapCasesAs(ByteUnitHex::from(...));

assert($decimal->fromName('MEBIBYTE')->format() === '1,048,576');
$unit = $decimal->fromName('MEBIBYTE');
Expand Down
1 change: 0 additions & 1 deletion examples/NativeExtension/Collections/FilePermission.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ enum NoPermission {
}

enum FilePermission: int {

case READ = 1 << 0;
case WRITE = 1 << 1;
case EXECUTE = 1 << 2;
Expand Down
2 changes: 1 addition & 1 deletion examples/NativeExtension/Composite/EnumComposite.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function canCancel(): bool {
assert($requirements->hasAll(Requirement::IdentityVerification, Requirement::AddressVerification));

$policy = new OrderStatusPolicy(OrderStatus::Paid, UserRole::Admin);
$guest = new OrderStatusPolicy(OrderStatus::Paid, UserRole::Guest);
$guest = new OrderStatusPolicy(OrderStatus::Paid, UserRole::Guest);

assert($policy->canCancel());
assert(!$guest->canCancel());
Expand Down
12 changes: 6 additions & 6 deletions examples/NativeExtension/Presentation/OrderStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ public function label(): string {
};
}

public function __toString(): string {
return $this->label();
}

public function color(): string {
return match ($this->unwrap()) {
OrderStatus::Pending => 'yellow',
Expand All @@ -58,11 +54,15 @@ public function color(): string {
OrderStatus::Cancelled => 'gray',
};
}

public function __toString(): string {
return $this->label();
}
}

$status = OrderStatus::Paid;
$ja = new OrderStatusPresentation($status, 'ja');
$en = new OrderStatusPresentation($status, 'en');
$ja = new OrderStatusPresentation($status, 'ja');
$en = new OrderStatusPresentation($status, 'en');

assert($status->canCancel());
assert($ja->label() === '支払い済み');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Notonza\PhpEnumEx\EnumCollection;

enum AirmailRegion {

case Asia;
case Europe;
case Africa;
Expand Down
9 changes: 4 additions & 5 deletions examples/NativeTraits/Collections/FilePermission.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,12 @@
use Notonza\PhpEnumEx\EnumTraits\EnumCollections;

enum FilePermission: int {

use EnumCollections;

case READ = 1 << 0;
case WRITE = 1 << 1;
case EXECUTE = 1 << 2;

public function isGrantedBy(int $mask): bool {
return ($mask & $this->value) === $this->value;
}

/** @return array<string, self> */
public static function granted(int $mask): array {
return self::filter(
Expand All @@ -35,6 +30,10 @@ public static function denied(int $mask): array {
=> $permission->isGrantedBy($mask),
);
}

public function isGrantedBy(int $mask): bool {
return ($mask & $this->value) === $this->value;
}
}

$mask = FilePermission::READ->value
Expand Down
199 changes: 104 additions & 95 deletions src/ClassBased/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,26 +192,6 @@ public final function debugOutput(string $delimiter = ' => '): string {
return $this->canonicalName() . $delimiter . var_export($this->scalar, true);
}

/**
* Alias of canonicalName().
*
* Example:
* Currency::JPY()->qualifiedName(); // 'App\\Currency::JPY'
*/
public final function qualifiedName(): string {
return $this->canonicalName();
}

/**
* Alias of debugOutput().
*
* Example:
* Currency::JPY()->qualifiedInfo(); // 'App\\Currency::JPY => 392'
*/
public final function qualifiedInfo(string $delimiter = ' => '): string {
return $this->debugOutput($delimiter);
}

/**
* Compares enum objects using PHP object equality (==), including their class and properties.
* Separate instances can be equal; this does not require identical object references (===).
Expand Down Expand Up @@ -313,26 +293,6 @@ public final static function elements(): array {
return self::$elements[$enum] = $values;
}

/**
* Alias of elements(): constant names mapped to cached class-based enum elements.
*
* @return array<string, static>
* @throws RootEnumMethodCallException If called directly on the abstract root Enum.
*/
public final static function nameToElement(): array {
return static::elements();
}

/**
* Compatibility alias of elements(); returns class-based enum elements, not native cases.
*
* @return array<string, static>
* @throws RootEnumMethodCallException If called directly on the abstract root Enum.
*/
public final static function nameToCase(): array {
return static::elements();
}

/**
* Returns all reflected constant names and original values, including inherited constants.
* Validates supported scalar/null types, rejects NAN and duplicate values, and requires a single non-null value type.
Expand Down Expand Up @@ -364,16 +324,6 @@ public final static function constants(): array {
return self::$constants[$enum] = $constants;
}

/**
* Alias of constants(): constant names mapped to original scalar/null values.
*
* @return array<string, bool|float|int|string|null>
* @throws RootEnumMethodCallException If called directly on the abstract root Enum.
*/
public final static function nameToValue(): array {
return static::constants();
}

/**
* Returns the reflected constant names in their original order.
*
Expand Down Expand Up @@ -492,28 +442,6 @@ private static function floatToArrayKey(float $value): int|string {
return sprintf('%.17h', $value);
}

/**
* Compatibility alias of valueToElement(); returns class-based enum elements, not native cases.
*
* @return array<array-key, static>
* @throws EnumDomainException If distinct values produce the same PHP array key.
* @throws RootEnumMethodCallException If called directly on the abstract root Enum.
*/
public final static function valueToCase(): array {
return static::valueToElement();
}

/**
* Projects the existing value-to-element map to constant names, preserving its keys and checks.
*
* @return array<array-key, string>
* @throws EnumDomainException If distinct values produce the same PHP array key.
* @throws RootEnumMethodCallException If called directly on the abstract root Enum.
*/
public final static function valueToName(): array {
return array_map(static fn(self $element): string => $element->name(), static::valueToElement());
}

/**
* Returns matching cached elements with original name keys and reflected constant order.
* A closure receives each element; array entries match enum equality via equals() or an exact scalar/null value.
Expand Down Expand Up @@ -740,29 +668,6 @@ public final static function listDebugOutput(string $delimiter = ' => '): array
));
}

/**
* Returns the canonical names of all elements, in reflected constant order.
*
* @return list<string>
* @throws RootEnumMethodCallException If called directly on the abstract root Enum.
*/
public final static function qualifiedNames(): array {
return array_values(array_map(
static fn(self $element): string => $element->canonicalName(),
static::elements(),
));
}

/**
* Alias of listDebugOutput().
*
* @return list<string>
* @throws RootEnumMethodCallException If called directly on the abstract root Enum.
*/
public final static function listQualifiedInfo(string $delimiter = ' => '): array {
return static::listDebugOutput($delimiter);
}

/**
* Returns a randomly selected cached element.
*
Expand Down Expand Up @@ -910,4 +815,108 @@ private static function throwAgainstUnknownValue($value): void {
}
throw new EnumDomainException("Unknown Enum value in $enum: [$value]");
}

/**
* Alias of canonicalName().
*
* @see self::canonicalName()
*/
public final function qualifiedName(): string {
return $this->canonicalName();
}

/**
* Alias of debugOutput().
*
* @see self::debugOutput()
*/
public final function qualifiedInfo(string $delimiter = ' => '): string {
return $this->debugOutput($delimiter);
}

/**
* Returns the canonical names of all elements, in reflected constant order.
*
* Example:
* Currency::qualifiedNames(); // ['App\\Currency::JPY', 'App\\Currency::USD', 'App\\Currency::EUR']
*
* @return list<string>
* @throws RootEnumMethodCallException If called directly on the abstract root Enum.
*/
public final static function qualifiedNames(): array {
return array_values(array_map(
static fn(self $element): string => $element->canonicalName(),
static::elements(),
));
}

/**
* Alias of listDebugOutput().
*
* @see self::listDebugOutput()
* @return list<string>
* @throws RootEnumMethodCallException If called directly on the abstract root Enum.
*/
public final static function listQualifiedInfo(string $delimiter = ' => '): array {
return static::listDebugOutput($delimiter);
}

/**
* Alias of constants(): constant names mapped to original scalar/null values.
*
* @see self::constants()
* @return array<string, bool|float|int|string|null>
* @throws RootEnumMethodCallException If called directly on the abstract root Enum.
*/
public final static function nameToValue(): array {
return static::constants();
}

/**
* Compatibility alias of elements(); returns class-based enum elements, not native cases.
*
* @see self::elements()
* @return array<string, static>
* @throws RootEnumMethodCallException If called directly on the abstract root Enum.
*/
public final static function nameToCase(): array {
return static::elements();
}

/**
* Alias of elements(): constant names mapped to cached class-based enum elements.
*
* @see self::elements()
* @return array<string, static>
* @throws RootEnumMethodCallException If called directly on the abstract root Enum.
*/
public final static function nameToElement(): array {
return static::elements();
}

/**
* Projects the existing value-to-element map to constant names, preserving its keys and checks.
*
* Example:
* Currency::valueToName(); // [392 => 'JPY', 840 => 'USD', 978 => 'EUR']
*
* @return array<array-key, string>
* @throws EnumDomainException If distinct values produce the same PHP array key.
* @throws RootEnumMethodCallException If called directly on the abstract root Enum.
*/
public final static function valueToName(): array {
return array_map(static fn(self $element): string => $element->name(), static::valueToElement());
}

/**
* Compatibility alias of valueToElement(); returns class-based enum elements, not native cases.
*
* @see self::valueToElement()
* @return array<array-key, static>
* @throws EnumDomainException If distinct values produce the same PHP array key.
* @throws RootEnumMethodCallException If called directly on the abstract root Enum.
*/
public final static function valueToCase(): array {
return static::valueToElement();
}
}
Loading