More params and returns to PHP 7.3+

This commit is contained in:
djmaze 2020-03-16 13:08:53 +01:00
parent 3d246ae842
commit 26c38b3ec9
95 changed files with 1028 additions and 4865 deletions

View file

@ -15,23 +15,11 @@ namespace MailSo\Base;
* @category MailSo
* @package Base
*/
class HtmlUtils
abstract class HtmlUtils
{
static $KOS = '@@_KOS_@@';
/**
* @access private
*/
private function __construct()
{
}
/**
* @param \DOMElement $oElement
*
* @return array
*/
public static function GetElementAttributesAsArray($oElement)
public static function GetElementAttributesAsArray(?\DOMElement $oElement) : array
{
$aResult = array();
if ($oElement)
@ -52,12 +40,7 @@ class HtmlUtils
return $aResult;
}
/**
* @param string $sText
*
* @return \DOMDocument|bool
*/
public static function GetDomFromText($sText)
public static function GetDomFromText(string $sText) : \DOMDocument
{
$bState = true;
if (\MailSo\Base\Utils::FunctionExistsAndEnabled('libxml_use_internal_errors'))
@ -92,10 +75,7 @@ class HtmlUtils
return $oDom;
}
/**
* @return \DOMDocument
*/
private static function createDOMDocument()
private static function createDOMDocument() : \DOMDocument
{
$oDoc = new \DOMDocument('1.0', 'UTF-8');
$oDoc->encoding = 'UTF-8';
@ -106,12 +86,7 @@ class HtmlUtils
return $oDoc;
}
/**
* @param \DOMDocument|\DOMElement $oElem
*
* @return string
*/
private static function domToString($oElem, $oDom = null)
private static function domToString(\DOMNode $oElem, ?\DOMDocument $oDom = null) : string
{
$sResult = '';
if ($oElem instanceof \DOMDocument)
@ -142,13 +117,7 @@ class HtmlUtils
return \trim($sResult);
}
/**
* @param \DOMDocument $oDom
* @param bool $bWrapByFakeHtmlAndBodyDiv = true
*
* @return string
*/
public static function GetTextFromDom_($oDom, $bWrapByFakeHtmlAndBodyDiv = true)
public static function GetTextFromDom_(\DOMDocument $oDom, bool $bWrapByFakeHtmlAndBodyDiv = true) : string
{
$sResult = '';
@ -201,13 +170,7 @@ class HtmlUtils
return $sResult;
}
/**
* @param \DOMDocument $oDom
* @param bool $bWrapByFakeHtmlAndBodyDiv = true
*
* @return string
*/
public static function GetTextFromDom($oDom, $bWrapByFakeHtmlAndBodyDiv = true)
public static function GetTextFromDom(\DOMDocument $oDom, bool $bWrapByFakeHtmlAndBodyDiv = true)
{
$sResult = '';
@ -251,14 +214,7 @@ class HtmlUtils
return $sResult;
}
/**
* @param string $sHtml
* @param string $sHtmlAttrs = ''
* @param string $sBodyAttrs = ''
*
* @return string
*/
public static function ClearBodyAndHtmlTag($sHtml, &$sHtmlAttrs = '', &$sBodyAttrs = '')
public static function ClearBodyAndHtmlTag(string $sHtml, string &$sHtmlAttrs = '', string &$sBodyAttrs = '') : string
{
$aMatch = array();
if (\preg_match('/<html([^>]+)>/im', $sHtml, $aMatch) && !empty($aMatch[1]))
@ -291,13 +247,7 @@ class HtmlUtils
return $sHtml;
}
/**
* @param string $sHtml
* @param bool $bClearEmpty = true
*
* @return string
*/
public static function FixSchemas($sHtml, $bClearEmpty = true)
public static function FixSchemas(string $sHtml, bool $bClearEmpty = true) : string
{
if ($bClearEmpty)
{
@ -310,12 +260,7 @@ class HtmlUtils
return $sHtml;
}
/**
* @param string $sHtml
*
* @return string
*/
public static function ClearFastTags($sHtml)
public static function ClearFastTags(string $sHtml) : string
{
return \preg_replace(array(
'/<p[^>]*><\/p>/i',
@ -354,9 +299,8 @@ class HtmlUtils
/**
* @param mixed $oDom
* @param bool $bClearStyleAndHead = true
*/
public static function ClearTags(&$oDom, $bClearStyleAndHead = true)
public static function ClearTags(&$oDom, bool $bClearStyleAndHead = true)
{
$aRemoveTags = array(
'svg', 'link', 'base', 'meta', 'title', 'x-script', 'script', 'bgsound', 'keygen', 'source',
@ -402,8 +346,8 @@ class HtmlUtils
/*
// public static function ClearStyleUrlValueParserHelper($oUrlValue, $oRule, $oRuleSet,
// $oElem = null,
// &$bHasExternals = false, &$aFoundCIDs = array(),
// $aContentLocationUrls = array(), &$aFoundedContentLocationUrls = array(),
// &$bHasExternals = false, array &$aFoundCIDs = array(),
// $aContentLocationUrls = array(), array &$aFoundedContentLocationUrls = array(),
// $bDoNotReplaceExternalUrl = false, $fAdditionalExternalFilter = null
// )
// {
@ -496,9 +440,9 @@ class HtmlUtils
// }
// }
//
// public static function ClearStyleSmart($sStyle, $oElement = null,
// &$bHasExternals = false, &$aFoundCIDs = array(),
// $aContentLocationUrls = array(), &$aFoundedContentLocationUrls = array(),
// public static function ClearStyleSmart(string $sStyle, $oElement = null,
// &$bHasExternals = false, array &$aFoundCIDs = array(),
// $aContentLocationUrls = array(), array &$aFoundedContentLocationUrls = array(),
// $bDoNotReplaceExternalUrl = false, $fAdditionalExternalFilter = null,
// $sSelectorPrefix = '')
// {
@ -527,7 +471,7 @@ class HtmlUtils
// $oCssParser = new \Sabberworm\CSS\Parser($sStyle, $oSettings);
// $oCss = $oCssParser->parse();
// }
// catch (\Exception $oEception)
// catch (\Throwable $oEception)
// {
// unset($oEception);
// $mResult = false;
@ -611,7 +555,7 @@ class HtmlUtils
// {
// $mResult = $oCss->render(\Sabberworm\CSS\OutputFormat::createCompact());
// }
// catch (\Exception $oEception)
// catch (\Throwable $oEception)
// {
// unset($oEception);
// $mResult = false;
@ -623,20 +567,10 @@ class HtmlUtils
*/
/**
*
* @param string $sStyle
* @param \DOMElement $oElement
* @param bool $bHasExternals
* @param array $aFoundCIDs
* @param array $aContentLocationUrls
* @param array $aFoundedContentLocationUrls
* @param bool $bDoNotReplaceExternalUrl = false
* @param callback|null $fAdditionalExternalFilter = null
*
* @return string
*/
public static function ClearStyle($sStyle, $oElement, &$bHasExternals, &$aFoundCIDs,
$aContentLocationUrls, &$aFoundedContentLocationUrls, $bDoNotReplaceExternalUrl = false, $fAdditionalExternalFilter = null)
public static function ClearStyle(string $sStyle, \DOMElement $oElement, bool &$bHasExternals, array &$aFoundCIDs,
array $aContentLocationUrls, array &$aFoundedContentLocationUrls, bool $bDoNotReplaceExternalUrl = false, $fAdditionalExternalFilter = null)
{
$sStyle = \trim($sStyle);
$aOutStyles = array();
@ -762,10 +696,7 @@ class HtmlUtils
return \implode(';', $aOutStyles);
}
/**
* @param \DOMDocument $oDom
*/
public static function FindLinksInDOM(&$oDom)
public static function FindLinksInDOM(\DOMDocument $oDom)
{
$aNodes = $oDom->getElementsByTagName('*');
foreach ($aNodes as /* @var $oElement \DOMElement */ $oElement)
@ -854,15 +785,7 @@ class HtmlUtils
unset($aNodes);
}
/**
* @param string $sHtml
* @param bool $bDoNotReplaceExternalUrl = false
* @param bool $bFindLinksInHtml = false
* @param bool $bWrapByFakeHtmlAndBodyDiv = true
*
* @return string
*/
public static function ClearHtmlSimple($sHtml, $bDoNotReplaceExternalUrl = false, $bFindLinksInHtml = false, $bWrapByFakeHtmlAndBodyDiv = true)
public static function ClearHtmlSimple(string $sHtml, bool $bDoNotReplaceExternalUrl = false, bool $bFindLinksInHtml = false, bool $bWrapByFakeHtmlAndBodyDiv = true) : string
{
$bHasExternals = false;
$aFoundCIDs = array();
@ -879,25 +802,14 @@ class HtmlUtils
}
/**
* @param string $sHtml
* @param bool $bHasExternals = false
* @param array $aFoundCIDs = array()
* @param array $aContentLocationUrls = array()
* @param array $aFoundedContentLocationUrls = array()
* @param bool $bDoNotReplaceExternalUrl = false
* @param bool $bFindLinksInHtml = false
* @param callback|null $fAdditionalExternalFilter = null
* @param callback|null $fAdditionalDomReader = null
* @param bool $bTryToDetectHiddenImages = false
* @param bool $bWrapByFakeHtmlAndBodyDiv = true
*
* @return string
*/
public static function ClearHtml($sHtml, &$bHasExternals = false, &$aFoundCIDs = array(),
$aContentLocationUrls = array(), &$aFoundedContentLocationUrls = array(),
$bDoNotReplaceExternalUrl = false, $bFindLinksInHtml = false,
public static function ClearHtml(string $sHtml, bool &$bHasExternals = false, array &$aFoundCIDs = array(),
array $aContentLocationUrls = array(), array &$aFoundedContentLocationUrls = array(),
bool $bDoNotReplaceExternalUrl = false, bool $bFindLinksInHtml = false,
$fAdditionalExternalFilter = null, $fAdditionalDomReader = false,
$bTryToDetectHiddenImages = false, $bWrapByFakeHtmlAndBodyDiv = true)
bool $bTryToDetectHiddenImages = false, bool $bWrapByFakeHtmlAndBodyDiv = true)
{
$sResult = '';
@ -1264,15 +1176,7 @@ class HtmlUtils
return $sResult;
}
/**
* @param string $sHtml
* @param array $aFoundCids = array()
* @param array|null $mFoundDataURL = null
* @param array $aFoundedContentLocationUrls = array()
*
* @return string
*/
public static function BuildHtml($sHtml, &$aFoundCids = array(), &$mFoundDataURL = null, &$aFoundedContentLocationUrls = array())
public static function BuildHtml(string $sHtml, array &$aFoundCids = array(), &$mFoundDataURL = null, array &$aFoundedContentLocationUrls = array()) : string
{
$oDom = \MailSo\Base\HtmlUtils::GetDomFromText($sHtml);
@ -1407,13 +1311,7 @@ class HtmlUtils
'<body>'.$sResult.'</body></html>';
}
/**
* @param string $sText
* @param bool $bLinksWithTargetBlank = true
*
* @return string
*/
public static function ConvertPlainToHtml($sText, $bLinksWithTargetBlank = true)
public static function ConvertPlainToHtml(string $sText, bool $bLinksWithTargetBlank = true) : string
{
$sText = \trim($sText);
if (0 === \strlen($sText))
@ -1495,12 +1393,7 @@ class HtmlUtils
return $sText;
}
/**
* @param string $sText
*
* @return string
*/
public static function ConvertHtmlToPlain($sText)
public static function ConvertHtmlToPlain(string $sText) : string
{
$sText = \trim(\stripslashes($sText));
$sText = \MailSo\Base\Utils::StripSpaces($sText);