htmlburger /
wpemerge
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * @package WPEmerge |
||
| 4 | * @author Atanas Angelov <[email protected]> |
||
| 5 | * @copyright 2017-2019 Atanas Angelov |
||
| 6 | * @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0 |
||
| 7 | * @link https://wpemerge.com/ |
||
| 8 | */ |
||
| 9 | |||
| 10 | namespace WPEmerge\Responses; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Converts values to a response. |
||
| 14 | */ |
||
| 15 | trait ConvertsToResponseTrait { |
||
| 16 | /** |
||
| 17 | * Get a Response Service instance. |
||
| 18 | * |
||
| 19 | * @return ResponseService |
||
| 20 | */ |
||
| 21 | protected abstract function getResponseService(); |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 22 | |||
| 23 | /** |
||
| 24 | * Convert a user returned response to a ResponseInterface instance if possible. |
||
| 25 | * Return the original value if unsupported. |
||
| 26 | * |
||
| 27 | * @param mixed $response |
||
| 28 | * @return mixed |
||
| 29 | */ |
||
| 30 | 4 | protected function toResponse( $response ) { |
|
| 31 | 4 | if ( is_string( $response ) ) { |
|
| 32 | 1 | return $this->getResponseService()->output( $response ); |
|
| 33 | } |
||
| 34 | |||
| 35 | 3 | if ( is_array( $response ) ) { |
|
| 36 | 1 | return $this->getResponseService()->json( $response ); |
|
| 37 | } |
||
| 38 | |||
| 39 | 2 | if ( $response instanceof ResponsableInterface ) { |
|
| 40 | 1 | return $response->toResponse(); |
|
| 41 | } |
||
| 42 | |||
| 43 | 1 | return $response; |
|
| 44 | } |
||
| 45 | } |
||
| 46 |