style.class.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * The style setter class file of zin lib.
  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;
  12. require_once dirname(__DIR__) . DS . 'utils' . DS . 'dataset.class.php';
  13. class style extends \zin\utils\dataset implements iDirective
  14. {
  15. /**
  16. * Magic method for setting style.
  17. *
  18. * @access public
  19. * @param string $name - Property name.
  20. * @param array $args - Property values.
  21. * @return style
  22. */
  23. public function __call($name, $args)
  24. {
  25. $value = empty($args) ? true : (count($args) === 1 ? $args[0] : $args);
  26. return $this->setVal($name, $value);
  27. }
  28. /**
  29. * Method for sub class to hook on setting it.
  30. *
  31. * @access protected
  32. * @param string $name Property name or properties list.
  33. * @param mixed $value Property value.
  34. * @return style
  35. */
  36. protected function setVal($name, $value)
  37. {
  38. $name = static::formatStyleName($name);
  39. if($value === null)
  40. {
  41. $this->storedData[$name] = $value;
  42. return $this;
  43. }
  44. if(!is_bool($value)) $value = static::formatStyleValue($name, $value);
  45. $this->storedData[$name] = $value;
  46. return $this;
  47. }
  48. /**
  49. * @param \zin\node $node
  50. * @param string $blockName
  51. */
  52. public function apply($node, $blockName)
  53. {
  54. $style = array();
  55. $class = array();
  56. foreach ($this->storedData as $name => $value)
  57. {
  58. if(is_bool($value)) $class[$name] = $value;
  59. else $style[$name] = $value;
  60. }
  61. if($style) $node->setProp('style', $style);
  62. if($class) $node->setProp('class', $class);
  63. }
  64. /**
  65. * @param string|mixed[] $nameOrMap
  66. * @param mixed $value
  67. */
  68. public function var($nameOrMap, $value = null)
  69. {
  70. if(is_array($nameOrMap))
  71. {
  72. foreach($nameOrMap as $name => $val) $this->setVal('--' . $name, $val);
  73. return $this;
  74. }
  75. return $this->setVal('--' . $nameOrMap, $value);
  76. }
  77. /**
  78. * Magic static method for style property value.
  79. *
  80. * @access public
  81. * @param string $name - Property name.
  82. * @param array $args - Property values.
  83. * @return style
  84. */
  85. public static function __callStatic($name, $args)
  86. {
  87. $style = new style();
  88. $style->$name(...$args);
  89. return $style;
  90. }
  91. /**
  92. * @param string $name
  93. */
  94. public static function formatStyleName($name)
  95. {
  96. return strtolower(preg_replace('/(?<!^)[A-Z]/', '-$0', str_replace('_', '-', $name)));
  97. }
  98. /**
  99. * @param null|string|int|mixed[] $value
  100. * @param string $name
  101. */
  102. public static function formatStyleValue($name, $value)
  103. {
  104. if(is_array($value))
  105. {
  106. $valueList = array();
  107. foreach($value as $v)
  108. {
  109. $valueList[] = self::formatStyleValue($name, $v);
  110. }
  111. return implode(' ', $valueList);
  112. }
  113. if(is_null($value)) return '';
  114. if(!is_string($value) && is_numeric($value) && (str_ends_with($name, '-width') || str_ends_with($name, '-height') || str_ends_with($name, '-radius') || in_array($name, array('width', 'height', 'radius', 'top', 'left', 'right', 'bottom', 'inset'))))
  115. {
  116. $value .= 'px';
  117. }
  118. elseif(is_string($value) && str_starts_with($value, '--'))
  119. {
  120. $value = 'var(' . $value . ')';
  121. }
  122. return is_string($value) ? $value : (string)$value;
  123. }
  124. }
  125. /**
  126. * Set widget style attribute.
  127. *
  128. * @return set
  129. * @param mixed[]|string $name
  130. */
  131. function setStyle($name, $value = null)
  132. {
  133. $style = new style($name, $value);
  134. return $style;
  135. }