/lib/core/Tracker/Tabular/Schema/Column.php

https://gitlab.com/ElvisAns/tiki · PHP · 270 lines · 220 code · 44 blank · 6 comment · 14 complexity · 96b21a2ec971c0640f01b0efc94bdc81 MD5 · raw file

  1. <?php
  2. // (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
  3. //
  4. // All Rights Reserved. See copyright.txt for details and a complete list of authors.
  5. // Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
  6. // $Id$
  7. namespace Tracker\Tabular\Schema;
  8. class Column implements \JsonSerializable
  9. {
  10. const HEADER_PATTERN = '/\[(\*?)(\w+):([^\]]+)\]$/';
  11. private $permName;
  12. private $remoteField = '';
  13. private $label;
  14. private $mode;
  15. private $isPrimary = false;
  16. private $isReadOnly = false;
  17. private $isExportOnly = false;
  18. private $isUniqueKey = false;
  19. private $displayAlign = 'left';
  20. private $renderTransform;
  21. private $parseIntoTransform;
  22. private $querySources = [];
  23. private $incompatibilities = [];
  24. private $plainReplacement = null;
  25. public function __construct($permName, $mode)
  26. {
  27. $this->permName = $permName;
  28. $this->mode = $mode;
  29. $this->parseIntoTransform = function (&$info, $value) {
  30. };
  31. }
  32. public function getLabel()
  33. {
  34. return $this->label;
  35. }
  36. public function setLabel($label)
  37. {
  38. $this->label = $label;
  39. return $this;
  40. }
  41. public function getRemoteFields()
  42. {
  43. return preg_split('/\s*,\s*/', $this->remoteField);
  44. }
  45. public function getRemoteField()
  46. {
  47. return $this->remoteField;
  48. }
  49. public function setRemoteField($remoteField)
  50. {
  51. $this->remoteField = $remoteField;
  52. return $this;
  53. }
  54. public function getDisplayAlign()
  55. {
  56. return $this->displayAlign;
  57. }
  58. public function setDisplayAlign($align)
  59. {
  60. $this->displayAlign = $align;
  61. return $this;
  62. }
  63. public function addIncompatibility($field, $mode)
  64. {
  65. $this->incompatibilities[] = [$field, $mode];
  66. return $this;
  67. }
  68. public function setRenderTransform(callable $transform)
  69. {
  70. $this->renderTransform = $transform;
  71. return $this;
  72. }
  73. public function setParseIntoTransform(callable $transform)
  74. {
  75. $this->parseIntoTransform = $transform;
  76. return $this;
  77. }
  78. public function setPrimaryKey($pk)
  79. {
  80. $this->isPrimary = (bool) $pk;
  81. return $this;
  82. }
  83. public function setReadOnly($readOnly)
  84. {
  85. $this->isReadOnly = (bool) $readOnly;
  86. return $this;
  87. }
  88. public function setExportOnly($exportOnly)
  89. {
  90. $this->isExportOnly = (bool) $exportOnly;
  91. return $this;
  92. }
  93. public function setUniqueKey($uniqueKey)
  94. {
  95. $this->isUniqueKey = (bool) $uniqueKey;
  96. return $this;
  97. }
  98. public function setPlainReplacement($replacement)
  99. {
  100. $this->plainReplacement = $replacement;
  101. return $this;
  102. }
  103. public function is($field, $mode)
  104. {
  105. return $field == $this->permName && $mode == $this->mode;
  106. }
  107. public function isPrimaryKey()
  108. {
  109. return $this->isPrimary;
  110. }
  111. public function isReadOnly()
  112. {
  113. return $this->isReadOnly;
  114. }
  115. public function isExportOnly()
  116. {
  117. return $this->isExportOnly;
  118. }
  119. public function isUniqueKey()
  120. {
  121. return $this->isUniqueKey;
  122. }
  123. public function getField()
  124. {
  125. return $this->permName;
  126. }
  127. public function getMode()
  128. {
  129. return $this->mode;
  130. }
  131. public function getEncodedHeader($schema)
  132. {
  133. if ($this->isReadOnly || $schema->isSimpleHeaders()) {
  134. return $this->label;
  135. } else {
  136. $pk = $this->isPrimary ? '*' : '';
  137. return "{$this->label} [$pk{$this->permName}:{$this->mode}]";
  138. }
  139. }
  140. public function getPlainReplacement()
  141. {
  142. return $this->plainReplacement;
  143. }
  144. public function render($value)
  145. {
  146. return call_user_func_array($this->renderTransform, func_get_args());
  147. }
  148. public function parseInto(&$info, $value, $extra = null)
  149. {
  150. $c = $this->parseIntoTransform;
  151. if (! $this->isReadOnly) {
  152. $c($info, $value, $extra);
  153. }
  154. }
  155. public function addQuerySource($name, $field)
  156. {
  157. $this->querySources[$name] = $field;
  158. return $this;
  159. }
  160. public function getQuerySources()
  161. {
  162. return $this->querySources;
  163. }
  164. public function validateAgainst(\Tracker\Tabular\Schema $schema)
  165. {
  166. if ($this->isPrimary && $this->isReadOnly) {
  167. throw new \Exception(tr('Primary Key fields cannot be read-only.'));
  168. }
  169. $selfCount = 0;
  170. foreach ($schema->getColumns() as $column) {
  171. if ($column->is($this->permName, $this->mode)) {
  172. $selfCount++;
  173. }
  174. foreach ($this->incompatibilities as $entry) {
  175. list($field, $mode) = $entry;
  176. if ($column->is($field, $mode)) {
  177. // Skip incompatibility if either field is read-only
  178. if ($this->isReadOnly() || $column->isReadOnly()) {
  179. continue;
  180. }
  181. throw new \Exception(tr(
  182. 'Column "%0" cannot co-exist with "%1".',
  183. $column->getEncodedHeader($schema),
  184. $this->getEncodedHeader($this)
  185. ));
  186. }
  187. }
  188. }
  189. if ($selfCount > 1) {
  190. throw new \Exception(tr('Column "%0:%1" found multiple times.', $this->permName, $this->mode));
  191. }
  192. }
  193. public function withWrappedRenderTransform(callable $callback)
  194. {
  195. $column = new self($this->permName, $this->mode);
  196. $column->label = $this->label;
  197. $column->remoteField = $this->remoteField;
  198. $column->isPrimary = $this->isPrimary;
  199. $column->isReadOnly = $this->isReadOnly;
  200. $column->isExportOnly = $this->isExportOnly;
  201. $column->isUniqueKey = $this->isUniqueKey;
  202. $column->displayAlign = $this->displayAlign;
  203. $column->parseIntoTransform = $this->parseIntoTransform;
  204. $column->querySources = $this->querySources;
  205. $column->incompatibilities = $this->incompatibilities;
  206. $column->plainReplacement = $this->plainReplacement;
  207. $column->renderTransform = function () use ($callback) {
  208. $value = call_user_func_array($this->renderTransform, func_get_args());
  209. return $callback($value);
  210. };
  211. return $column;
  212. }
  213. public function jsonSerialize()
  214. {
  215. return [
  216. 'label' => $this->label,
  217. 'field' => $this->permName,
  218. 'mode' => $this->mode,
  219. 'displayAlign' => $this->displayAlign,
  220. 'isPrimary' => $this->isPrimary,
  221. 'isReadOnly' => $this->isReadOnly,
  222. 'isExportOnly' => $this->isExportOnly,
  223. 'isUniqueKey' => $this->isUniqueKey,
  224. ];
  225. }
  226. }