Completed
Push — master ( 00ed35...63fbb3 )
by Oscar
10:20
created

FormTrait::isPost()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 8.8571
cc 6
eloc 9
nc 6
nop 1
1
<?php
2
3
namespace Psr7Middlewares\Utils;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Psr7Middlewares\Middleware;
7
8
/**
9
 * Utilities used by middlewares that manipulates forms.
10
 */
11
trait FormTrait
12
{
13
    /**
14
     * Insert content into all POST forms.
15
     * 
16
     * @param ResponseInterface $response
17
     * @param callable          $replace
18
     * 
19
     * @return ResponseInterface
20
     */
21
    private function insertIntoPostForms(ResponseInterface $response, callable $replace)
22
    {
23
        $html = (string) $response->getBody();
24
        $html = preg_replace_callback('/(<form\s[^>]*method=["\']?POST["\']?[^>]*>)/i', $replace, $html, -1, $count);
25
26
        if (!empty($count)) {
27
            $body = Middleware::createStream();
28
            $body->write($html);
29
30
            return $response->withBody($body);
31
        }
32
33
        return $response;
34
    }
35
}
36