autowp/zf-components

View on GitHub
src/Filter/SingleSpaces.php

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
<?php

declare(strict_types=1);

namespace Autowp\ZFComponents\Filter;

use Laminas\Filter\FilterInterface;

use function explode;
use function implode;
use function preg_replace;
use function str_replace;
use function strlen;

class SingleSpaces implements FilterInterface
{
    /**
     * @param mixed $value
     */
    public function filter($value): string
    {
        $value = (string) $value;

        if (strlen($value) <= 0) {
            return '';
        }

        $value = str_replace("\r", "", $value);
        $lines = explode("\n", $value);
        foreach ($lines as &$line) {
            $line = preg_replace('/[[:space:]]+/s', ' ', $line);
        }
        return implode("\n", $lines);
    }
}