generated from spawnia/php-package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Implement Lightcycler XML parsing with validation for unique sa… #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3805ede
feat: Implement Lightcycler XML parsing with validation for unique sa…
c7c913d
feat: Implement Lightcycler XML parsing with validation for unique sa…
755f1b1
Apply suggestions from code review
simbig 49e9c88
no final
26d3e3a
inline attributes
43e9a98
refactor: Improve code readability by adjusting variable assignment f…
829fd9b
allow ternary.shortNotAllowed via neon file
575326c
revert short ternary
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/LightcyclerExportSheet/DuplicateCoordinatesException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace MLL\Utils\LightcyclerExportSheet; | ||
|
||
class DuplicateCoordinatesException extends \InvalidArgumentException | ||
{ | ||
/** @param array<string> $duplicateCoordinates */ | ||
public static function forCoordinates(array $duplicateCoordinates): self | ||
{ | ||
$coordinates = implode(', ', $duplicateCoordinates); | ||
|
||
return new self("Duplicate sample coordinates found: {$coordinates}."); | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
src/LightcyclerExportSheet/LightcyclerDataParsingTrait.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace MLL\Utils\LightcyclerExportSheet; | ||
|
||
use Illuminate\Support\Collection; | ||
|
||
trait LightcyclerDataParsingTrait | ||
{ | ||
protected function parseFloatValue(?string $value): ?float | ||
{ | ||
$cleanString = $this->cleanString($value); | ||
|
||
if ($cleanString === null) { | ||
return null; | ||
} | ||
|
||
if (! is_numeric($cleanString)) { | ||
throw new \InvalidArgumentException("Invalid float value: '{$cleanString}'"); | ||
} | ||
|
||
return (float) $cleanString; | ||
} | ||
|
||
/** @return array{float, float} */ | ||
protected function validateConcentrationAndCrossingPoint(?string $concentration, ?string $crossingPoint): array | ||
{ | ||
$parsedConcentration = $this->parseFloatValue($concentration); | ||
$parsedCrossingPoint = $this->parseFloatValue($crossingPoint); | ||
|
||
if (($parsedConcentration === null) !== ($parsedCrossingPoint === null)) { | ||
throw new \InvalidArgumentException('Concentration and crossing point must both be present or both be absent'); | ||
} | ||
|
||
return [ | ||
$parsedConcentration ?? LightcyclerXmlParser::FLOAT_ZERO, | ||
$parsedCrossingPoint ?? LightcyclerXmlParser::FLOAT_ZERO, | ||
simbig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
]; | ||
} | ||
|
||
protected function cleanString(?string $maybeString): ?string | ||
{ | ||
if ($maybeString === null) { | ||
return null; | ||
} | ||
$trimmed = trim($maybeString); | ||
|
||
return $trimmed !== '' | ||
? $trimmed | ||
: null; | ||
} | ||
|
||
/** @param array<string, string> $properties */ | ||
protected function requiredProperty(array $properties, string $propertyName): string | ||
{ | ||
$cleaned = $this->cleanString($properties[$propertyName] ?? null); | ||
if ($cleaned === null) { | ||
throw MissingRequiredPropertyException::forProperty($propertyName); | ||
} | ||
|
||
return $cleaned; | ||
} | ||
|
||
/** @param array<string, string> $properties */ | ||
protected function optionalProperty(array $properties, string $propertyName): ?string | ||
{ | ||
return $this->cleanString($properties[$propertyName] ?? null); | ||
} | ||
|
||
/** | ||
* @param Collection<array-key, LightcyclerSample> $samples | ||
* | ||
* @return Collection<array-key, LightcyclerSample> | ||
*/ | ||
protected function validateUniqueCoordinates(Collection $samples): Collection | ||
{ | ||
$coordinateCount = []; | ||
|
||
foreach ($samples as $sample) { | ||
$coordinateString = $sample->coordinates->toString(); | ||
$coordinateCount[$coordinateString] = ($coordinateCount[$coordinateString] ?? 0) + 1; | ||
} | ||
|
||
$duplicates = array_keys(array_filter($coordinateCount, fn (int $count): bool => $count > 1)); | ||
|
||
if ($duplicates !== []) { | ||
throw DuplicateCoordinatesException::forCoordinates($duplicates); | ||
} | ||
|
||
return $samples; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace MLL\Utils\LightcyclerExportSheet; | ||
|
||
use MLL\Utils\Microplate\Coordinates; | ||
use MLL\Utils\Microplate\CoordinateSystem12x8; | ||
|
||
class LightcyclerSample | ||
{ | ||
public string $name; | ||
|
||
/** @var Coordinates<CoordinateSystem12x8> */ | ||
public Coordinates $coordinates; | ||
|
||
public float $calculatedConcentration; | ||
|
||
public float $crossingPoint; | ||
|
||
public ?float $standardConcentration; | ||
|
||
/** @param Coordinates<CoordinateSystem12x8> $coordinates */ | ||
public function __construct( | ||
string $name, | ||
Coordinates $coordinates, | ||
float $calculatedConcentration, | ||
float $crossingPoint, | ||
?float $standardConcentration = null | ||
) { | ||
$this->standardConcentration = $standardConcentration; | ||
$this->crossingPoint = $crossingPoint; | ||
$this->calculatedConcentration = $calculatedConcentration; | ||
$this->coordinates = $coordinates; | ||
$this->name = $name; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace MLL\Utils\LightcyclerExportSheet; | ||
|
||
use Illuminate\Support\Collection; | ||
use MLL\Utils\Microplate\Coordinates; | ||
use MLL\Utils\Microplate\CoordinateSystem12x8; | ||
|
||
use function Safe\simplexml_load_string; | ||
|
||
class LightcyclerXmlParser | ||
{ | ||
use LightcyclerDataParsingTrait; | ||
|
||
public const FLOAT_ZERO = 0.0; | ||
simbig marked this conversation as resolved.
Show resolved
Hide resolved
simbig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** @return Collection<array-key, LightcyclerSample> */ | ||
public function parse(string $xmlContent): Collection | ||
{ | ||
$xml = simplexml_load_string($xmlContent); | ||
|
||
$analyses = $xml->analyses; | ||
if ($analyses === null || $analyses->analysis === null) { | ||
return new Collection(); | ||
} | ||
|
||
return $this->extractAnalysisSamples($analyses); | ||
} | ||
|
||
/** @return Collection<array-key, LightcyclerSample> */ | ||
private function extractAnalysisSamples(\SimpleXMLElement $analyses): Collection | ||
{ | ||
$samples = []; | ||
|
||
foreach ($analyses->analysis as $analysis) { | ||
if (property_exists($analysis, 'AnalysisSamples') | ||
&& $analysis->AnalysisSamples !== null | ||
) { | ||
foreach ($analysis->AnalysisSamples->AnalysisSample as $xmlSample) { | ||
$samples[] = $this->createSampleFromXml($xmlSample); | ||
} | ||
} | ||
} | ||
|
||
return $this->validateUniqueCoordinates(new Collection($samples)); | ||
} | ||
|
||
private function createSampleFromXml(\SimpleXMLElement $xmlSample): LightcyclerSample | ||
{ | ||
$sampleProperties = $this->extractPropertiesFromXml($xmlSample); | ||
|
||
[$validatedConcentration, $validatedCrossingPoint] = $this->validateConcentrationAndCrossingPoint( | ||
$this->optionalProperty($sampleProperties, 'CalcConc'), | ||
$this->optionalProperty($sampleProperties, 'CrossingPoint'), | ||
); | ||
|
||
$coordinates = Coordinates::fromString( | ||
$this->requiredProperty($sampleProperties, 'Position'), | ||
new CoordinateSystem12x8(), | ||
); | ||
|
||
return new LightcyclerSample( | ||
$this->requiredProperty($sampleProperties, 'name'), | ||
$coordinates, | ||
$validatedConcentration, | ||
$validatedCrossingPoint, | ||
$this->parseFloatValue($this->optionalProperty( | ||
$sampleProperties, | ||
'StandardConc', | ||
)), | ||
); | ||
} | ||
|
||
/** @return array<string, string> */ | ||
private function extractPropertiesFromXml(\SimpleXMLElement $xmlElement): array | ||
{ | ||
$properties = []; | ||
|
||
foreach ($xmlElement->prop as $propertyNode) { | ||
$propertyName = (string) $propertyNode->attributes()->name; | ||
$propertyValue = $propertyNode->__toString(); | ||
|
||
if (! isset($properties[$propertyName])) { | ||
$properties[$propertyName] = $propertyValue; | ||
} | ||
} | ||
|
||
return $properties; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/LightcyclerExportSheet/MissingRequiredPropertyException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace MLL\Utils\LightcyclerExportSheet; | ||
|
||
class MissingRequiredPropertyException extends \InvalidArgumentException | ||
{ | ||
public static function forProperty(string $propertyName): self | ||
{ | ||
return new self("Required property '{$propertyName}' is missing or empty."); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.