CodeRangeGenerator
in package
CodeRangeGenerator
Given a table "type" (e.g. 'kpi'), this class:
- Looks up the row in table
code_rangewhere code_range_table_name = :type - Ensures code_range_type contains 'MPA_PREFIX'
- Reads code_range_prefix (e.g. 'CMP-') to know the code prefix
- Scans the target data table (e.g.
kpi) for the highest numeric suffix after the prefix on the column<type>_code(e.g.kpi_code) where the format is EXACTLY "PREFIX-######" - Returns the next code, zero-padded to PAD (default 6)
Table of Contents
Constants
- PAD = 6
Properties
Methods
- __construct() : mixed
- nextCode() : string
- Returns the next available code for the given table type.
- escapeForMysqlRegex() : string
- Escape characters in a literal to be safely used inside a MySQL REGEXP (which uses a PCRE-like engine).
- loadCodeRangeRow() : array<string|int, mixed>|null
- Load the code_range row for the given table (type).
- normalizePrefix() : string
- Ensure prefix ends with exactly one '-' and no trailing spaces.
Constants
PAD
private
mixed
PAD
= 6
Properties
$db
private
MySql
$db
Methods
__construct()
public
__construct(MySql $db) : mixed
Parameters
- $db : MySql
nextCode()
Returns the next available code for the given table type.
public
nextCode(string $type) : string
Example: nextCode('kpi') => 'CMP-000082'
Parameters
- $type : string
-
Table name / type (e.g. 'kpi', 'unit', etc.)
Return values
string —Next code
escapeForMysqlRegex()
Escape characters in a literal to be safely used inside a MySQL REGEXP (which uses a PCRE-like engine).
private
escapeForMysqlRegex(string $literal) : string
Parameters
- $literal : string
Return values
stringloadCodeRangeRow()
Load the code_range row for the given table (type).
private
loadCodeRangeRow(string $type) : array<string|int, mixed>|null
Expects columns:
- code_range_table_name
- code_range_type (should contain 'MPA_PREFIX')
- code_range_prefix (e.g., 'CMP-' or 'CMP')
Parameters
- $type : string
Return values
array<string|int, mixed>|nullnormalizePrefix()
Ensure prefix ends with exactly one '-' and no trailing spaces.
private
normalizePrefix(string $prefix) : string
Examples: 'CMP' -> 'CMP-' 'CMP-' -> 'CMP-' 'CMP--' -> 'CMP-'
Parameters
- $prefix : string