FormTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 43
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A autoInsert() 0 6 1
A insertIntoPostForms() 0 14 2
1
<?php
2
3
namespace Psr7Middlewares\Utils;
4
5
use Psr\Http\Message\ResponseInterface;
6
7
/**
8
 * Utilities used by middlewares that manipulate forms.
9
 */
10
trait FormTrait
11
{
12
    use StreamTrait;
13
14
    private $autoInsert = false;
15
16
    /**
17
     * Configure if autoinsert or not the inputs automatically.
18
     *
19
     * @param bool $autoInsert
20
     *
21
     * @return self
22
     */
23
    public function autoInsert($autoInsert = true)
24
    {
25
        $this->autoInsert = $autoInsert;
26
27
        return $this;
28
    }
29
30
    /**
31
     * Insert content into all POST forms.
32
     *
33
     * @param ResponseInterface $response
34
     * @param callable          $replace
35
     *
36
     * @return ResponseInterface
37
     */
38
    private function insertIntoPostForms(ResponseInterface $response, callable $replace)
39
    {
40
        $html = (string) $response->getBody();
41
        $html = preg_replace_callback('/(<form\s[^>]*method=["\']?POST["\']?[^>]*>)/i', $replace, $html, -1, $count);
42
43
        if (!empty($count)) {
44
            $body = self::createStream();
45
            $body->write($html);
46
47
            return $response->withBody($body);
48
        }
49
50
        return $response;
51
    }
52
}
53