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/ArrayToAdditionalLicenseOptionsMapper.php b/src/Mapper/ArrayToAdditionalLicenseOptionsMapper.php new file mode 100644 index 0000000..d28138f --- /dev/null +++ b/src/Mapper/ArrayToAdditionalLicenseOptionsMapper.php @@ -0,0 +1,21 @@ +setAdditionalLicenseOption(ArrayToStringArrayMapper::mapArrayToStringArray($additionalLicenseOptionsArray ?? [])); + return $additionalLicenseOptions; + + } +} diff --git a/src/Mapper/ArrayToEckRecordMapper.php b/src/Mapper/ArrayToEckRecordMapper.php index 76b15ab..698c90f 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->setAdditionalLicenseOptions(ArrayToAdditionalLicenseOptionsMapper::mapArrayToAdditionalLicenseOptions($recordArray['AdditionalLicenseOptions'] ?? null)); $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/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/AdditionalLicenseOptions.php b/src/Model/AdditionalLicenseOptions.php new file mode 100644 index 0000000..c34ebb5 --- /dev/null +++ b/src/Model/AdditionalLicenseOptions.php @@ -0,0 +1,26 @@ +additionalLicenseOption; + } + + /** + * @param string[] $additionalLicenseOption + */ + public function setAdditionalLicenseOption(array $additionalLicenseOption): void + { + $this->additionalLicenseOption = $additionalLicenseOption; + } +} diff --git a/src/Model/Entry.php b/src/Model/Entry.php index 53aed21..52d893c 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; @@ -161,10 +157,8 @@ class Entry /** @var int */ private $licenseCount = 0; - /** - * @Todo: Add AdditionalLicenseOptions complex object - * AdditionalLicenseOptions - **/ + /** @var AdditionalLicenseOptions */ + private $additionalLicenseOptions = null; /** @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; @@ -625,6 +639,16 @@ public function setLicenseCount(int $licenseCount): void $this->licenseCount = $licenseCount; } + public function getAdditionalLicenseOptions(): ?AdditionalLicenseOptions + { + return $this->additionalLicenseOptions; + } + + public function setAdditionalLicenseOptions(?AdditionalLicenseOptions $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/ArrayToActivationBeforeMapperTest.php b/tests/Mapper/ArrayToActivationBeforeMapperTest.php new file mode 100644 index 0000000..b2c5ecb --- /dev/null +++ b/tests/Mapper/ArrayToActivationBeforeMapperTest.php @@ -0,0 +1,55 @@ + '30' + ], + (new ActivationBeforeBuilder()) + ->withActivationBeforeDays(30) + ->build() + , + 'Creates an activation before object with ActivationBeforeDays set to 30 and date not there.' + ]; + + yield [ + [ + 'ActivationBeforeDate' => '2021-07-16T23:59:59+01:00' + ], + (new ActivationBeforeBuilder()) + ->withActivationBeforeDate(new DateTimeImmutable('2021-07-16T23:59:59+01:00')) + ->build() + , + 'Creates an activation before object with ActivationBeforeDate set to july 16th 2021 and no activation before days.' + ]; + + yield [ + null, + null, + 'No activation before data returns null' + ]; + } +} diff --git a/tests/Mapper/ArrayToAdditionalLicenseOptionsMapperTest.php b/tests/Mapper/ArrayToAdditionalLicenseOptionsMapperTest.php new file mode 100644 index 0000000..863b320 --- /dev/null +++ b/tests/Mapper/ArrayToAdditionalLicenseOptionsMapperTest.php @@ -0,0 +1,52 @@ + ['Inkijkexemplaar', 'Demo-exemplaar'] + ], + (new AdditionalLicenseOptionsBuilder()) + ->withAdditionalLicenseOption(['Inkijkexemplaar', 'Demo-exemplaar']) + ->build() + , + 'Creates an additional license options object with multiple additional license options' + ]; + + yield [ + [ + 'AdditionalLicenseOption' => 'Inkijkexemplaar' + ], + (new AdditionalLicenseOptionsBuilder()) + ->withAdditionalLicenseOption(['Inkijkexemplaar']) + ->build() + , + 'Creates an additional license options object with a single additional license option' + ]; + + yield [ + null, + null, + 'No additional license options should return null' + ]; + } +} diff --git a/tests/Mapper/ArrayToEckRecordMapperTest.php b/tests/Mapper/ArrayToEckRecordMapperTest.php index 94da57c..10ff41d 100644 --- a/tests/Mapper/ArrayToEckRecordMapperTest.php +++ b/tests/Mapper/ArrayToEckRecordMapperTest.php @@ -6,8 +6,12 @@ use DateTimeImmutable; use Kennisnet\ECK\Mapper\ArrayToEckRecordMapper; use Kennisnet\ECK\EckRecord; +use Kennisnet\ECK\Model\AdditionalLicenseOptions; use Kennisnet\ECK\ResponseSerializer; +use Kennisnet\ECK\Tests\builder\ActivationBeforeBuilder; +use Kennisnet\ECK\Tests\builder\AdditionalLicenseOptionsBuilder; use Kennisnet\ECK\Tests\builder\EckRecordBuilder; +use Kennisnet\ECK\Tests\builder\EnvironmentsBuilder; use Kennisnet\ECK\Tests\builder\PriceBuilder; use Kennisnet\ECK\Tests\builder\PricesBuilder; use PHPUnit\Framework\TestCase; @@ -35,6 +39,10 @@ private function expectedResult(): EckRecord ->withProductThumbnailLocation('https://www.kinheim.com/wp-content/uploads/2012/05/712-292-Mijn-Boek-deel-2.png') ->withAuthors(['kinheim']) ->withDescription('Het aanbod van puzzels in Mijn Boek deel 3 omvat: woordzoekers, speurpuzzels, woordpuzzels, legletters, zoek de verschillen, anagrammen en magische vierkanten.') + ->withEnvironments(((new EnvironmentsBuilder()) + ->withPlatform(['iOS', 'Windows']) + ->withDevice(['pc ready'])) + ->build()) ->withContentLocation('https://www.kinheim.com/leermiddelen/mijn-boek-3/?attribute_pa_aantal=los-exemplaar') ->withIntendedEndUserRole('Onderwijsvolger') ->withProductState('Leverbaar') @@ -47,11 +55,23 @@ private function expectedResult(): EckRecord ->withLevels(['PO']) ->withYears(['jaar 3']) ->withSaleUnitSize(1) - ->withPrices((new PricesBuilder())->withCurrency('EUR')->withPrice([ - (new PriceBuilder())->withVat(9)->withAmount(913)->build() - ])->build()) + ->withPrices((new PricesBuilder()) + ->withCurrency('EUR') + ->withPrice([(new PriceBuilder()) + ->withVat(9) + ->withAmount(913) + ->build() + ]) + ->build()) ->withIsLicensed(false) + ->withActivationBefore((new ActivationBeforeBuilder()) + ->withActivationBeforeDays(30) + ->withActivationBeforeDate(new DateTimeImmutable('2021-07-16T23:59:59+01:00')) + ->build()) ->withIsCatalogItem(true) + ->withAdditionalLicenseOptions((new AdditionalLicenseOptionsBuilder()) + ->withAdditionalLicenseOption(['Inkijkexemplaar', 'Demo-exemplaar']) + ->build()) ->withCopyright('yes') ->withLastModifiedDate(new DateTimeImmutable('2020-01-02T21:14:42+01:00')) ->build(); diff --git a/tests/Mapper/ArrayToEnvironmentsMapperTest.php b/tests/Mapper/ArrayToEnvironmentsMapperTest.php new file mode 100644 index 0000000..1be466c --- /dev/null +++ b/tests/Mapper/ArrayToEnvironmentsMapperTest.php @@ -0,0 +1,64 @@ + ['iOS', 'Android'], + 'Device' => ['mobile ready', 'tablet'], + 'Browser' => ['Chrome', 'Safari'], + ], + (new EnvironmentsBuilder()) + ->withPlatform(['iOS', 'Android']) + ->withDevice(['mobile ready', 'tablet']) + ->withBrowser(['Chrome', 'Safari']) + ->build() + , + 'Creates an environments object with multiple platforms, devices and browsers.' + ]; + + yield [ + [], + (new EnvironmentsBuilder()) + ->build() + , + 'Creates an environments object without any platforms, devices or browsers' + ]; + + yield [ + [ + 'Platform' => 'iOS' + ], + (new EnvironmentsBuilder()) + ->withPlatform(['iOS']) + ->build() + , + 'Creates an environments object with only a single platform and no devices or browsers' + ]; + + yield [ + null, + null, + 'No environments data returns null.' + ]; + } +} diff --git a/tests/builder/ActivationBeforeBuilder.php b/tests/builder/ActivationBeforeBuilder.php new file mode 100644 index 0000000..fe3572f --- /dev/null +++ b/tests/builder/ActivationBeforeBuilder.php @@ -0,0 +1,36 @@ +activationBefore = $activationBefore ?? new ActivationBefore(); + } + + public function build(): ActivationBefore + { + return $this->activationBefore; + } + + public function withActivationBeforeDays(int $activationBeforeDays): self + { + $builder = clone $this; + $builder->activationBefore->setActivationBeforeDays($activationBeforeDays); + return $builder; + } + + public function withActivationBeforeDate(\DateTimeImmutable $activationBeforeDate): self + { + $builder = clone $this; + $builder->activationBefore->setActivationBeforeDate($activationBeforeDate); + return $builder; + } +} diff --git a/tests/builder/AdditionalLicenseOptionsBuilder.php b/tests/builder/AdditionalLicenseOptionsBuilder.php new file mode 100644 index 0000000..04d5491 --- /dev/null +++ b/tests/builder/AdditionalLicenseOptionsBuilder.php @@ -0,0 +1,29 @@ +additionalLicenseOptions = $additionalLicenseOptions ?? new AdditionalLicenseOptions(); + } + + public function build(): AdditionalLicenseOptions + { + return $this->additionalLicenseOptions; + } + + public function withAdditionalLicenseOption(array $additionalLicenseOption): self + { + $builder = clone $this; + $builder->additionalLicenseOptions->setAdditionalLicenseOption($additionalLicenseOption); + return $builder; + } +} diff --git a/tests/builder/EckRecordBuilder.php b/tests/builder/EckRecordBuilder.php index 0667e5b..36e2b51 100644 --- a/tests/builder/EckRecordBuilder.php +++ b/tests/builder/EckRecordBuilder.php @@ -5,6 +5,9 @@ use DateTimeImmutable; use Kennisnet\ECK\EckRecord; +use Kennisnet\ECK\Model\ActivationBefore; +use Kennisnet\ECK\Model\AdditionalLicenseOptions; +use Kennisnet\ECK\Model\Environments; use Kennisnet\ECK\Model\Prices; final class EckRecordBuilder @@ -43,6 +46,13 @@ public function withDescription(string $description): self return $builder; } + public function withEnvironments(?Environments $environments): self + { + $builder = clone $this; + $builder->record->setEnvironments($environments); + return $builder; + } + public function withInformationLocation(string $informationLocation): self { $builder = clone $this; @@ -284,6 +294,13 @@ public function withIsLicensed(bool $isLicensed): self return $builder; } + public function withActivationBefore(ActivationBefore $activationBefore): self + { + $builder = clone $this; + $builder->record->setActivationBefore($activationBefore); + return $builder; + } + public function withLicenseAvailabilityOptions(array $licenseAvailabilityOptions): self { $builder = clone $this; @@ -319,6 +336,13 @@ public function withLicenseCount(int $licenseCount): self return $builder; } + public function withAdditionalLicenseOptions(AdditionalLicenseOptions $additionalLicenseOptions): self + { + $builder = clone $this; + $builder->record->setAdditionalLicenseOptions($additionalLicenseOptions); + return $builder; + } + public function withIsCatalogItem(bool $isCatalogItem): self { $builder = clone $this; diff --git a/tests/builder/EnvironmentsBuilder.php b/tests/builder/EnvironmentsBuilder.php new file mode 100644 index 0000000..a5e9f86 --- /dev/null +++ b/tests/builder/EnvironmentsBuilder.php @@ -0,0 +1,43 @@ +environments = $environments ?? new Environments(); + } + + public function build(): Environments + { + return $this->environments; + } + + public function withPlatform(array $platform): self + { + $builder = clone $this; + $builder->environments->setPlatform($platform); + return $builder; + } + + public function withDevice(array $device): self + { + $builder = clone $this; + $builder->environments->setDevice($device); + return $builder; + } + + public function withBrowser(array $browser): self + { + $builder = clone $this; + $builder->environments->setBrowser($browser); + return $builder; + } +} diff --git a/tests/data/eckResponse.xml b/tests/data/eckResponse.xml index d4738f7..662fc72 100644 --- a/tests/data/eckResponse.xml +++ b/tests/data/eckResponse.xml @@ -27,6 +27,11 @@ Het aanbod van puzzels in Mijn Boek deel 3 omvat: woordzoekers, speurpuzzels, woordpuzzels, legletters, zoek de verschillen, anagrammen en magische vierkanten. + + iOS + Windows + pc ready + https://www.kinheim.com/leermiddelen/mijn-boek-3/?attribute_pa_aantal=los-exemplaar @@ -62,7 +67,15 @@ false false + + 30 + 2021-07-16T23:59:59+01:00 + true + + Inkijkexemplaar + Demo-exemplaar + yes 2020-01-02T21:14:42+01:00