Extend MDMeasurement with surrounding enums to cover measurements of

objects
This commit is contained in:
2024-11-21 21:46:24 +01:00
parent fa8bc6dac0
commit 7fc57353a8
9 changed files with 968 additions and 14 deletions

View File

@ -0,0 +1,75 @@
<?PHP
/**
* Describes a measurement of an object.
*
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
*/
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;
}
}