styleset.class.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * The style setting class file of zin of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2023 青岛易软天创网络科技有限公司(QingDao Nature Easy Soft Network Technology Co,LTD, www.cnezsoft.com)
  6. * @author Hao Sun <sunhao@easycorp.ltd>
  7. * @package zin
  8. * @version $Id
  9. * @link https://www.zentao.net
  10. */
  11. namespace zin\utils;
  12. require_once __DIR__ . DS . 'dataset.class.php';
  13. /**
  14. * Manage style for html element and widgets
  15. *
  16. * Example:
  17. *
  18. * // Create a style object an convert to css string
  19. * $style = static::create(array('color' => 'red'));
  20. * echo $style(); // Output "color:red"
  21. *
  22. * // Above example same as:
  23. * echo static::css(array('color' => 'red'));
  24. *
  25. * // Modifier style
  26. * $style = static::create(array('color' => 'red'));
  27. * $style->set('background', 'green');
  28. *
  29. * // Modifier style with property name directly
  30. * $style->background = 'green';
  31. *
  32. * // Get style value
  33. * echo $style->get('background'); // Output "green"
  34. *
  35. * // Get style value with property name directly
  36. * echo $style->background; // Output "green"
  37. *
  38. * @todo @sunhao: Validate style properties on modifying
  39. */
  40. class styleset extends dataset
  41. {
  42. /**
  43. * Format CSS variable name with prefix "--"
  44. *
  45. * @access public
  46. * @param string $name - CSS variable name
  47. * @return string
  48. */
  49. public static function formatVarName($name)
  50. {
  51. return \zin\str_starts_with($name, '--') ? $name : "--$name";
  52. }
  53. /**
  54. * Set or get css variable, an array can be passed to set multiple variables
  55. * If only pass variable name, then the variable value will be returned
  56. * If no params passed, then return all setted variables with an array
  57. *
  58. * Notice: no need to prepend prefix '--' to variable name, the method will prepend it automatically, if prepended already, the method will skip to prepend smartly
  59. *
  60. * Example:
  61. *
  62. * // Create a style object and set
  63. * $style = new style();
  64. * $style->cssVar('text-size', '14px');
  65. *
  66. * // Set multiple variables
  67. * $style->cssVar(array('text-color' => 'yellow', 'background-image' => 'none'));
  68. *
  69. * // Get variable value
  70. * echo $style->cssVar('text-size'); // Output "14px"
  71. *
  72. * // Get all variables value
  73. * echo $style->cssVar();
  74. * // Output array('text-size' => '14px', 'color' => 'yellow', 'background' => 'none');
  75. *
  76. * // Remove variable by setting value with an empty string
  77. * $style->cssVar('text-color', '');
  78. *
  79. * @access public
  80. * @param array|string $name - Variable name or variables list
  81. * @param string|null $value - Property value
  82. * @return styleset|array|string
  83. */
  84. public function cssVar($name = '', $value = null)
  85. {
  86. /* Support for setting multiple variables by an array */
  87. if(is_array($name))
  88. {
  89. foreach($name as $n => $value) $this->set(static::formatVarName($n), $value);
  90. return $this;
  91. }
  92. /* Return all setted variables without passed any params */
  93. if(empty($name))
  94. {
  95. $vars = array();
  96. foreach ($this->storedData as $prop => $value)
  97. {
  98. if(strncmp($name, '--', strlen('--')) !== 0) continue;
  99. $vars[substr($prop, 2)] = $value;
  100. }
  101. return $vars;
  102. }
  103. $varName = static::formatVarName($name);
  104. /* Return the specific variable value by name */
  105. if($value === null) return $this->get($varName);
  106. /* Set the specific variable value and return style object self */
  107. $this->set($varName, $value === '' ? null : $value);
  108. return $this;
  109. }
  110. /**
  111. * Convert to string
  112. *
  113. * @access public
  114. * @return string
  115. */
  116. public function toStr()
  117. {
  118. $pairs = array();
  119. foreach($this->storedData as $prop => $value)
  120. {
  121. /* Skip any empty value */
  122. if($value === null || $value === '') continue;
  123. $pairs[] = $prop . ': ' . strval($value) . ';';
  124. }
  125. return implode(' ', $pairs);
  126. }
  127. }