From fee740479591122a1a2cbbae204ca0d0f429fc0e Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 18 May 2023 17:44:51 -0700 Subject: [PATCH] (REF) CodeGen - Split "OptionGroup" and "SqlData" --- CRM/Core/CodeGen/AbstractSqlData.php | 142 +++++++++++++++++++++++++++ CRM/Core/CodeGen/OptionGroup.php | 119 +--------------------- CRM/Core/CodeGen/SqlData.php | 40 ++++++++ 3 files changed, 185 insertions(+), 116 deletions(-) create mode 100644 CRM/Core/CodeGen/AbstractSqlData.php create mode 100644 CRM/Core/CodeGen/SqlData.php diff --git a/CRM/Core/CodeGen/AbstractSqlData.php b/CRM/Core/CodeGen/AbstractSqlData.php new file mode 100644 index 0000000000..019fcd720b --- /dev/null +++ b/CRM/Core/CodeGen/AbstractSqlData.php @@ -0,0 +1,142 @@ +sortKey !== $b->sortKey) { + return strnatcmp($a->sortKey, $b->sortKey); + } + else { + return strnatcmp($a->metadata['name'], $b->metadata['name']); + } + } + + abstract public function toSQL(): string; + + /** + * Default values to apply to each record. + * + * @var array + */ + protected $defaults = []; + + protected $rows = []; + + protected $syncRules = []; + + /** + * @var string + */ + protected $sortKey; + + /** + * Copy fields + * + * @param string $mode + * copy|fill + * @param array $fields + * Array(string $srcField => string $destField). + * Ex: ['name' => 'label'] + * @return $this + */ + public function syncColumns(string $mode, array $fields) { + foreach ($fields as $from => $to) { + $this->syncRules[] = [$mode, $from, $to]; + } + return $this; + } + + public function addDefaults(array $fields) { + $this->defaults = array_merge($this->defaults, $fields); + return $this; + } + + /** + * Add a bunch of values to the option-group. + * + * @param array $optionValues + * List of option-value records. + * Ex: [ + * ['name' => 'foo_bar', 'label' => ts('Foo Bar')], + * ['name' => 'whiz_bang', 'label' => ts('Whiz Bang')], + * ] + * @return $this + */ + public function addValues(array $optionValues) { + $this->rows = array_merge($this->rows, $optionValues); + return $this; + } + + /** + * Add a bunch of values to the option-group using a tabular notation. + * + * @param array $header + * Ex: ['name', 'label'] + * @param array $optionValues + * A list of option-value records (aligned with the header). + * + * Ex: [ + * ['foo_bar', ts('Foo Bar')] + * ['whiz_bang', ts('Whiz Bang')] + * ] + * + * Additionally, to address outliers that don't fit tabular form, you may add key-value pairs. + * + * Ex: ['whiz_bang', ts('Whiz Bang'), 'component_id' => 100] + * + * @return $this + */ + public function addValueTable(array $header, array $optionValues) { + foreach ($optionValues as $optionValue) { + $row = []; + foreach ($optionValue as $key => $value) { + if (is_numeric($key)) { + $key = $header[$key]; + } + $row[$key] = $value; + } + $this->rows[] = $row; + } + return $this; + } + + /** + * @param array $row + * @return array + * Updated row + */ + protected function applySyncRules(array $row): array { + foreach ($this->syncRules as $syncRule) { + [$mode, $from, $to] = $syncRule; + switch ($mode) { + case 'copy': + $row[$to] = $row[$from]; + break; + + case 'fill': + if (array_key_exists($from, $row) && !array_key_exists($to, $row)) { + $row[$to] = $row[$from]; + } + break; + + default: + throw new \RuntimeException("Invalid sync mod: $mode"); + } + } + return $row; + } + +} diff --git a/CRM/Core/CodeGen/OptionGroup.php b/CRM/Core/CodeGen/OptionGroup.php index 01114bd70f..9cb2af64aa 100644 --- a/CRM/Core/CodeGen/OptionGroup.php +++ b/CRM/Core/CodeGen/OptionGroup.php @@ -4,23 +4,7 @@ * @internal * This class may change radically if doing so helps with the installer or upgrader. */ -class CRM_Core_CodeGen_OptionGroup { - - /** - * Determine the relative order of two option-groups. - * - * @param \CRM_Core_CodeGen_OptionGroup $a - * @param \CRM_Core_CodeGen_OptionGroup $b - * @return int - */ - public static function compare(CRM_Core_CodeGen_OptionGroup $a, CRM_Core_CodeGen_OptionGroup $b): int { - if ($a->sortKey !== $b->sortKey) { - return strnatcmp($a->sortKey, $b->sortKey); - } - else { - return strnatcmp($a->metadata['name'], $b->metadata['name']); - } - } +class CRM_Core_CodeGen_OptionGroup extends CRM_Core_CodeGen_AbstractSqlData { /** * OptionGroup properties. @@ -28,7 +12,7 @@ class CRM_Core_CodeGen_OptionGroup { * @var array * @internal */ - public $metadata = [ + protected $metadata = [ 'is_active' => 1, 'is_reserved' => 1, 'option_value_fields' => 'name,label,description', @@ -56,15 +40,6 @@ class CRM_Core_CodeGen_OptionGroup { protected $var; - protected $rows = []; - - protected $syncRules = []; - - /** - * @var string - */ - private $sortKey; - public static function create(string $name, ?string $sortKey = NULL): CRM_Core_CodeGen_OptionGroup { $og = new static(); $og->metadata['name'] = $name; @@ -85,99 +60,11 @@ class CRM_Core_CodeGen_OptionGroup { return $this; } - /** - * Copy fields - * - * @param string $mode - * copy|fill - * @param array $fields - * Array(string $srcField => string $destField). - * Ex: ['name' => 'label'] - * @return $this - */ - public function syncColumns(string $mode, array $fields): CRM_Core_CodeGen_OptionGroup { - foreach ($fields as $from => $to) { - $this->syncRules[] = [$mode, $from, $to]; - } - return $this; - } - - public function addDefaults(array $fields): CRM_Core_CodeGen_OptionGroup { - $this->defaults = array_merge($this->defaults, $fields); - return $this; - } - - /** - * Add a bunch of values to the option-group. - * - * @param array $optionValues - * List of option-value records. - * Ex: [ - * ['name' => 'foo_bar', 'label' => ts('Foo Bar')], - * ['name' => 'whiz_bang', 'label' => ts('Whiz Bang')], - * ] - * @return $this - */ - public function addValues(array $optionValues) : CRM_Core_CodeGen_OptionGroup { - $this->rows = array_merge($this->rows, $optionValues); - return $this; - } - - /** - * Add a bunch of values to the option-group using a tabular notation. - * - * @param array $header - * Ex: ['name', 'label'] - * @param array $optionValues - * A list of option-value records (aligned with the header). - * - * Ex: [ - * ['foo_bar', ts('Foo Bar')] - * ['whiz_bang', ts('Whiz Bang')] - * ] - * - * Additionally, to address outliers that don't fit tabular form, you may add key-value pairs. - * - * Ex: ['whiz_bang', ts('Whiz Bang'), 'component_id' => 100] - * - * @return $this - */ - public function addValueTable(array $header, array $optionValues) : CRM_Core_CodeGen_OptionGroup { - foreach ($optionValues as $optionValue) { - $row = []; - foreach ($optionValue as $key => $value) { - if (is_numeric($key)) { - $key = $header[$key]; - } - $row[$key] = $value; - } - $this->rows[] = $row; - } - return $this; - } - public function toArray(): array { $position = 1; $result = []; foreach ($this->rows as $row) { - foreach ($this->syncRules as $syncRule) { - [$mode, $from, $to] = $syncRule; - switch ($mode) { - case 'copy': - $row[$to] = $row[$from]; - break; - - case 'fill': - if (array_key_exists($from, $row) && !array_key_exists($to, $row)) { - $row[$to] = $row[$from]; - } - break; - - default: - throw new \RuntimeException("Invalid sync mod: $mode"); - } - } - + $row = $this->applySyncRules($row); $result[] = array_merge( ['option_group_id' => new CRM_Utils_SQL_Literal($this->var), 'value' => $position, 'weight' => $position], $this->defaults, diff --git a/CRM/Core/CodeGen/SqlData.php b/CRM/Core/CodeGen/SqlData.php new file mode 100644 index 0000000000..058a005ace --- /dev/null +++ b/CRM/Core/CodeGen/SqlData.php @@ -0,0 +1,40 @@ +table = $table; + return $sqlData; + } + + public function toArray(): array { + $result = []; + foreach ($this->rows as $row) { + $result[] = array_merge($this->defaults, $this->applySyncRules($row)); + } + return $result; + } + + public function toSQL(): string { + $result = ''; + $rows = $this->toArray(); + if ($rows) { + $result .= CRM_Utils_SQL_Insert::into($this->table) + ->allowLiterals() + ->rows($rows) + ->toSQL() . ";\n"; + } + return $result; + } + +} -- 2.25.1