| @@ 27-44 (lines=18) @@ | ||
| 24 | * |
|
| 25 | * @return FlaggedEnum[]|null An array of FlaggedEnum instances with single bit flag |
|
| 26 | */ |
|
| 27 | public function transform($value) |
|
| 28 | { |
|
| 29 | if ($value === null) { |
|
| 30 | return null; |
|
| 31 | } |
|
| 32 | ||
| 33 | if (!$value instanceof $this->enumClass) { |
|
| 34 | throw new TransformationFailedException(sprintf( |
|
| 35 | 'Expected instance of "%s". Got "%s".', |
|
| 36 | $this->enumClass, |
|
| 37 | \is_object($value) ? \get_class($value) : \gettype($value) |
|
| 38 | )); |
|
| 39 | } |
|
| 40 | ||
| 41 | return array_map(function ($flag) { |
|
| 42 | return $this->createEnum($flag); |
|
| 43 | }, $value->getFlags()); |
|
| 44 | } |
|
| 45 | ||
| 46 | /** |
|
| 47 | * Transforms an array of flagged enums to single flagged enumeration instance. |
|
| @@ 49-64 (lines=16) @@ | ||
| 46 | * |
|
| 47 | * @return int|string Value of EnumInterface |
|
| 48 | */ |
|
| 49 | public function transform($value) |
|
| 50 | { |
|
| 51 | if ($value === null) { |
|
| 52 | return null; |
|
| 53 | } |
|
| 54 | ||
| 55 | if (!$value instanceof $this->enumClass) { |
|
| 56 | throw new TransformationFailedException(sprintf( |
|
| 57 | 'Expected instance of "%s". Got "%s".', |
|
| 58 | $this->enumClass, |
|
| 59 | \is_object($value) ? \get_class($value) : \gettype($value) |
|
| 60 | )); |
|
| 61 | } |
|
| 62 | ||
| 63 | return $value->getValue(); |
|
| 64 | } |
|
| 65 | ||
| 66 | /** |
|
| 67 | * Transforms a raw enumerated value to an enumeration instance. |
|