<?php

namespace Licenzo\Services\ShortcodeParser;

class DefaultPipes
{
    protected static $pipes = [
        'strtoupper',
        'strtolower',
        'ucwords',
        'ucfirst',
        'lcfirst',
        'trim',
        'ltrim',
        'rtrim',
        'nl2br',
        'wp_strip_all_tags',
        'esc_html',
        'esc_attr',
        'sanitize_text_field',
        'sanitize_title',
        'absint',
        'intval',
        'floatval',
        'number_format',
    ];

    protected static $additionalSupports = [
        'upper' => 'strtoupper',
        'lower' => 'strtolower',
        'title' => 'ucwords',
    ];

    public static function hasPipe($pipe)
    {
        if (in_array($pipe, static::$pipes, true)) {
            return true;
        }

        if (array_key_exists($pipe, static::$additionalSupports)) {
            return true;
        }

        return false;
    }

    public static function apply($content, $pipe)
    {
        if (!static::hasPipe($pipe)) {
            throw new \Exception('Invalid pipe: ' . esc_html($pipe));
        }

        if (array_key_exists($pipe, static::$additionalSupports)) {
            return call_user_func(static::$additionalSupports[$pipe], $content);
        }

        return call_user_func($pipe, $content);
    }
}
