<?php
declare(strict_types=1);

namespace App\Service\Utils;

use App\Tests\Service\Utils\StringsUtilsTest;

/**
 * Class StringsUtils : méthodes d'aide pour la gestion de string.
 * @package App\Service\Utils
 */
class StringsUtils
{
    /**
     * Supprime les quotes d'une chaine de caractères
     * @param string $str
     * @return string
     * @see StringsUtilsTest::testUnquote()
     */
    public static function unquote(string $str): string
    {
        return str_replace("'", "", $str);
    }

    /**
     * Convert CamelCase formatted string into snake_case
     * @see https://stackoverflow.com/a/40514305/4279120
     *
     * @param string $string
     * @param string $sep
     * @return string
     *
     * @see StringsUtilsTest::testCamelToSnake()
     */
    public static function camelToSnake(string $string, string $sep = "_"): string
    {
        return strtolower(
            preg_replace(
            '/(?<=\d)(?=[A-Za-z])|(?<=[A-Za-z])(?=\d)|(?<=[a-z])(?=[A-Z])/',
            $sep,
            $string
            )
        );
    }

    /**
     * @param string $html
     * @return string
     *
     * @see StringsUtilsTest::testConvertHtmlToText()
     */
    public function convertHtmlToText(string $html): string
    {
        return strip_tags(preg_replace('{<(head|style)\b.*?</\1>}is', '', $html));
    }
}
