| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Cerbero\Enum\Enums; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Cerbero\Enum\Attributes\Meta; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Cerbero\Enum\Concerns\Enumerates; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Generator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use function Cerbero\Enum\camel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use function Cerbero\Enum\parseCaseValue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use function Cerbero\Enum\snake; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * The backed value when generating new enums. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  | enum Backed | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     use Enumerates; | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     #[Meta(label: 'The enum is pure, no values needed')] | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     case pure; | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     #[Meta(label: 'Custom values to assign manually')] | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     case custom; | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     #[Meta(label: 'The name in snake case (case_one)')] | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     case snake; | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     #[Meta(label: 'The name in camel case (caseOne)')] | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     case camel; | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     #[Meta(label: 'The name in kebab case (case-one)')] | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     case kebab; | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     #[Meta(label: 'The name in upper case (CASEONE)')] | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     case upper; | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     #[Meta(label: 'The name in lower case (caseone)')] | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     case lower; | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     #[Meta(label: 'Integer starting from 0 (0, 1, 2...)')] | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     case int0; | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     #[Meta(label: 'Integer starting from 1 (1, 2, 3...)')] | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     case int1; | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     #[Meta(label: 'Bitwise flag (1, 2, 4...)')] | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |     case bitwise; | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |      * Retrieve the given cases, optionally backed by the provided backing strategy. | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      * @param string[] $cases | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      * @return array<string, string|int|null> | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      * @throws \ValueError | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     public static function backCases(array $cases, ?string $strategy = null): array | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         $backed = match (true) { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |             is_string($strategy) => self::fromName($strategy), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |             default => str_contains($cases[0] ?? '', '=') ? self::custom : self::pure, | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |         }; | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         return $backed->back($cases); | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |      * Retrieve the given cases after backing them. | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |      * @param string[] $cases | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |      * @return array<string, string|int|null> | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     public function back(array $cases): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $backedCases = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $pairs = $this->yieldPairs(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         foreach ($cases as $case) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             $backedCases += $pairs->send($case); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             $pairs->next(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 86 |  |  |         return $backedCases; | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |      * Yield the case-value pairs. | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |      * @return Generator<int, array<string, string|int|null>> | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     public function yieldPairs(): Generator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $i = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $callback = match ($this) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             self::pure => fn(string $name) => [$name => null], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             self::custom => parseCaseValue(...), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             self::snake => fn(string $name) => [$name => snake($name)], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             self::camel => fn(string $name) => [$name => camel($name)], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             self::kebab => fn(string $name) => [$name => snake($name, '-')], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             self::upper => fn(string $name) => [$name => strtoupper($name)], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             self::lower => fn(string $name) => [$name => strtolower($name)], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             self::int0 => fn(string $name, int $i) => [$name => $i], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             self::int1 => fn(string $name, int $i) => [$name => $i + 1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             self::bitwise => fn(string $name, int $i) => [$name => "1 << {$i}"], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         /** @phpstan-ignore while.alwaysTrue */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         while (true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             /** @phpstan-ignore-next-line */ | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 114 |  |  |             yield $callback(yield, $i++); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 115 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 117 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 118 |  |  |  |