| @@ 46-61 (lines=16) @@ | ||
| 43 | * |
|
| 44 | * @return int|string Value of EnumInterface |
|
| 45 | */ |
|
| 46 | public function transform($value) |
|
| 47 | { |
|
| 48 | if ($value === null) { |
|
| 49 | return null; |
|
| 50 | } |
|
| 51 | ||
| 52 | if (!$value instanceof $this->enumClass) { |
|
| 53 | throw new TransformationFailedException(sprintf( |
|
| 54 | 'Expected instance of "%s". Got "%s".', |
|
| 55 | $this->enumClass, |
|
| 56 | is_object($value) ? get_class($value) : gettype($value) |
|
| 57 | )); |
|
| 58 | } |
|
| 59 | ||
| 60 | return $value->getValue(); |
|
| 61 | } |
|
| 62 | ||
| 63 | /** |
|
| 64 | * Transforms scalar enum value to enumeration instance. |
|
| @@ 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. |
|