PageRenderTime 28ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/bizproc/lib/basetype/base.php

https://gitlab.com/alexprowars/bitrix
PHP | 866 lines | 516 code | 105 blank | 245 comment | 53 complexity | 6c616693eeb06dbe8fce65970d466059 MD5 | raw file
  1. <?php
  2. namespace Bitrix\Bizproc\BaseType;
  3. use Bitrix\Main;
  4. use Bitrix\Main\Localization\Loc;
  5. use Bitrix\Bizproc\FieldType;
  6. /**
  7. * Class Base
  8. * @package Bitrix\Bizproc\BaseType
  9. */
  10. class Base
  11. {
  12. /**
  13. * Gets the name of the field type.
  14. * @return string
  15. */
  16. public static function getName(): string
  17. {
  18. return static::getType();
  19. }
  20. /**
  21. * @return string
  22. */
  23. public static function getType()
  24. {
  25. return FieldType::STRING;
  26. }
  27. /** @var array $formats */
  28. protected static $formats = [
  29. 'printable' => [
  30. 'callable' => 'formatValuePrintable',
  31. 'separator' => ', ',
  32. ],
  33. ];
  34. /**
  35. * @param string $format
  36. * @return callable|null
  37. */
  38. protected static function getFormatCallable($format)
  39. {
  40. $format = mb_strtolower($format);
  41. $formats = static::getFormats();
  42. if (isset($formats[$format]['callable']))
  43. {
  44. $callable = $formats[$format]['callable'];
  45. if (is_string($callable))
  46. {
  47. $callable = [get_called_class(), $callable];
  48. }
  49. return $callable;
  50. }
  51. return null;
  52. }
  53. /**
  54. * @param string $format
  55. * @return string
  56. */
  57. protected static function getFormatSeparator($format)
  58. {
  59. $format = mb_strtolower($format);
  60. $separator = ', '; //default - coma
  61. $formats = static::getFormats();
  62. if (isset($formats[$format]['separator']))
  63. {
  64. $separator = $formats[$format]['separator'];
  65. }
  66. return $separator;
  67. }
  68. /**
  69. * @param string $name Format name.
  70. * @param array $options Format options.
  71. * @return void
  72. * @throws Main\ArgumentException
  73. */
  74. public static function addFormat($name, array $options)
  75. {
  76. $name = mb_strtolower($name);
  77. if (empty($options['callable']))
  78. {
  79. throw new Main\ArgumentException('Callable property in format options is not set.');
  80. }
  81. static::$formats[$name] = $options;
  82. }
  83. /**
  84. * Get formats list.
  85. * @return array
  86. */
  87. public static function getFormats()
  88. {
  89. return static::$formats;
  90. }
  91. /**
  92. * Normalize single value.
  93. *
  94. * @param FieldType $fieldType Document field type.
  95. * @param mixed $value Field value.
  96. * @return mixed Normalized value
  97. */
  98. public static function toSingleValue(FieldType $fieldType, $value)
  99. {
  100. return $value;
  101. }
  102. /**
  103. * @param FieldType $fieldType Document field type.
  104. * @param mixed $value Field value.
  105. * @param string $format Format name.
  106. * @return string
  107. */
  108. public static function formatValueMultiple(FieldType $fieldType, $value, $format = 'printable')
  109. {
  110. $value = (array)$value;
  111. foreach ($value as $k => $v)
  112. {
  113. $value[$k] = static::formatValueSingle($fieldType, $v, $format);
  114. }
  115. return implode(static::getFormatSeparator($format), $value);
  116. }
  117. /**
  118. * @param FieldType $fieldType Document field type.
  119. * @param mixed $value Field value.
  120. * @param string $format Format name.
  121. * @return mixed|null
  122. */
  123. public static function formatValueSingle(FieldType $fieldType, $value, $format = 'printable')
  124. {
  125. $callable = static::getFormatCallable($format);
  126. $value = static::toSingleValue($fieldType, $value);
  127. if (is_callable($callable))
  128. {
  129. return call_user_func($callable, $fieldType, $value);
  130. }
  131. //return original value if format not found
  132. return $value;
  133. }
  134. /**
  135. * @param FieldType $fieldType
  136. * @param $value
  137. * @return string
  138. */
  139. protected static function formatValuePrintable(FieldType $fieldType, $value)
  140. {
  141. return static::convertValueSingle($fieldType, $value, StringType::class);
  142. }
  143. /**
  144. * @param FieldType $fieldType Document field type.
  145. * @param mixed $value Field value.
  146. * @param string $toTypeClass Type class name.
  147. * @return array
  148. */
  149. public static function convertValueMultiple(FieldType $fieldType, $value, $toTypeClass)
  150. {
  151. $value = (array)$value;
  152. foreach ($value as $k => $v)
  153. {
  154. $value[$k] = static::convertValueSingle($fieldType, $v, $toTypeClass);
  155. }
  156. return $value;
  157. }
  158. /**
  159. * @param FieldType $fieldType Document field type.
  160. * @param mixed $value Field value.
  161. * @param string $toTypeClass Type class name.
  162. * @return bool|int|float|string
  163. * @throws Main\ArgumentException
  164. */
  165. public static function convertValueSingle(FieldType $fieldType, $value, $toTypeClass)
  166. {
  167. $value = static::toSingleValue($fieldType, $value);
  168. /** @var Base $toTypeClass */
  169. $result = static::convertTo($fieldType, $value, $toTypeClass);
  170. if ($result === null)
  171. $result = $toTypeClass::convertFrom($fieldType, $value, get_called_class());
  172. if ($result !== null)
  173. $fieldType->setTypeClass($toTypeClass);
  174. return $result !== null ? $result : $value;
  175. }
  176. /**
  177. * @param FieldType $fieldType Document field type.
  178. * @param mixed $value Field value.
  179. * @param string $toTypeClass Type class name.
  180. * @return null
  181. */
  182. public static function convertTo(FieldType $fieldType, $value, $toTypeClass)
  183. {
  184. return null;
  185. }
  186. /**
  187. * @param FieldType $fieldType Document field type.
  188. * @param mixed $value Field value.
  189. * @param string $fromTypeClass Type class name.
  190. * @return null
  191. */
  192. public static function convertFrom(FieldType $fieldType, $value, $fromTypeClass)
  193. {
  194. return null;
  195. }
  196. /**
  197. * Return conversion map for current type.
  198. * @return array Map.
  199. */
  200. public static function getConversionMap()
  201. {
  202. return [
  203. //to
  204. [],
  205. //from
  206. [],
  207. ];
  208. }
  209. /**
  210. * @param FieldType $fieldType Document field type.
  211. * @param array $baseValue Base value.
  212. * @param mixed $appendValue Value to append.
  213. * @return mixed Merge result.
  214. */
  215. public static function mergeValue(FieldType $fieldType, array $baseValue, $appendValue): array
  216. {
  217. if (\CBPHelper::isEmptyValue($baseValue))
  218. {
  219. return (array)$appendValue;
  220. }
  221. if (!is_array($appendValue))
  222. {
  223. $baseValue[] = $appendValue;
  224. return $baseValue;
  225. }
  226. $isSimple = !\CBPHelper::isAssociativeArray($baseValue) && !\CBPHelper::isAssociativeArray($appendValue);
  227. $result = $isSimple ? array_merge($baseValue, $appendValue) : $baseValue + $appendValue;
  228. if ($isSimple)
  229. {
  230. $result = array_values(array_unique($result));
  231. }
  232. return $result;
  233. }
  234. /**
  235. * @var array
  236. */
  237. protected static $errors = [];
  238. /**
  239. * @param mixed $error Error description.
  240. */
  241. public static function addError($error)
  242. {
  243. static::$errors[] = $error;
  244. }
  245. /**
  246. * @param array $errors Errors description.
  247. * @return void
  248. */
  249. public static function addErrors(array $errors)
  250. {
  251. static::$errors = array_merge(static::$errors, $errors);
  252. }
  253. /**
  254. * @return array
  255. */
  256. public static function getErrors()
  257. {
  258. return static::$errors;
  259. }
  260. /**
  261. * Clean errors
  262. */
  263. protected static function cleanErrors()
  264. {
  265. static::$errors = [];
  266. }
  267. /**
  268. * @param array $field
  269. * @return string
  270. */
  271. protected static function generateControlId(array $field)
  272. {
  273. $id = 'id_' . $field['Field'];
  274. $index = isset($field['Index']) ? $field['Index'] : null;
  275. if ($index !== null)
  276. {
  277. $id .= '__n' . $index . '_';
  278. }
  279. return $id;
  280. }
  281. /**
  282. * @param array $field
  283. * @return string
  284. */
  285. protected static function generateControlName(array $field)
  286. {
  287. $name = $field['Field'];
  288. $index = isset($field['Index']) ? $field['Index'] : null;
  289. if ($index !== null)
  290. {
  291. //new multiple name style
  292. $name .= '[]';
  293. }
  294. return $name;
  295. }
  296. protected static function generateControlClassName(FieldType $fieldType, array $field)
  297. {
  298. $prefix = 'bizproc-type-control';
  299. $classes = [$prefix];
  300. $classes[] = $prefix . '-' . static::getType();
  301. if ($fieldType->isMultiple())
  302. {
  303. $classes[] = $prefix . '-multiple';
  304. }
  305. if ($fieldType->isRequired())
  306. {
  307. $classes[] = $prefix . '-required';
  308. }
  309. return implode(' ', $classes);
  310. }
  311. /**
  312. * @param array $controls
  313. * @param string $wrapperId
  314. * @return string
  315. */
  316. protected static function wrapCloneableControls(array $controls, $wrapperId)
  317. {
  318. $wrapperId = (string)$wrapperId;
  319. $renderResult = '<table width="100%" border="0" cellpadding="2" cellspacing="2" id="BizprocCloneable_'
  320. . htmlspecialcharsbx($wrapperId) . '">';
  321. foreach ($controls as $control)
  322. {
  323. $renderResult .= '<tr><td>' . $control . '</td></tr>';
  324. }
  325. $renderResult .= '</table>';
  326. $renderResult .= sprintf(
  327. '<input type="button" value="%s" onclick="BX.Bizproc.cloneTypeControl(\'BizprocCloneable_%s\')"/><br />',
  328. Loc::getMessage('BPDT_BASE_ADD'),
  329. htmlspecialcharsbx($wrapperId)
  330. );
  331. return $renderResult;
  332. }
  333. /**
  334. * @param FieldType $fieldType
  335. * @param array $field
  336. * @param array $controls
  337. * @return string
  338. */
  339. protected static function renderPublicMultipleWrapper(FieldType $fieldType, array $field, array $controls)
  340. {
  341. $messageAdd = Loc::getMessage('BPDT_BASE_ADD');
  342. $name = Main\Text\HtmlFilter::encode(\CUtil::jsEscape(static::generateControlName($field)));
  343. $property = Main\Text\HtmlFilter::encode(Main\Web\Json::encode($fieldType->getProperty()));
  344. $renderResult = implode('', $controls) . <<<HTML
  345. <div>
  346. <a onclick="BX.Bizproc.FieldType.cloneControl({$property}, '{$name}', this.parentNode); return false;"
  347. class="bizproc-type-control-clone-btn">
  348. {$messageAdd}
  349. </a>
  350. </div>
  351. HTML;
  352. return $renderResult;
  353. }
  354. /**
  355. * Low-level control rendering method
  356. * @param FieldType $fieldType
  357. * @param array $field
  358. * @param mixed $value
  359. * @param bool $allowSelection
  360. * @param int $renderMode
  361. * @return string - HTML rendering
  362. */
  363. protected static function renderControl(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
  364. {
  365. $name = static::generateControlName($field);
  366. $controlId = static::generateControlId($field);
  367. $className = static::generateControlClassName($fieldType, $field);
  368. if ($renderMode&FieldType::RENDER_MODE_PUBLIC)
  369. {
  370. $selectorAttributes = '';
  371. if ($allowSelection)
  372. {
  373. $selectorAttributes = sprintf(
  374. 'data-role="inline-selector-target" data-property="%s" ',
  375. htmlspecialcharsbx(Main\Web\Json::encode($fieldType->getProperty()))
  376. );
  377. }
  378. return sprintf(
  379. '<input type="text" class="%s" name="%s" value="%s" placeholder="%s" %s/>',
  380. htmlspecialcharsbx($className),
  381. htmlspecialcharsbx($name),
  382. htmlspecialcharsbx((string)$value),
  383. htmlspecialcharsbx($fieldType->getDescription()),
  384. $selectorAttributes
  385. );
  386. }
  387. // example: control rendering
  388. return sprintf(
  389. '<input type="text" class="%s" size="40" id="%s" name="%s" value="%s"/>',
  390. htmlspecialcharsbx($className),
  391. htmlspecialcharsbx($controlId),
  392. htmlspecialcharsbx($name),
  393. htmlspecialcharsbx((string)$value)
  394. );
  395. }
  396. /**
  397. * @param int $renderMode Control render mode.
  398. * @return bool
  399. */
  400. public static function canRenderControl($renderMode)
  401. {
  402. if ($renderMode&FieldType::RENDER_MODE_MOBILE)
  403. {
  404. return false;
  405. }
  406. return true;
  407. }
  408. /**
  409. * @param FieldType $fieldType Document field type.
  410. * @param array $field Form field.
  411. * @param mixed $value Field value.
  412. * @param bool $allowSelection Allow selection flag.
  413. * @param int $renderMode Control render mode.
  414. * @return string
  415. */
  416. public static function renderControlSingle(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
  417. {
  418. $value = static::toSingleValue($fieldType, $value);
  419. $selectorValue = null;
  420. if ($allowSelection && \CBPActivity::isExpression($value))
  421. {
  422. $selectorValue = $value;
  423. $value = null;
  424. }
  425. $renderResult = static::renderControl($fieldType, $field, $value, $allowSelection, $renderMode);
  426. if ($allowSelection)
  427. {
  428. $renderResult .= static::renderControlSelector($field, $selectorValue, true, '', $fieldType);
  429. }
  430. return $renderResult;
  431. }
  432. /**
  433. * @param FieldType $fieldType Document field type.
  434. * @param array $field Form field.
  435. * @param mixed $value Field value.
  436. * @param bool $allowSelection Allow selection flag.
  437. * @param int $renderMode Control render mode.
  438. * @return string
  439. */
  440. public static function renderControlMultiple(FieldType $fieldType, array $field, $value, $allowSelection, $renderMode)
  441. {
  442. $selectorValue = null;
  443. $typeValue = [];
  444. if (!is_array($value) || is_array($value) && \CBPHelper::isAssociativeArray($value))
  445. {
  446. $value = [$value];
  447. }
  448. foreach ($value as $v)
  449. {
  450. if (\CBPActivity::isExpression($v))
  451. {
  452. $selectorValue = $v;
  453. }
  454. else
  455. {
  456. $typeValue[] = $v;
  457. }
  458. }
  459. // need to show at least one control
  460. if (empty($typeValue))
  461. {
  462. $typeValue[] = null;
  463. }
  464. $controls = [];
  465. foreach ($typeValue as $k => $v)
  466. {
  467. $singleField = $field;
  468. $singleField['Index'] = $k;
  469. $controls[] = static::renderControl(
  470. $fieldType,
  471. $singleField,
  472. $v,
  473. $allowSelection,
  474. $renderMode
  475. );
  476. }
  477. if ($renderMode&FieldType::RENDER_MODE_PUBLIC)
  478. {
  479. $renderResult = static::renderPublicMultipleWrapper($fieldType, $field, $controls);
  480. }
  481. else
  482. {
  483. $renderResult = static::wrapCloneableControls($controls, static::generateControlName($field));
  484. }
  485. if ($allowSelection)
  486. {
  487. $renderResult .= static::renderControlSelector($field, $selectorValue, true, '', $fieldType);
  488. }
  489. return $renderResult;
  490. }
  491. /**
  492. * @param array $field
  493. * @param null|string $value
  494. * @param bool $showInput
  495. * @param string $selectorMode
  496. * @param FieldType $fieldType
  497. * @return string
  498. */
  499. protected static function renderControlSelector(array $field, $value = null, $showInput = false, $selectorMode = '', FieldType $fieldType = null)
  500. {
  501. $html = '';
  502. $controlId = static::generateControlId($field);
  503. $name = static::generateControlName($field);
  504. if ($showInput)
  505. {
  506. if ($showInput !== 'combine')
  507. {
  508. $controlId = $controlId . '_text';
  509. $name = static::generateControlName($field) . '_text';
  510. }
  511. $cols = 70;
  512. $rows = max((static::getType() === FieldType::TEXT ? 5 : 1), min(5, ceil(mb_strlen((string)$value)) / $cols));
  513. $html = '<table cellpadding="0" cellspacing="0" border="0" width="100%" style="margin: 2px 0"><tr><td valign="top"><textarea ';
  514. $html .= 'rows="' . $rows . '" ';
  515. $html .= 'cols="' . $cols . '" ';
  516. $html .= 'name="' . htmlspecialcharsbx($name) . '" ';
  517. $html .= 'id="' . htmlspecialcharsbx($controlId) . '" ';
  518. $html .= 'style="width: 100%"';
  519. $html .= '>' . htmlspecialcharsbx((string)$value);
  520. $html .= '</textarea></td>';
  521. $html .= '<td valign="top" style="padding-left:4px" width="30">';
  522. }
  523. $html .= static::renderControlSelectorButton($controlId, $fieldType, $selectorMode);
  524. if ($showInput)
  525. {
  526. $html .= '</td></tr></table>';
  527. }
  528. return $html;
  529. }
  530. protected static function renderControlSelectorButton($controlId, FieldType $fieldType, $selectorMode = '')
  531. {
  532. $baseType = $fieldType ? $fieldType->getBaseType() : null;
  533. $selectorProps = Main\Web\Json::encode([
  534. 'controlId' => $controlId,
  535. 'baseType' => $baseType,
  536. 'type' => $fieldType ? $fieldType->getType() : null,
  537. 'documentType' => $fieldType ? $fieldType->getDocumentType() : null,
  538. 'documentId' => $fieldType ? $fieldType->getDocumentId() : null,
  539. ]);
  540. return sprintf(
  541. '<input type="button" value="..." onclick="BPAShowSelector(\'%s\', \'%s\', %s, null, %s);" data-role="bp-selector-button" data-bp-selector-props="%s">',
  542. \CUtil::jsEscape(htmlspecialcharsbx($controlId)),
  543. \CUtil::jsEscape(htmlspecialcharsbx($baseType)),
  544. $selectorMode ? '\'' . \CUtil::jsEscape(htmlspecialcharsbx($selectorMode)) . '\'' : 'null',
  545. htmlspecialcharsbx(Main\Web\Json::encode($fieldType ? $fieldType->getDocumentType() : null)),
  546. htmlspecialcharsbx($selectorProps)
  547. );
  548. }
  549. /**
  550. * @param FieldType $fieldType Document field type.
  551. * @param string $callbackFunctionName Client callback function name.
  552. * @param mixed $value Field value.
  553. * @return string
  554. */
  555. public static function renderControlOptions(FieldType $fieldType, $callbackFunctionName, $value)
  556. {
  557. return '';
  558. }
  559. /**
  560. * @param FieldType $fieldType
  561. * @param array $field
  562. * @param array $request
  563. * @return null|mixed
  564. */
  565. protected static function extractValue(FieldType $fieldType, array $field, array $request)
  566. {
  567. $name = $field['Field'];
  568. $value = isset($request[$name]) ? $request[$name] : null;
  569. $fieldIndex = isset($field['Index']) ? $field['Index'] : null;
  570. if (is_array($value) && !\CBPHelper::isAssociativeArray($value))
  571. {
  572. if ($fieldIndex !== null)
  573. {
  574. $value = isset($value[$fieldIndex]) ? $value[$fieldIndex] : null;
  575. }
  576. else
  577. {
  578. reset($value);
  579. $value = current($value);
  580. }
  581. }
  582. return $value;
  583. }
  584. /**
  585. * @param FieldType $fieldType Document field type.
  586. * @param array $field Form field.
  587. * @param array $request Request data.
  588. * @return mixed|null
  589. */
  590. public static function extractValueSingle(FieldType $fieldType, array $field, array $request)
  591. {
  592. static::cleanErrors();
  593. $result = static::extractValue($fieldType, $field, $request);
  594. if ($result === null || $result === '')
  595. {
  596. $nameText = $field['Field'] . '_text';
  597. $text = isset($request[$nameText]) ? $request[$nameText] : null;
  598. if (\CBPActivity::isExpression($text))
  599. $result = $text;
  600. }
  601. return $result;
  602. }
  603. /**
  604. * @param FieldType $fieldType Document field type.
  605. * @param array $field Form field.
  606. * @param array $request Request data.
  607. * @return array
  608. */
  609. public static function extractValueMultiple(FieldType $fieldType, array $field, array $request)
  610. {
  611. static::cleanErrors();
  612. $name = $field['Field'];
  613. $value = isset($request[$name]) ? $request[$name] : [];
  614. if (!is_array($value) || is_array($value) && \CBPHelper::isAssociativeArray($value))
  615. {
  616. $value = [$value];
  617. }
  618. foreach ($value as $k => $v)
  619. {
  620. $field['Index'] = $k;
  621. $result = static::extractValue($fieldType, $field, $request);
  622. if ($result === null || $result === '')
  623. {
  624. unset($value[$k]);
  625. }
  626. else
  627. {
  628. $value[$k] = $result;
  629. }
  630. }
  631. //append selector value
  632. $nameText = $field['Field'] . '_text';
  633. $text = isset($request[$nameText]) ? $request[$nameText] : null;
  634. if (\CBPActivity::isExpression($text))
  635. {
  636. $value[] = $text;
  637. }
  638. return array_values($value);
  639. }
  640. /**
  641. * @param FieldType $fieldType Document field type.
  642. * @param mixed $value Field value.
  643. * @return void
  644. */
  645. public static function clearValueSingle(FieldType $fieldType, $value)
  646. {
  647. //Method fires when workflow was complete
  648. }
  649. /**
  650. * @param FieldType $fieldType Document field type.
  651. * @param mixed $value Field value.
  652. * @return void
  653. */
  654. public static function clearValueMultiple(FieldType $fieldType, $value)
  655. {
  656. if (!is_array($value) || is_array($value) && \CBPHelper::isAssociativeArray($value))
  657. {
  658. $value = [$value];
  659. }
  660. foreach ($value as $v)
  661. {
  662. static::clearValueSingle($fieldType, $v);
  663. }
  664. }
  665. /**
  666. * @param FieldType $fieldType Document field type.
  667. * @param string $context Context identification (Document, Variable etc.)
  668. * @param mixed $value Field value.
  669. * @return mixed
  670. */
  671. public static function internalizeValue(FieldType $fieldType, $context, $value)
  672. {
  673. return $value;
  674. }
  675. /**
  676. * @param FieldType $fieldType Document field type.
  677. * @param string $context Context identification (Document, Variable etc.)
  678. * @param mixed $value Field value.
  679. * @return mixed
  680. */
  681. public static function internalizeValueSingle(FieldType $fieldType, $context, $value)
  682. {
  683. return static::internalizeValue($fieldType, $context, $value);
  684. }
  685. /**
  686. * @param FieldType $fieldType Document field type.
  687. * @param string $context Context identification (Document, Variable etc.)
  688. * @param mixed $value Field value.
  689. * @return mixed
  690. */
  691. public static function internalizeValueMultiple(FieldType $fieldType, $context, $value)
  692. {
  693. if (is_array($value))
  694. {
  695. foreach ($value as $k => $v)
  696. {
  697. $value[$k] = static::internalizeValue($fieldType, $context, $v);
  698. }
  699. }
  700. return $value;
  701. }
  702. /**
  703. * @param FieldType $fieldType Document field type.
  704. * @param string $context Context identification (Document, Variable etc.)
  705. * @param mixed $value Field value.
  706. * @return mixed
  707. */
  708. public static function externalizeValue(FieldType $fieldType, $context, $value)
  709. {
  710. if (is_object($value) && method_exists($value, '__toString'))
  711. {
  712. return (string)$value;
  713. }
  714. return $value;
  715. }
  716. /**
  717. * @param FieldType $fieldType Document field type.
  718. * @param string $context Context identification (Document, Variable etc.)
  719. * @param mixed $value Field value.
  720. * @return mixed
  721. */
  722. public static function externalizeValueSingle(FieldType $fieldType, $context, $value)
  723. {
  724. return static::externalizeValue($fieldType, $context, $value);
  725. }
  726. /**
  727. * @param FieldType $fieldType Document field type.
  728. * @param string $context Context identification (Document, Variable etc.)
  729. * @param mixed $value Field value.
  730. * @return mixed
  731. */
  732. public static function externalizeValueMultiple(FieldType $fieldType, $context, $value)
  733. {
  734. if (!is_array($value) || \CBPHelper::isAssociativeArray($value))
  735. {
  736. $value = [$value];
  737. }
  738. foreach ($value as $k => $v)
  739. {
  740. $value[$k] = static::externalizeValue($fieldType, $context, $v);
  741. }
  742. return $value;
  743. }
  744. /**
  745. * @param mixed $valueA First value.
  746. * @param mixed $valueB Second value.
  747. * @return int Returns 1, -1 or 0
  748. */
  749. public static function compareValues($valueA, $valueB)
  750. {
  751. if ($valueA > $valueB)
  752. {
  753. return 1;
  754. }
  755. if ($valueA < $valueB)
  756. {
  757. return -1;
  758. }
  759. return 0;
  760. }
  761. }