diff --git a/l18n b/l18n index 974fc86..eb9834c 160000 --- a/l18n +++ b/l18n @@ -1 +1 @@ -Subproject commit 974fc860c69c644d91366b6f2b4b261b401e954f +Subproject commit eb9834ca20aebcb61b46f9002d45f9ec7404b0b0 diff --git a/src/MDMeasurementUnitInterface.php b/src/MDMeasurementUnitInterface.php new file mode 100644 index 0000000..215a059 --- /dev/null +++ b/src/MDMeasurementUnitInterface.php @@ -0,0 +1,21 @@ + + */ +declare(strict_types = 1); + +/** + * Describes an interface for a measurement units. + */ +interface MDMeasurementUnitInterface { + /** + * Returns the measurement calculated down to the base unit (e.g. mm for lengths). + * + * @param float $value Measurement value. + * + * @return float + */ + public function convertToBaseUnit(float $value):float; +} diff --git a/src/classes/MDMeasurement.php b/src/classes/MDMeasurement.php new file mode 100644 index 0000000..d078a28 --- /dev/null +++ b/src/classes/MDMeasurement.php @@ -0,0 +1,75 @@ + + */ +declare(strict_types = 1); + +/** + * Describes a measurement of an object. + */ +final class MDMeasurement implements JsonSerializable { + + public readonly int $object_id; + public readonly MDMeasurementType $type; + public float $value; + public MDLengthUnit|MDCountCopiesUnit|MDWeightUnit|MDCountPartsUnit|MDDieAxisUnit $unit; + public bool $exactness; + public string $note; + public string $updated_by = ""; + public int $update_timestamp = 0; + + /** + * Returns array for forming JSON representation. + * + * @return array{type: MDMeasurementType, value: float, unit: MDLengthUnit|MDCountCopiesUnit|MDWeightUnit|MDCountPartsUnit|MDDieAxisUnit, exactness: bool, note: string}|array{type: MDMeasurementType, value: float, unit: MDLengthUnit|MDCountCopiesUnit|MDWeightUnit|MDCountPartsUnit|MDDieAxisUnit, exactness: bool, note: string, updated_by: string, update_timestamp: int} + */ + public function jsonSerialize():array { + + $output = [ + 'type' => $this->type, + 'value' => $this->value, + 'unit' => $this->unit, + 'exactness' => $this->exactness, + 'note' => $this->note, + ]; + + if ($this->update_timestamp !== 0) { + $output['updated_by'] = $this->updated_by; + $output['update_timestamp'] = $this->update_timestamp; + } + + return $output; + + } + + /** + * Constructor. + * + * @param integer $object_id Object ID. + * @param MDMeasurementType $type Type. + * @param float $value Value. + * @param MDLengthUnit|MDCountCopiesUnit|MDWeightUnit|MDCountPartsUnit|MDDieAxisUnit $unit Unit. + * @param boolean $exactness Exactness. + * @param string $note Note. + * + * @return void + */ + public function __construct(int $object_id, MDMeasurementType $type, float $value, MDLengthUnit|MDCountCopiesUnit|MDWeightUnit|MDCountPartsUnit|MDDieAxisUnit $unit, bool $exactness, string $note) { + + $this->object_id = $object_id; + $this->type = $type; + + // Validate unit + if (get_class($unit) !== $this->type->getMeasurementUnit()) { + throw new Exception("Invalid unit selected"); + } + + $this->value = $value; + $this->unit = $unit; + $this->exactness = $exactness; + $this->note = $note; + + } +} diff --git a/src/enums/MDCountCopiesUnit.php b/src/enums/MDCountCopiesUnit.php new file mode 100644 index 0000000..fd83740 --- /dev/null +++ b/src/enums/MDCountCopiesUnit.php @@ -0,0 +1,148 @@ + + */ +declare(strict_types = 1); + +/** + * Represents a measurement type. + */ +enum MDCountCopiesUnit implements MDValueEnumInterface, JsonSerializable, MDMeasurementUnitInterface { + + case copies; + case specimen; + + /** + * Returns a value of this type based on a string. + * + * @param string $input Input to get a value from. + * + * @return MDCountCopiesUnit + */ + public static function fromString(string $input):MDCountCopiesUnit { + + return match($input) { + 'copies' => self::copies, + 'specimen' => self::specimen, + default => throw new MDpageParameterNotFromListException("Unknown copies count unit"), + }; + + } + + /** + * Returns a value of this type based on an integer. + * + * @param integer $input Input to get a value from. + * + * @return MDCountCopiesUnit + */ + public static function fromInt(int $input):MDCountCopiesUnit { + + return match($input) { + 1 => self::copies, + 2 => self::specimen, + default => throw new MDpageParameterNotFromListException("Unknown copies count unit"), + }; + + } + + /** + * + */ + + /** + * Lists all available names. + * + * @return array + */ + public static function caseNames():array { + + $output = []; + + $cases = self::cases(); + foreach ($cases as $case) { + $output[] = $case->name; + } + + return $output; + + } + + /** + * Returns an integer representation of the collective (for storage in DB). + * + * @return integer + */ + public function toInt():int { + + return match($this) { + self::copies => 1, + self::specimen => 2, + # default => throw new MDpageParameterNotFromListException("Unknown measurement type"), + }; + + } + + /** + * Returns the measurement calculated down to the base unit. + * + * @param float $value Measurement value. + * + * @return float + */ + public function convertToBaseUnit(float $value):float { + + return $value; + + } + + /** + * Gets an unsorted list of the entries in a translated version. + * + * @param MDTlLoader $tlLoader Translation loader. + * + * @return array + */ + public static function getUnsortedList(MDTlLoader $tlLoader):array { + return MDValueSet::getTlUnsortedList($tlLoader, self::caseNames(), "units_count_copies_set", "units_count_copies_set"); + + } + + /** + * Gets a sorted list of the entries in a translated version. + * + * @param MDTlLoader $tlLoader Translation loader. + * + * @return array + */ + public static function getSortedList(MDTlLoader $tlLoader):array { + return MDValueSet::getTlSortedList($tlLoader, self::caseNames(), "units_count_copies_set", "units_count_copies_set"); + + } + + /** + * Returns the name of the current value in translation. + * + * @param MDTlLoader $tlLoader Translation loader. + * + * @return string + */ + public function getTledName(MDTlLoader $tlLoader):string { + + return $tlLoader->tl("units_count_copies_set", "units_count_copies_set", $this->name); + + } + + /** + * Provides the option to serialize as a string during json_encode(). + * + * @return string + */ + public function jsonSerialize():string { + + return $this->name; + + } +} diff --git a/src/enums/MDCountPartsUnit.php b/src/enums/MDCountPartsUnit.php new file mode 100644 index 0000000..bed501b --- /dev/null +++ b/src/enums/MDCountPartsUnit.php @@ -0,0 +1,148 @@ + + */ +declare(strict_types = 1); + +/** + * Represents a measurement type. + */ +enum MDCountPartsUnit implements MDValueEnumInterface, JsonSerializable, MDMeasurementUnitInterface { + + case parts; + case pages; + + /** + * Returns a value of this type based on a string. + * + * @param string $input Input to get a value from. + * + * @return MDCountPartsUnit + */ + public static function fromString(string $input):MDCountPartsUnit { + + return match($input) { + 'parts' => self::parts, + 'pages' => self::pages, + default => throw new MDpageParameterNotFromListException("Unknown parts counting unit"), + }; + + } + + /** + * Returns a value of this type based on an integer. + * + * @param integer $input Input to get a value from. + * + * @return MDCountPartsUnit + */ + public static function fromInt(int $input):MDCountPartsUnit { + + return match($input) { + 1 => self::parts, + 2 => self::pages, + default => throw new MDpageParameterNotFromListException("Unknown parts counting unit"), + }; + + } + + /** + * + */ + + /** + * Lists all available names. + * + * @return array + */ + public static function caseNames():array { + + $output = []; + + $cases = self::cases(); + foreach ($cases as $case) { + $output[] = $case->name; + } + + return $output; + + } + + /** + * Returns an integer representation of the collective (for storage in DB). + * + * @return integer + */ + public function toInt():int { + + return match($this) { + self::parts => 1, + self::pages => 2, + # default => throw new MDpageParameterNotFromListException("Unknown measurement type"), + }; + + } + + /** + * Returns the measurement calculated down to the base unit. + * + * @param float $value Measurement value. + * + * @return float + */ + public function convertToBaseUnit(float $value):float { + + return $value; + + } + + /** + * Gets an unsorted list of the entries in a translated version. + * + * @param MDTlLoader $tlLoader Translation loader. + * + * @return array + */ + public static function getUnsortedList(MDTlLoader $tlLoader):array { + return MDValueSet::getTlUnsortedList($tlLoader, self::caseNames(), "units_count_parts_set", "units_count_parts_set"); + + } + + /** + * Gets a sorted list of the entries in a translated version. + * + * @param MDTlLoader $tlLoader Translation loader. + * + * @return array + */ + public static function getSortedList(MDTlLoader $tlLoader):array { + return MDValueSet::getTlSortedList($tlLoader, self::caseNames(), "units_count_parts_set", "units_count_parts_set"); + + } + + /** + * Returns the name of the current value in translation. + * + * @param MDTlLoader $tlLoader Translation loader. + * + * @return string + */ + public function getTledName(MDTlLoader $tlLoader):string { + + return $tlLoader->tl("units_count_parts_set", "units_count_parts_set", $this->name); + + } + + /** + * Provides the option to serialize as a string during json_encode(). + * + * @return string + */ + public function jsonSerialize():string { + + return $this->name; + + } +} diff --git a/src/enums/MDDieAxisUnit.php b/src/enums/MDDieAxisUnit.php new file mode 100644 index 0000000..6d9e1eb --- /dev/null +++ b/src/enums/MDDieAxisUnit.php @@ -0,0 +1,144 @@ + + */ +declare(strict_types = 1); + +/** + * Represents a measurement type. + */ +enum MDDieAxisUnit implements MDValueEnumInterface, JsonSerializable, MDMeasurementUnitInterface { + + case h; + + /** + * Returns a value of this type based on a string. + * + * @param string $input Input to get a value from. + * + * @return MDDieAxisUnit + */ + public static function fromString(string $input):MDDieAxisUnit { + + return match($input) { + 'h' => self::h, + default => throw new MDpageParameterNotFromListException("Unknown h counting unit"), + }; + + } + + /** + * Returns a value of this type based on an integer. + * + * @param integer $input Input to get a value from. + * + * @return MDDieAxisUnit + */ + public static function fromInt(int $input):MDDieAxisUnit { + + return match($input) { + 1 => self::h, + default => throw new MDpageParameterNotFromListException("Unknown h counting unit"), + }; + + } + + /** + * + */ + + /** + * Lists all available names. + * + * @return array + */ + public static function caseNames():array { + + $output = []; + + $cases = self::cases(); + foreach ($cases as $case) { + $output[] = $case->name; + } + + return $output; + + } + + /** + * Returns an integer representation of the collective (for storage in DB). + * + * @return integer + */ + public function toInt():int { + + return match($this) { + self::h => 1, + # default => throw new MDpageParameterNotFromListException("Unknown measurement type"), + }; + + } + + /** + * Returns the measurement calculated down to the base unit. + * + * @param float $value Measurement value. + * + * @return float + */ + public function convertToBaseUnit(float $value):float { + + return $value; + + } + + /** + * Gets an unsorted list of the entries in a translated version. + * + * @param MDTlLoader $tlLoader Translation loader. + * + * @return array + */ + public static function getUnsortedList(MDTlLoader $tlLoader):array { + return MDValueSet::getTlUnsortedList($tlLoader, self::caseNames(), "units_die_axis_set", "units_die_axis_set"); + + } + + /** + * Gets a sorted list of the entries in a translated version. + * + * @param MDTlLoader $tlLoader Translation loader. + * + * @return array + */ + public static function getSortedList(MDTlLoader $tlLoader):array { + return MDValueSet::getTlSortedList($tlLoader, self::caseNames(), "units_die_axis_set", "units_die_axis_set"); + + } + + /** + * Returns the name of the current value in translation. + * + * @param MDTlLoader $tlLoader Translation loader. + * + * @return string + */ + public function getTledName(MDTlLoader $tlLoader):string { + + return $tlLoader->tl("units_die_axis_set", "units_die_axis_set", $this->name); + + } + + /** + * Provides the option to serialize as a string during json_encode(). + * + * @return string + */ + public function jsonSerialize():string { + + return $this->name; + + } +} diff --git a/src/enums/MDLengthUnit.php b/src/enums/MDLengthUnit.php new file mode 100644 index 0000000..53aae3b --- /dev/null +++ b/src/enums/MDLengthUnit.php @@ -0,0 +1,173 @@ + + */ +declare(strict_types = 1); + +/** + * Represents a measurement type. + */ +enum MDLengthUnit implements MDValueEnumInterface, JsonSerializable, MDMeasurementUnitInterface { + + case m; + case dm; + case cm; + case mm; + case ft; + case in; + + /** + * Returns a value of this type based on a string. + * + * @param string $input Input to get a value from. + * + * @return MDLengthUnit + */ + public static function fromString(string $input):MDLengthUnit { + + return match($input) { + 'm' => self::m, + 'dm' => self::dm, + 'cm' => self::cm, + 'mm' => self::mm, + 'ft' => self::ft, + 'in' => self::in, + default => throw new MDpageParameterNotFromListException("Unknown length unit"), + }; + + } + + /** + * Returns a value of this type based on an integer. + * + * @param integer $input Input to get a value from. + * + * @return MDLengthUnit + */ + public static function fromInt(int $input):MDLengthUnit { + + return match($input) { + 1 => self::m, + 2 => self::dm, + 3 => self::cm, + 4 => self::mm, + 5 => self::ft, + 6 => self::in, + default => throw new MDpageParameterNotFromListException("Unknown length unit"), + }; + + } + + /** + * + */ + + /** + * Lists all available names. + * + * @return array + */ + public static function caseNames():array { + + $output = []; + + $cases = self::cases(); + foreach ($cases as $case) { + $output[] = $case->name; + } + + return $output; + + } + + /** + * Returns an integer representation of the collective (for storage in DB). + * + * @return integer + */ + public function toInt():int { + + return match($this) { + self::m => 1, + self::dm => 2, + self::cm => 3, + self::mm => 4, + self::ft => 5, + self::in => 6, + # default => throw new MDpageParameterNotFromListException("Unknown measurement type"), + }; + + } + + /** + * Returns the measurement calculated down to the base unit (e.g. mm for lengths). + * + * @param float $value Measurement value. + * + * @return float + */ + public function convertToBaseUnit(float $value):float { + + $multiplier = match($this) { + self::m => 1000, + self::ft => 304.8, + self::dm => 100, + self::in => 25.4, + self::cm => 10, + self::mm => 1, + }; + + return floatval($value * $multiplier); + + } + + /** + * Gets an unsorted list of the entries in a translated version. + * + * @param MDTlLoader $tlLoader Translation loader. + * + * @return array + */ + public static function getUnsortedList(MDTlLoader $tlLoader):array { + return MDValueSet::getTlUnsortedList($tlLoader, self::caseNames(), "units_length_set", "units_length_set"); + + } + + /** + * Gets a sorted list of the entries in a translated version. + * + * @param MDTlLoader $tlLoader Translation loader. + * + * @return array + */ + public static function getSortedList(MDTlLoader $tlLoader):array { + return MDValueSet::getTlSortedList($tlLoader, self::caseNames(), "units_length_set", "units_length_set"); + + } + + /** + * Returns the name of the current value in translation. + * + * @param MDTlLoader $tlLoader Translation loader. + * + * @return string + */ + public function getTledName(MDTlLoader $tlLoader):string { + + return $tlLoader->tl("units_length_set", "units_length_set", $this->name); + + } + + /** + * Provides the option to serialize as a string during json_encode(). + * + * @return string + */ + public function jsonSerialize():string { + + return $this->name; + + } +} diff --git a/src/enums/MDMeasurementType.php b/src/enums/MDMeasurementType.php index 8c34f8b..d723612 100644 --- a/src/enums/MDMeasurementType.php +++ b/src/enums/MDMeasurementType.php @@ -9,17 +9,25 @@ declare(strict_types = 1); /** * Represents a measurement type. */ -enum MDMeasurementType implements JsonSerializable { +enum MDMeasurementType implements MDValueEnumInterface, JsonSerializable { + + // Main types case length; case height; case width; - case number_of_pieces; case weight; case diameter; case wall; - case number_of_pages; case die_axis; + case number_of_copies; // copies, specimen + case number_of_parts; + + // Sub-types + + case length_socle; + case height_socle; + case width_socle; /** * Returns a value of this type based on a string. @@ -34,12 +42,15 @@ enum MDMeasurementType implements JsonSerializable { 'length' => self::length, 'height' => self::height, 'width' => self::width, - 'number_of_pieces' => self::number_of_pieces, + 'number_of_copies' => self::number_of_copies, 'weight' => self::weight, 'diameter' => self::diameter, 'wall' => self::wall, - 'number_of_pages' => self::number_of_pages, + 'number_of_parts' => self::number_of_parts, 'die_axis' => self::die_axis, + 'length_socle' => self::length_socle, + 'height_socle' => self::height_socle, + 'width_socle' => self::width_socle, default => throw new MDpageParameterNotFromListException("Unknown measurement type"), }; @@ -58,12 +69,15 @@ enum MDMeasurementType implements JsonSerializable { 1 => self::length, 2 => self::height, 3 => self::width, - 4 => self::number_of_pieces, + 4 => self::number_of_copies, 5 => self::weight, 6 => self::diameter, 7 => self::wall, - 8 => self::number_of_pages, + 8 => self::number_of_parts, 9 => self::die_axis, + 10 => self::length_socle, + 11 => self::height_socle, + 12 => self::width_socle, default => throw new MDpageParameterNotFromListException("Unknown measurement type"), }; @@ -92,15 +106,26 @@ enum MDMeasurementType implements JsonSerializable { } /** - * Returns the human-readable name of the measurement type. + * Returns the highest level measurement type relative to the provided one. * - * @return string + * @return MDMeasurementType */ - public function getName():string { + public function getHighestSuperordinate():MDMeasurementType { return match($this) { + self::length => self::length, + self::height => self::height, + self::width => self::width, + self::number_of_copies => self::number_of_copies, + self::weight => self::weight, + self::diameter => self::diameter, + self::wall => self::wall, + self::number_of_parts => self::number_of_parts, + self::die_axis => self::die_axis, + self::length_socle => self::length, + self::height_socle => self::height, + self::width_socle => self::width, # default => throw new MDpageParameterNotFromListException("Unknown measurement type"), - default => throw new MDpageParameterNotFromListException("To be implemented"), }; } @@ -116,17 +141,79 @@ enum MDMeasurementType implements JsonSerializable { self::length => 1, self::height => 2, self::width => 3, - self::number_of_pieces => 4, + self::number_of_copies => 4, self::weight => 5, self::diameter => 6, self::wall => 7, - self::number_of_pages => 8, + self::number_of_parts => 8, self::die_axis => 9, + self::length_socle => 10, + self::height_socle => 11, + self::width_socle => 12, # default => throw new MDpageParameterNotFromListException("Unknown measurement type"), }; } + /** + * Returns suitable measurement type. + * + * @return class-string + */ + public function getMeasurementUnit():string { + + return match($this->getHighestSuperordinate()) { + self::length, + self::height, + self::width, + self::wall, + self::diameter => MDLengthUnit::Class, + self::number_of_copies => MDCountCopiesUnit::Class, + self::weight => MDWeightUnit::Class, + self::number_of_parts => MDCountPartsUnit::Class, + self::die_axis => MDDieAxisUnit::Class, + # default => throw new MDpageParameterNotFromListException("Unknown measurement type"), + }; + + } + + /** + * Gets an unsorted list of the entries in a translated version. + * + * @param MDTlLoader $tlLoader Translation loader. + * + * @return array + */ + public static function getUnsortedList(MDTlLoader $tlLoader):array { + return MDValueSet::getTlUnsortedList($tlLoader, self::caseNames(), "measurement_type_set", "measurement_type_set"); + + } + + /** + * Gets a sorted list of the entries in a translated version. + * + * @param MDTlLoader $tlLoader Translation loader. + * + * @return array + */ + public static function getSortedList(MDTlLoader $tlLoader):array { + return MDValueSet::getTlSortedList($tlLoader, self::caseNames(), "measurement_type_set", "measurement_type_set"); + + } + + /** + * Returns the name of the current value in translation. + * + * @param MDTlLoader $tlLoader Translation loader. + * + * @return string + */ + public function getTledName(MDTlLoader $tlLoader):string { + + return $tlLoader->tl("measurement_type_set", "measurement_type_set", $this->name); + + } + /** * Provides the option to serialize as a string during json_encode(). * diff --git a/src/enums/MDWeightUnit.php b/src/enums/MDWeightUnit.php new file mode 100644 index 0000000..3fdf49e --- /dev/null +++ b/src/enums/MDWeightUnit.php @@ -0,0 +1,158 @@ + + */ +declare(strict_types = 1); + +/** + * Represents a measurement type. + */ +enum MDWeightUnit implements MDValueEnumInterface, JsonSerializable, MDMeasurementUnitInterface { + + case t; + case kg; + case g; + + /** + * Returns a value of this type based on a string. + * + * @param string $input Input to get a value from. + * + * @return MDWeightUnit + */ + public static function fromString(string $input):MDWeightUnit { + + return match($input) { + 't' => self::t, + 'kg' => self::kg, + 'g' => self::g, + default => throw new MDpageParameterNotFromListException("Unknown length unit"), + }; + + } + + /** + * Returns a value of this type based on an integer. + * + * @param integer $input Input to get a value from. + * + * @return MDWeightUnit + */ + public static function fromInt(int $input):MDWeightUnit { + + return match($input) { + 1 => self::t, + 2 => self::kg, + 3 => self::g, + default => throw new MDpageParameterNotFromListException("Unknown length unit"), + }; + + } + + /** + * + */ + + /** + * Lists all available names. + * + * @return array + */ + public static function caseNames():array { + + $output = []; + + $cases = self::cases(); + foreach ($cases as $case) { + $output[] = $case->name; + } + + return $output; + + } + + /** + * Returns an integer representation of the collective (for storage in DB). + * + * @return integer + */ + public function toInt():int { + + return match($this) { + self::t => 1, + self::kg => 2, + self::g => 3, + # default => throw new MDpageParameterNotFromListException("Unknown measurement type"), + }; + + } + + /** + * Returns the measurement calculated down to the base unit (e.g. mm for lengths). + * + * @param float $value Measurement value. + * + * @return float + */ + public function convertToBaseUnit(float $value):float { + + $multiplier = match($this) { + self::t => 1000000, + self::kg => 1000, + self::g => 1, + }; + + return floatval($value * $multiplier); + + } + + /** + * Gets an unsorted list of the entries in a translated version. + * + * @param MDTlLoader $tlLoader Translation loader. + * + * @return array + */ + public static function getUnsortedList(MDTlLoader $tlLoader):array { + return MDValueSet::getTlUnsortedList($tlLoader, self::caseNames(), "units_weight_set", "units_weight_set"); + + } + + /** + * Gets a sorted list of the entries in a translated version. + * + * @param MDTlLoader $tlLoader Translation loader. + * + * @return array + */ + public static function getSortedList(MDTlLoader $tlLoader):array { + return MDValueSet::getTlSortedList($tlLoader, self::caseNames(), "units_weight_set", "units_weight_set"); + + } + + /** + * Returns the name of the current value in translation. + * + * @param MDTlLoader $tlLoader Translation loader. + * + * @return string + */ + public function getTledName(MDTlLoader $tlLoader):string { + + return $tlLoader->tl("units_weight_set", "units_weight_set", $this->name); + + } + + /** + * Provides the option to serialize as a string during json_encode(). + * + * @return string + */ + public function jsonSerialize():string { + + return $this->name; + + } +}