diff --git a/src/Mapper/ArrayToActivationBeforeMapper.php b/src/Mapper/ArrayToActivationBeforeMapper.php new file mode 100644 index 0000000..0d89521 --- /dev/null +++ b/src/Mapper/ArrayToActivationBeforeMapper.php @@ -0,0 +1,23 @@ +setActivationBeforeDays(StringToIntMapper::mapStringToNullableInt($activationBeforeArray['ActivationBeforeDays'] ?? null)); + $activationBefore->setActivationBeforeDate(StringToDateMapper::mapStringToDate($activationBeforeArray['ActivationBeforeDate'] ?? null)); + + return $activationBefore; + } +} diff --git a/src/Mapper/ArrayToEckRecordMapper.php b/src/Mapper/ArrayToEckRecordMapper.php index 76b15ab..e742eff 100644 --- a/src/Mapper/ArrayToEckRecordMapper.php +++ b/src/Mapper/ArrayToEckRecordMapper.php @@ -12,6 +12,7 @@ public static function mapArrayToEckRecord(array $recordArray): EckRecord $eckRecord = new EckRecord($recordArray['RecordId'], $recordArray['Title']); $eckRecord->setLastModifiedDate(StringToDateMapper::mapStringToDate($recordArray['LastModifiedDate'] ?? null)); $eckRecord->setDescription($recordArray['Description'] ?? ''); + $eckRecord->setEnvironments(ArrayToEnvironmentsMapper::mapArrayToEnvironments($recordArray['Environments'] ?? null)); $eckRecord->setPublisher($recordArray['Publisher'] ?? null); $eckRecord->setAuthors(ArrayToStringArrayMapper::mapArrayToStringArray($recordArray['Authors'] ?? [])); $eckRecord->setInformationLocation($recordArray['InformationLocation'] ?? null); @@ -47,11 +48,13 @@ public static function mapArrayToEckRecord(array $recordArray): EckRecord $eckRecord->setPrices(ArrayToPricesMapper::mapArrayToPrices($recordArray['Prices'] ?? [])); $eckRecord->setPriceIsIndicative(StringToBoolMapper::mapStringToBool($recordArray['PriceIsIndicative'] ?? null)); $eckRecord->setIsLicensed(StringToBoolMapper::mapStringToBool($recordArray['IsLicensed'] ?? null)); + $eckRecord->setActivationBefore(ArrayToActivationBeforeMapper::mapArrayToActivationBefore($recordArray['ActivationBefore'] ?? null)); $eckRecord->setLicenseAvailabilityOptions($recordArray['LicenseAvailabilityOptions'] ?? null); $eckRecord->setLicenseStartDate(StringToDateMapper::mapStringToDate($recordArray['LicenseStartDate'] ?? null)); $eckRecord->setLicenseEndDate(StringToDateMapper::mapStringToDate($recordArray['LicenseEndDate'] ?? null)); $eckRecord->setLicenseDuration($recordArray['LicenseDuration'] ?? null); - $eckRecord->setLicenseCount(StringToIntMapper::mapStringToInt($recordArray['LicenseCount'] ?? null)); + $eckRecord->setLicenseCount(StringToIntMapper::mapStringToNullableInt($recordArray['LicenseCount'] ?? null)); + $eckRecord->setAdditionalLicenseOptions(ArrayToStringArrayMapper::mapArrayToStringArray($recordArray['AdditionalLicenseOptions'] ?? [])); $eckRecord->setIsCatalogItem(StringToBoolMapper::mapStringToBool($recordArray['IsCatalogItem'] ?? null)); $eckRecord->setCopyright($recordArray['Copyright'] ?? null); return $eckRecord; diff --git a/src/Mapper/ArrayToEnvironmentsMapper.php b/src/Mapper/ArrayToEnvironmentsMapper.php new file mode 100644 index 0000000..16fd17b --- /dev/null +++ b/src/Mapper/ArrayToEnvironmentsMapper.php @@ -0,0 +1,22 @@ +setPlatform(ArrayToStringArrayMapper::mapValueToStringArray($environmentsArray['Platform'] ?? null)); + $environments->setDevice(ArrayToStringArrayMapper::mapValueToStringArray($environmentsArray['Device'] ?? null)); + $environments->setBrowser(ArrayToStringArrayMapper::mapValueToStringArray($environmentsArray['Browser'] ?? null)); + return $environments; + } +} diff --git a/src/Mapper/ArrayToPriceMapper.php b/src/Mapper/ArrayToPriceMapper.php index d190fad..f93a177 100644 --- a/src/Mapper/ArrayToPriceMapper.php +++ b/src/Mapper/ArrayToPriceMapper.php @@ -12,13 +12,21 @@ final class ArrayToPriceMapper */ public static function mapArrayToPrices(array $pricesArray): array { - $keys = array_keys($pricesArray); - if (in_array(array_shift($keys), ['Amount', 'VAT'], true)) { + if (self::isSinglePriceArray($pricesArray)) { return [ArrayToPriceMapper::mapArrayToSinglePrice($pricesArray)]; } - return array_map(function (array $singlePriceArray) { - return ArrayToPriceMapper::mapArrayToSinglePrice($singlePriceArray); - }, $pricesArray); + + $combinedAmountAndPricesArrays = self::createPricesArraysFromAmountAndVatArrays( + array_values($pricesArray['Amount'] ?? []), + array_values($pricesArray['VAT'] ?? []) + ); + + return array_map( + function (array $priceArray) { + return self::mapArrayToSinglePrice($priceArray); + }, $combinedAmountAndPricesArrays + ); + } private static function mapArrayToSinglePrice(array $priceArray): Price @@ -29,4 +37,30 @@ private static function mapArrayToSinglePrice(array $priceArray): Price return $price; } + private static function createPricesArraysFromAmountAndVatArrays(array $amountArray, array $vatArray): array + { + $combinedAmountAndPricesArrays = []; + for ($i = 0; $i < sizeof($amountArray); $i++) { + $combinedAmountAndPricesArrays[] = [ + 'Amount' => $amountArray[$i], + 'VAT' => $vatArray[$i], + ]; + } + return $combinedAmountAndPricesArrays; + } + + private static function isSinglePriceArray(array $pricesArray): bool + { + /* + * The serializer returns some strange results. It casts a 0 to a null for example + * which makes it hard to differentiate between a set value and a not-set + * value. We only look at the VAT therefore, because that contains a not-0 value + * for the VAT, which does not cast to null in the serializer.. + */ + return sizeof($pricesArray) === 2 + && isset($pricesArray['VAT']) + && !is_array($pricesArray['VAT']); + + } + } diff --git a/src/Mapper/ArrayToStringArrayMapper.php b/src/Mapper/ArrayToStringArrayMapper.php index bbdf88a..6708242 100644 --- a/src/Mapper/ArrayToStringArrayMapper.php +++ b/src/Mapper/ArrayToStringArrayMapper.php @@ -12,9 +12,18 @@ public static function mapArrayToStringArray(array $arrayWithValues): array { $valueArray = array_values($arrayWithValues); $propertyValue = array_shift($valueArray); - if (is_array($propertyValue)){ - return $propertyValue; + return self::mapValueToStringArray($propertyValue); + } + + /** + * @param string[]|string|null $value + * @return string[] + */ + public static function mapValueToStringArray($value): array + { + if (is_array($value)) { + return $value; } - return null !== $propertyValue ? [$propertyValue] : []; + return null !== $value ? [$value] : []; } } diff --git a/src/Model/ActivationBefore.php b/src/Model/ActivationBefore.php new file mode 100644 index 0000000..fea323b --- /dev/null +++ b/src/Model/ActivationBefore.php @@ -0,0 +1,47 @@ +activationBeforeDays; + } + + /** + * @param int|null $activationBeforeDays + */ + public function setActivationBeforeDays(?int $activationBeforeDays): void + { + $this->activationBeforeDays = $activationBeforeDays; + } + + /** + * @return DateTimeImmutable|null + */ + public function getActivationBeforeDate(): ?DateTimeImmutable + { + return $this->activationBeforeDate; + } + + /** + * @param DateTimeImmutable|null $activationBeforeDate + */ + public function setActivationBeforeDate(?DateTimeImmutable $activationBeforeDate): void + { + $this->activationBeforeDate = $activationBeforeDate; + } +} diff --git a/src/Model/Entry.php b/src/Model/Entry.php index 53aed21..b08bac4 100644 --- a/src/Model/Entry.php +++ b/src/Model/Entry.php @@ -39,10 +39,8 @@ class Entry /** @var string */ private $description = ''; - /** - * TODO: Add Environments - * Environments - */ + /** @var Environments|null */ + private $environments = null; /** @var string|null */ private $contentLocation = null; @@ -141,10 +139,8 @@ class Entry /** @var bool */ private $isLicensed = false; - /** - * @Todo: Add ActivationBefore complex object - * ActivationBefore - **/ + /** @var ActivationBefore|null */ + private $activationBefore = null; /** @var string|null */ private $licenseAvailabilityOptions = null; @@ -158,13 +154,11 @@ class Entry /** @var string|null */ private $licenseDuration = null; - /** @var int */ - private $licenseCount = 0; + /** @var int|null */ + private $licenseCount = null; - /** - * @Todo: Add AdditionalLicenseOptions complex object - * AdditionalLicenseOptions - **/ + /** @var string[] */ + private $additionalLicenseOptions = []; /** @var bool */ private $isCatalogItem = false; @@ -212,6 +206,16 @@ public function getDescription(): string return $this->description; } + public function getEnvironments(): ?Environments + { + return $this->environments; + } + + public function setEnvironments(?Environments $environments): void + { + $this->environments = $environments; + } + public function getPublisher(): string { return $this->publisher; @@ -575,6 +579,16 @@ public function setIsLicensed(bool $isLicensed): void $this->isLicensed = $isLicensed; } + public function getActivationBefore(): ?ActivationBefore + { + return $this->activationBefore; + } + + public function setActivationBefore(?ActivationBefore $activationBefore): void + { + $this->activationBefore = $activationBefore; + } + public function getLicenseAvailabilityOptions(): ?string { return $this->licenseAvailabilityOptions; @@ -615,16 +629,32 @@ public function setLicenseDuration(?string $licenseDuration): void $this->licenseDuration = $licenseDuration; } - public function getLicenseCount(): int + public function getLicenseCount(): ?int { return $this->licenseCount; } - public function setLicenseCount(int $licenseCount): void + public function setLicenseCount(?int $licenseCount): void { $this->licenseCount = $licenseCount; } + /** + * @return string[] + */ + public function getAdditionalLicenseOptions(): array + { + return $this->additionalLicenseOptions; + } + + /** + * @param string[] $additionalLicenseOptions + */ + public function setAdditionalLicenseOptions(array $additionalLicenseOptions): void + { + $this->additionalLicenseOptions = $additionalLicenseOptions; + } + public function isCatalogItem(): bool { return $this->isCatalogItem; diff --git a/src/Model/Environments.php b/src/Model/Environments.php new file mode 100644 index 0000000..fc2d89f --- /dev/null +++ b/src/Model/Environments.php @@ -0,0 +1,64 @@ +platform; + } + + /** + * @param string[] $platform + */ + public function setPlatform(array $platform): void + { + $this->platform = $platform; + } + + /** + * @return string[] + */ + public function getDevice(): array + { + return $this->device; + } + + /** + * @param string[] $device + */ + public function setDevice(array $device): void + { + $this->device = $device; + } + + /** + * @return string[] + */ + public function getBrowser(): array + { + return $this->browser; + } + + /** + * @param string[] $browser + */ + public function setBrowser(array $browser): void + { + $this->browser = $browser; + } +} diff --git a/tests/Mapper/ArrayToPricesMapperTest.php b/tests/Mapper/ArrayToPricesMapperTest.php index e38a8d7..bb7e6ab 100644 --- a/tests/Mapper/ArrayToPricesMapperTest.php +++ b/tests/Mapper/ArrayToPricesMapperTest.php @@ -25,14 +25,14 @@ public function pricesArrayDataProvider(): \Generator [ 'Currency' => 'EUR', 'Price' => [ - [ - 'Amount' => '913', - 'VAT' => '9', + 'Amount' => [ + '913', + '1002', + ], + 'VAT' => [ + '9', + '21', ], - [ - 'Amount' => '1002', - 'VAT' => '21', - ] ] ], (new PricesBuilder())