ResponseTrait::render()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.0466

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 10
ccs 6
cts 7
cp 0.8571
crap 4.0466
rs 10
1
<?php
2
3
/**
4
 * Bluz Framework Component
5
 *
6
 * @copyright Bluz PHP Team
7
 * @link      https://github.com/bluzphp/framework
8
 */
9
10
declare(strict_types=1);
11
12
namespace Bluz\Response;
13
14
/**
15
 * Response Trait
16
 *
17
 * @package  Bluz\Response
18
 * @author   Anton Shevchuk
19
 */
20
trait ResponseTrait
21
{
22
    /**
23
     * @return string
24
     */
25
    abstract public function jsonSerialize();
26
27
    /**
28
     * @return string
29
     */
30
    abstract public function __toString();
31
32
    /**
33
     * Render object as HTML or JSON
34
     *
35
     * @param string $type
36
     *
37
     * @return string
38
     */
39 3
    public function render(string $type = 'HTML'): string
40
    {
41
        // switch statement by response type
42 3
        switch (strtoupper($type)) {
43 3
            case 'CLI':
44 3
            case 'JSON':
45
                return $this->jsonSerialize();
46 3
            case 'HTML':
47
            default:
48 3
                return $this->__toString();
49
        }
50
    }
51
}
52