update
This commit is contained in:
37
ts/formats/base/base.decoder.ts
Normal file
37
ts/formats/base/base.decoder.ts
Normal file
@ -0,0 +1,37 @@
|
||||
import type { TInvoice } from '../../interfaces/common.js';
|
||||
import { ValidationLevel } from '../../interfaces/common.js';
|
||||
import type { ValidationResult } from '../../interfaces/common.js';
|
||||
|
||||
/**
|
||||
* Base decoder class that defines common decoding functionality
|
||||
* for all invoice format decoders
|
||||
*/
|
||||
export abstract class BaseDecoder {
|
||||
protected xml: string;
|
||||
|
||||
constructor(xml: string) {
|
||||
this.xml = xml;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes XML into a TInvoice object
|
||||
* @returns Promise resolving to a TInvoice object
|
||||
*/
|
||||
abstract decode(): Promise<TInvoice>;
|
||||
|
||||
/**
|
||||
* Gets letter data in the standard format
|
||||
* @returns Promise resolving to a TInvoice object
|
||||
*/
|
||||
public async getLetterData(): Promise<TInvoice> {
|
||||
return this.decode();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the raw XML content
|
||||
* @returns XML string
|
||||
*/
|
||||
public getXml(): string {
|
||||
return this.xml;
|
||||
}
|
||||
}
|
14
ts/formats/base/base.encoder.ts
Normal file
14
ts/formats/base/base.encoder.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import type { TInvoice } from '../../interfaces/common.js';
|
||||
|
||||
/**
|
||||
* Base encoder class that defines common encoding functionality
|
||||
* for all invoice format encoders
|
||||
*/
|
||||
export abstract class BaseEncoder {
|
||||
/**
|
||||
* Encodes a TInvoice object into XML
|
||||
* @param invoice TInvoice object to encode
|
||||
* @returns XML string
|
||||
*/
|
||||
abstract encode(invoice: TInvoice): Promise<string>;
|
||||
}
|
64
ts/formats/base/base.validator.ts
Normal file
64
ts/formats/base/base.validator.ts
Normal file
@ -0,0 +1,64 @@
|
||||
import { ValidationLevel } from '../../interfaces/common.js';
|
||||
import type { ValidationResult, ValidationError } from '../../interfaces/common.js';
|
||||
|
||||
/**
|
||||
* Base validator class that defines common validation functionality
|
||||
* for all invoice format validators
|
||||
*/
|
||||
export abstract class BaseValidator {
|
||||
protected xml: string;
|
||||
protected errors: ValidationError[] = [];
|
||||
|
||||
constructor(xml: string) {
|
||||
this.xml = xml;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates XML against the specified level of validation
|
||||
* @param level Validation level (syntax, semantic, business)
|
||||
* @returns Result of validation
|
||||
*/
|
||||
abstract validate(level?: ValidationLevel): ValidationResult;
|
||||
|
||||
/**
|
||||
* Gets all validation errors found during validation
|
||||
* @returns Array of validation errors
|
||||
*/
|
||||
public getValidationErrors(): ValidationError[] {
|
||||
return this.errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the document is valid
|
||||
* @returns True if no validation errors were found
|
||||
*/
|
||||
public isValid(): boolean {
|
||||
return this.errors.length === 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates XML against schema
|
||||
* @returns True if schema validation passed
|
||||
*/
|
||||
protected abstract validateSchema(): boolean;
|
||||
|
||||
/**
|
||||
* Validates business rules
|
||||
* @returns True if business rule validation passed
|
||||
*/
|
||||
protected abstract validateBusinessRules(): boolean;
|
||||
|
||||
/**
|
||||
* Adds an error to the validation errors list
|
||||
* @param code Error code
|
||||
* @param message Error message
|
||||
* @param location Location in the XML where the error occurred
|
||||
*/
|
||||
protected addError(code: string, message: string, location: string = ''): void {
|
||||
this.errors.push({
|
||||
code,
|
||||
message,
|
||||
location
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user