BlackHorse
in package
Static Price Encoding Utility for Retail Inventory Management
Provides a simple substitution cipher for encoding decimal prices into alphabetic codes, allowing retailers to display encoded cost information on price stickers that staff can decode but customers cannot easily understand.
Encoding System Overview
The encoding uses any 10-character word with unique letters where each letter maps to a digit (1-9, 0). Prices are converted to cent values and then encoded letter-by-letter.
Key Features
- Substitution Cipher: Maps digits to letters using a customizable code word
- Flexible Encoding Length: Supports various price ranges via configurable output length
- Customer ID Encoding: Additional methods for encoding customer IDs with privacy offset
- Method Chaining: Fluent interface for setting code word and encoding in one statement
- Event Integration: Uses Primordyx EventManager for error reporting
Digit Mapping Example
For the default code word "BLACKHORSE":
- B=1, L=2, A=3, C=4, K=5, H=6, O=7, R=8, S=9, E=0
Usage Patterns
Chained Style
$encoded = BlackHorse::setCodeWord('NIGHTCRAWL')->encode(12.34); // "EEBLAC"
$price = BlackHorse::decode('EEBLAC'); // 12.34
Separate Style
BlackHorse::setCodeWord('WORKINGDAY');
$encode1 = BlackHorse::encode(1234.56);
$encode2 = BlackHorse::encode(987.54);
$decode1 = BlackHorse::decode('EEBLAC');
Customer ID Encoding
BlackHorse::setCodeWord('BLACKHORSE');
$publicId = BlackHorse::encodeCustomerId(47); // Adds offset of 25000
$customerId = BlackHorse::decodeCustomerId($publicId); // Returns 47
Example Code Words
Any 10-character combination with unique letters works:
- BLACKHORSE (default)
- THUNDERBOX
- NIGHTCRAWL
- WORKINGDAY
- PRODUCTKEY
- MASTERLOCK
Maximum Encodable Values by Length
- Length 4: Up to $99.99
- Length 6: Up to $9999.99 (default)
- Length 8: Up to $999999.99
Security Considerations
- Not cryptographically secure - designed for staff/customer separation only
- Should not be used for sensitive data protection
- Code word changes affect all subsequent operations globally
Tags
Table of Contents
Properties
- $codeWord : string
- Current code word being used for encoding/decoding
Methods
- decode() : float
- Decode an alphabetic code back to the original decimal price
- decodeCustomerId() : int
- Decode a public customer ID back to the internal customer ID
- encode() : string
- Encode a decimal price into an alphabetic code
- encodeCustomerId() : string
- Encode a customer ID into a public-facing obfuscated ID
- getCodeWord() : string
- Get the current code word being used for encoding/decoding
- setCodeWord() : string
- Set the code word for subsequent encode/decode operations
- validateCodeWord() : void
- Validate code word format
Properties
$codeWord
Current code word being used for encoding/decoding
private
static string
$codeWord
= 'BLACKHORSE'
Methods
decode()
Decode an alphabetic code back to the original decimal price
public
static decode(string $encoded) : float
Reverses the encoding process by converting each letter back to its corresponding digit using the current code word, then converting the resulting cent value back to decimal dollars.
Decoding Process
- Map each letter to digit: EELBAC → 001234
- Convert cents to dollars: 001234 → 12.34
Error Handling
Throws an exception if the encoded string contains any character that doesn't exist in the current code word. Also fires an event for logging purposes before throwing.
Parameters
- $encoded : string
-
Encoded alphabetic price string of any length
Tags
Return values
float —The decoded price in decimal dollars (e.g., 12.34)
decodeCustomerId()
Decode a public customer ID back to the internal customer ID
public
static decodeCustomerId(string $publicId) : int
Reverses the customer ID encoding by decoding the alphabetic string and removing the privacy offset to retrieve the original internal customer ID.
Error Handling
If the encoded public ID contains invalid characters, the underlying decode() method will throw an InvalidArgumentException. Consider wrapping calls in try-catch blocks when processing untrusted input.
Parameters
- $publicId : string
-
The encoded public customer ID to decode
Tags
Return values
int —The original internal customer ID
encode()
Encode a decimal price into an alphabetic code
public
static encode(float $price[, int $length = 6 ]) : string
Converts a decimal price (dollars.cents) into an encoded alphabetic string using the current code word as a substitution cipher. The price is first converted to cents, padded with leading zeros to the specified length, then each digit is mapped to its corresponding letter.
Encoding Process
- Convert price to cents: 12.34 → 1234
- Pad with zeros: 1234 → 001234 (for length 6)
- Map digits to letters: 001234 → EELBAC
Length Parameter Guidelines
- Length 4: For prices up to $99.99
- Length 6: For prices up to $9999.99 (default)
- Length 8: For prices up to $999999.99
Parameters
- $price : float
-
The decimal price to encode (e.g., 12.34 for $12.34)
- $length : int = 6
-
Number of characters in output (default: 6)
Tags
Return values
string —Encoded alphabetic price string of specified length
encodeCustomerId()
Encode a customer ID into a public-facing obfuscated ID
public
static encodeCustomerId(int $customerId) : string
Adds a privacy offset to a customer ID before encoding it, making it suitable for use in public URLs, order references, or anywhere you need to reference a customer without exposing their actual database ID.
Privacy Offset
The method adds 25000 to the customer ID before encoding. This ensures:
- Customer IDs don't start from obvious low numbers
- The encoded result is longer and less guessable
- Sequential customer IDs don't produce sequential codes
Parameters
- $customerId : int
-
The internal customer ID to encode
Tags
Return values
string —Encoded public customer ID string
getCodeWord()
Get the current code word being used for encoding/decoding
public
static getCodeWord() : string
Returns the currently active code word. Useful for debugging or when you need to verify which code word is currently in use.
Tags
Return values
string —Current code word in uppercase
setCodeWord()
Set the code word for subsequent encode/decode operations
public
static setCodeWord(string $codeWord) : string
Validates and sets a new code word that will be used for all subsequent encoding and decoding operations. The code word must contain exactly 10 unique letters. Converts the input to uppercase automatically.
Method Chaining
Returns the class name to enable fluent interface pattern:
$encoded = BlackHorse::setCodeWord('NIGHTCRAWL')->encode(12.34);
Global State Warning
Changing the code word affects ALL subsequent BlackHorse operations across the entire application until changed again.
Parameters
- $codeWord : string
-
Code word containing exactly 10 unique letters
Tags
Return values
string —Returns class name for method chaining
validateCodeWord()
Validate code word format
private
static validateCodeWord(string $codeWord) : void
Parameters
- $codeWord : string
-
Code word to validate