ソースを参照

Allow engines to work in 5.x

mscherer 2 年 前
コミット
2500c94159
3 ファイル変更45 行追加13 行削除
  1. 1 1
      src/Utility/Utility.php
  2. 20 4
      src/View/Helper/NumberHelper.php
  3. 24 8
      src/View/Helper/TextHelper.php

+ 1 - 1
src/Utility/Utility.php

@@ -239,7 +239,7 @@ class Utility {
 	 * @param array<string> $protocols Defaults to http and https. Pass empty array for all.
 	 * @param array<string> $protocols Defaults to http and https. Pass empty array for all.
 	 * @return string strippedUrl
 	 * @return string strippedUrl
 	 */
 	 */
-	public static function stripProtocol($url, $protocols = ['http', 'https']): string {
+	public static function stripProtocol(string $url, array $protocols = ['http', 'https']): string {
 		$pieces = parse_url($url);
 		$pieces = parse_url($url);
 		// Already stripped?
 		// Already stripped?
 		if (empty($pieces['scheme'])) {
 		if (empty($pieces['scheme'])) {

+ 20 - 4
src/View/Helper/NumberHelper.php

@@ -2,10 +2,10 @@
 
 
 namespace Tools\View\Helper;
 namespace Tools\View\Helper;
 
 
-use Cake\Utility\Hash;
+use Cake\Core\App;
+use Cake\Core\Exception\CakeException;
 use Cake\View\Helper\NumberHelper as CakeNumberHelper;
 use Cake\View\Helper\NumberHelper as CakeNumberHelper;
 use Cake\View\View;
 use Cake\View\View;
-use Tools\Utility\Number;
 
 
 /**
 /**
  * Overwrite to allow usage of own Number class.
  * Overwrite to allow usage of own Number class.
@@ -15,6 +15,13 @@ use Tools\Utility\Number;
 class NumberHelper extends CakeNumberHelper {
 class NumberHelper extends CakeNumberHelper {
 
 
 	/**
 	/**
+	 * Cake\I18n\Number instance
+	 *
+	 * @var \Cake\I18n\Number
+	 */
+	protected $_engine;
+
+	/**
 	 * ### Settings:
 	 * ### Settings:
 	 *
 	 *
 	 * - `engine` Class name to use to replace Number functionality.
 	 * - `engine` Class name to use to replace Number functionality.
@@ -24,10 +31,19 @@ class NumberHelper extends CakeNumberHelper {
 	 * @param array<string, mixed> $config Configuration settings for the helper
 	 * @param array<string, mixed> $config Configuration settings for the helper
 	 */
 	 */
 	public function __construct(View $View, array $config = []) {
 	public function __construct(View $View, array $config = []) {
-		$config = Hash::merge(['engine' => 'Tools.Number'], $config);
+		$config += ['engine' => 'Tools.Number'];
 
 
 		parent::__construct($View, $config);
 		parent::__construct($View, $config);
+
+		/** @psalm-var class-string<\Cake\I18n\Number>|null $engineClass */
+		$engineClass = App::className($config['engine'], 'Utility');
+		if ($engineClass === null) {
+			throw new CakeException(sprintf('Class for `%s` could not be found', $config['engine']));
+		}
+
+		$this->_engine = new $engineClass($config);
 	}
 	}
+
 	/**
 	/**
 	 * Call methods from Cake\I18n\Number utility class
 	 * Call methods from Cake\I18n\Number utility class
 	 *
 	 *
@@ -36,7 +52,7 @@ class NumberHelper extends CakeNumberHelper {
 	 * @return mixed Whatever is returned by called method, or false on failure
 	 * @return mixed Whatever is returned by called method, or false on failure
 	 */
 	 */
 	public function __call(string $method, array $params): mixed {
 	public function __call(string $method, array $params): mixed {
-		return Number::{$method}(...$params);
+		return $this->_engine->{$method}(...$params);
 	}
 	}
 
 
 }
 }

+ 24 - 8
src/View/Helper/TextHelper.php

@@ -2,11 +2,12 @@
 
 
 namespace Tools\View\Helper;
 namespace Tools\View\Helper;
 
 
+use Cake\Core\App;
+use Cake\Core\Exception\CakeException;
 use Cake\Utility\Hash;
 use Cake\Utility\Hash;
 use Cake\View\Helper\TextHelper as CakeTextHelper;
 use Cake\View\Helper\TextHelper as CakeTextHelper;
 use Cake\View\View;
 use Cake\View\View;
 use Tools\Utility\Number;
 use Tools\Utility\Number;
-use Tools\Utility\Text;
 use Tools\Utility\Utility;
 use Tools\Utility\Utility;
 
 
 if (!defined('CHAR_HELLIP')) {
 if (!defined('CHAR_HELLIP')) {
@@ -32,6 +33,13 @@ if (!defined('CHAR_HELLIP')) {
 class TextHelper extends CakeTextHelper {
 class TextHelper extends CakeTextHelper {
 
 
 	/**
 	/**
+	 * Cake Utility Text instance
+	 *
+	 * @var \Cake\Utility\Text
+	 */
+	protected $_engine;
+
+	/**
 	 * ### Settings:
 	 * ### Settings:
 	 *
 	 *
 	 * - `engine` Class name to use to replace Text functionality.
 	 * - `engine` Class name to use to replace Text functionality.
@@ -44,6 +52,14 @@ class TextHelper extends CakeTextHelper {
 		$config = Hash::merge(['engine' => 'Tools.Text'], $config);
 		$config = Hash::merge(['engine' => 'Tools.Text'], $config);
 
 
 		parent::__construct($View, $config);
 		parent::__construct($View, $config);
+
+		/** @psalm-var class-string<\Cake\Utility\Text>|null $engineClass */
+		$engineClass = App::className($config['engine'], 'Utility');
+		if ($engineClass === null) {
+			throw new CakeException(sprintf('Class for `%s` could not be found', $config['engine']));
+		}
+
+		$this->_engine = new $engineClass($config);
 	}
 	}
 
 
 	/**
 	/**
@@ -54,7 +70,7 @@ class TextHelper extends CakeTextHelper {
 	 * @return mixed Whatever is returned by called method, or false on failure
 	 * @return mixed Whatever is returned by called method, or false on failure
 	 */
 	 */
 	public function __call(string $method, array $params): mixed {
 	public function __call(string $method, array $params): mixed {
-		return Text::{$method}(...$params);
+		return $this->_engine->{$method}(...$params);
 	}
 	}
 
 
 	/**
 	/**
@@ -66,7 +82,7 @@ class TextHelper extends CakeTextHelper {
 	 * - placeholder
 	 * - placeholder
 	 * @return string the manipulated url (+ eventuell ...)
 	 * @return string the manipulated url (+ eventuell ...)
 	 */
 	 */
-	public function minimizeUrl($url, $max = null, array $options = []) {
+	public function minimizeUrl(string $url, ?int $max = null, array $options = []): string {
 		// check if there is nothing to do
 		// check if there is nothing to do
 		if (!$url || mb_strlen($url) <= (int)$max) {
 		if (!$url || mb_strlen($url) <= (int)$max) {
 			return $url;
 			return $url;
@@ -99,10 +115,10 @@ class TextHelper extends CakeTextHelper {
 	 * Removes http:// or other protocols from the link.
 	 * Removes http:// or other protocols from the link.
 	 *
 	 *
 	 * @param string $url
 	 * @param string $url
-	 * @param array $protocols Defaults to http and https. Pass empty array for all.
+	 * @param array<string> $protocols Defaults to http and https. Pass empty array for all.
 	 * @return string strippedUrl
 	 * @return string strippedUrl
 	 */
 	 */
-	public function stripProtocol($url, $protocols = ['http', 'https']) {
+	public function stripProtocol(string $url, array $protocols = ['http', 'https']): string {
 		return Utility::stripProtocol($url, $protocols);
 		return Utility::stripProtocol($url, $protocols);
 	}
 	}
 
 
@@ -114,7 +130,7 @@ class TextHelper extends CakeTextHelper {
 	 * @param bool $sup Whether to wrap the suffix in a superscript (<sup>) tag on output.
 	 * @param bool $sup Whether to wrap the suffix in a superscript (<sup>) tag on output.
 	 * @return string ordinal
 	 * @return string ordinal
 	 */
 	 */
-	public function ordinalNumber($num = 0, $sup = false) {
+	public function ordinalNumber(int $num = 0, bool $sup = false): string {
 		$ordinal = Number::ordinal($num);
 		$ordinal = Number::ordinal($num);
 
 
 		return ($sup) ? $num . '<sup>' . $ordinal . '</sup>' : $num . $ordinal;
 		return ($sup) ? $num . '<sup>' . $ordinal . '</sup>' : $num . $ordinal;
@@ -126,7 +142,7 @@ class TextHelper extends CakeTextHelper {
 	 * @param string $file Filename
 	 * @param string $file Filename
 	 * @return string
 	 * @return string
 	 */
 	 */
-	public function highlightFile($file) {
+	public function highlightFile(string $file): string {
 		return highlight_file($file, true);
 		return highlight_file($file, true);
 	}
 	}
 
 
@@ -136,7 +152,7 @@ class TextHelper extends CakeTextHelper {
 	 * @param string $string Content
 	 * @param string $string Content
 	 * @return string
 	 * @return string
 	 */
 	 */
-	public function highlightString($string) {
+	public function highlightString(string $string): string {
 		return highlight_string($string, true);
 		return highlight_string($string, true);
 	}
 	}