Changeset 3772069 for linguise/trunk/src/Helper.php
- Timestamp:
- 10/29/2026 04:38:34 AM (16 hours ago)
- File:
-
- 1 edited
-
linguise/trunk/src/Helper.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
linguise/trunk/src/Helper.php
r3757638 r3772069 371 371 372 372 /** 373 * Parse a CSS color (HEX, RGB, or RGBA) into its RGBA components. 374 * 375 * @param string $color The CSS color to parse 376 * 377 * @return array|null The parsed color as an array with 'r', 'g', 'b', and 'a' keys, or null if invalid 378 */ 379 public static function parseColorToRgba($color) 380 { 381 if (!is_string($color)) { 382 return null; 383 } 384 385 $color = trim($color); 386 387 if (preg_match('/^rgba?\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*(?:,\s*([\d.]+)\s*)?\)$/i', $color, $matches)) { 388 $r = (int) $matches[1]; 389 $g = (int) $matches[2]; 390 $b = (int) $matches[3]; 391 $a = (isset($matches[4]) && $matches[4] !== '') ? (float) $matches[4] : 1.0; 392 393 if ($r > 255 || $g > 255 || $b > 255 || $a < 0 || $a > 1) { 394 return null; 395 } 396 397 return ['r' => $r, 'g' => $g, 'b' => $b, 'a' => $a]; 398 } 399 400 $rgb = self::parseHexColor($color); 401 if ($rgb === null) { 402 return null; 403 } 404 405 return ['r' => $rgb['r'], 'g' => $rgb['g'], 'b' => $rgb['b'], 'a' => 1.0]; 406 } 407 408 /** 409 * Sanitize a CSS color, allowing only HEX, RGB, and RGBA values. 410 * 411 * @param string $color The CSS color to sanitize 412 * @param string $fallback The value to return when the color is invalid 413 * 414 * @return string The sanitized color or the fallback 415 */ 416 public static function sanitizeCssColor($color, $fallback = '') 417 { 418 if (self::parseColorToRgba($color) === null) { 419 return $fallback; 420 } 421 422 return trim($color); 423 } 424 425 /** 373 426 * Check if the array is an actual object or not. 374 427 * … … 397 450 return implode('', $keys) !== implode(range(0, count($keys) - 1)); 398 451 // @codeCoverageIgnoreEnd 452 } 453 454 /** 455 * Recommend a switcher dropdown mode for the number of selectable languages. 456 * 457 * @param string $defaultLanguage The site's default language. 458 * @param array|object $enabledLanguages Enabled language codes or language objects. 459 * 460 * @return string 461 */ 462 public static function getRecommendedFlagDisplayType($defaultLanguage, $enabledLanguages) 463 { 464 $languageCodes = []; 465 $defaultLanguage = is_string($defaultLanguage) ? strtolower(trim($defaultLanguage)) : ''; 466 if ($defaultLanguage !== '') { 467 $languageCodes[] = $defaultLanguage; 468 } 469 470 foreach ((array) $enabledLanguages as $language) { 471 if (is_object($language)) { 472 $language = isset($language->code) ? $language->code : ''; 473 } elseif (is_array($language)) { 474 $language = isset($language['code']) ? $language['code'] : ''; 475 } 476 477 if (is_string($language) && trim($language) !== '') { 478 $languageCodes[] = strtolower(trim($language)); 479 } 480 } 481 482 return count(array_unique($languageCodes)) < 10 ? 'dropdown' : 'dropdown_search'; 483 } 484 485 /** 486 * Determine whether a saved display mode should be treated as user-managed. 487 * 488 * Legacy installations with existing switcher configuration are kept on 489 * their current mode; an explicit marker takes precedence over inference. 490 * 491 * @param array $options Stored Linguise options. 492 * 493 * @return integer 494 */ 495 public static function isFlagDisplayTypeCustomized($options) 496 { 497 if (array_key_exists('flag_display_type_customized', $options)) { 498 return in_array($options['flag_display_type_customized'], [true, 1, '1'], true) ? 1 : 0; 499 } 500 501 return (array_key_exists('flag_display_type', $options) || !empty($options['token']) || !empty($options['enabled_languages'])) ? 1 : 0; 399 502 } 400 503
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)