Middleware to truncate length of input

Is it possible for Middleware to modify the $request object such that the each of the array items returned by getParsedBody() can be truncated to not exceed a set length, then the $request object has the truncated body replace its original body?

Something like:

        $bodyin = $request->getParsedBody();

        foreach ($bodyin as $k => $v) {
            $bodyin[$k] = mb_substr($v,0,100);
        }

        $body = new \Slim\Http\Body(fopen('php://temp', 'wb+'));
        $body->write(serialize($bodyin)); // This part does not work as is
        $request = $request->withBody($body);

Then the rest of the app will access the new modified $request object.

You could use withParsedBody:

        $bodyin = $request->getParsedBody();

        foreach ($bodyin as $k => $v) {
            $bodyin[$k] = mb_substr($v,0,100);
        }

        $request = $request->withParsedBody($bodyin);

P.S: It’s a good idea to check if $v is an array.

Thanks. I’ll try that.
This was just a quick mock-up; a proper recursive function will be needed to the truncating of the array values.