From 6f050b1fa6688a4d67fbed06017ee76ec601d43e Mon Sep 17 00:00:00 2001 From: Mike Kaperys Date: Fri, 3 Nov 2017 14:02:49 +0000 Subject: [PATCH] v0.2 RC (#9) * Saving progress * Saving progress * Update ignore * Saving * Update composer and ignore * Update docblock * Removed .idea * Saving * Saving packer progress * Added docker container * More core work * Abstracted field validation * Packer and validation progress * Unpacker progress * Packer progress * Packer progress * CI integration and validation progress * Update CI * Added CI badge * Packer progress * Test Progress * Update readme * CI integration * Fix namespace * Coveralls integration * CI fix * CI fix * Coveralls fix * Coveralls fix * Coveralls fix * Fix phpunit coverage * Fix build logs and add restart to container * Added license * Added MessageTypeIndicator class * Removed unneeded @lengthindicator from schema and refactored message unpacking * Coverage progress * Fixed variable length padding * Coverage, cleanup and docs * Update docs * Update docs * Remove cache from git * Made cache write if not exist * Update docs * Update docs * Support for 0 header length * Fix travis build and coverage * Coverage * Update docs and fix message header length * Update docs * Fixed example message in unpack test * PHP CS (#5) * Port to docker-compose * Refactor bitmap calculation * [#7] Fixed handing of DateTime fields (#8) --- examples/pack.php | 2 + examples/unpack.php | 13 ++- src/Message/Packer/MessagePacker.php | 12 +-- src/Message/Schema/ISO8583.php | 82 +++++++++---------- .../Schema/Validator/FieldValidator.php | 24 ------ src/Message/Unpacker/MessageUnpacker.php | 8 +- 6 files changed, 60 insertions(+), 81 deletions(-) diff --git a/examples/pack.php b/examples/pack.php index 5c73e44..9cf17be 100644 --- a/examples/pack.php +++ b/examples/pack.php @@ -32,7 +32,9 @@ * We are now able to set fields on the message schema through the schema manager (with the help of type hinting). * The schema manager will log the fields we set in preparation for packing the generated message. */ +$schemaManager->setProcessingCode(123625); $schemaManager->setCurrencyCodeCardholderBilling('GBP'); +$schemaManager->setLocalTransactionTime((new DateTime("now"))->format("His")); $schemaManager->setPrivateReserved6('sample'); /* diff --git a/examples/unpack.php b/examples/unpack.php index 9697d35..3b81631 100644 --- a/examples/unpack.php +++ b/examples/unpack.php @@ -41,14 +41,13 @@ * We are now able define our message. We need to set the ISO message to parse and the header length. The length and * fields will be validated when they are unpacked. */ -$isoMessage = "012430323030f23e4491a8e08020000000000000002031362a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a3030303030303030303030" . - "3030303130303031323132313435343038303030303039313435343038313231323137303331323133303030303930323030433030303030" . - "3030303036303030303230303630303030323033372a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a" . - "2a2a504652333437303030303039323837353937353830303030303030303030333039333733303134373430342054657374204167656e74" . - "203220204861746669656c64202020202048654742383236303238303030303030323136317c303030307c50465233343730303030303930" . - "3135353630313031323634303243313031"; - $message->setHeaderLength(2); +$isoMessage = "012430323030f23e4491a8e08020000000000000002031363030303030303030303030303030303030303030303030303030" . + "3030303031303030313231323134353430383030303030393134353430383132313231373033313231333030303039303230" . + "304330303030303030303036303030303230303630303030323033372a2a2a2a2a2a2a2a2a3d2a2a2a2a2a2a2a2a2a2a2a2a" . + "2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a5046523334373030303030393238373539373538303030303030303030303330393337" . + "33303134373430342054657374204167656e74203220204861746669656c6420202020204865474238323630323830303030" . + "3030323136317c303030307c504652333437303030303039303135353630313031323634303243313031"; /* * 5) Parse the unpacked message diff --git a/src/Message/Packer/MessagePacker.php b/src/Message/Packer/MessagePacker.php index fae37dc..b8a488a 100644 --- a/src/Message/Packer/MessagePacker.php +++ b/src/Message/Packer/MessagePacker.php @@ -142,17 +142,19 @@ protected function parseBitmap(array $setFields): string */ protected function parseDataElement(array $setFields): string { - ksort($setFields); - $schemaCache = $this->cacheManager->getSchemaCache($this->schemaManager->getSchema()); + $dataCache = []; - $dataElement = ''; foreach ($setFields as $field) { $fieldData = $schemaCache->getDataForProperty($field); - $dataElement .= $fieldData->getMapper()->pack($this->schemaManager->{$fieldData->getGetterName()}()); + $dataCache[$fieldData->getBit()] = $fieldData->getMapper()->pack( + $this->schemaManager->{$fieldData->getGetterName()}() + ); } - return $dataElement; + ksort($dataCache); + + return implode("", $dataCache); } } diff --git a/src/Message/Schema/ISO8583.php b/src/Message/Schema/ISO8583.php index 11702dd..e98d30f 100644 --- a/src/Message/Schema/ISO8583.php +++ b/src/Message/Schema/ISO8583.php @@ -2,8 +2,6 @@ namespace Kaperys\Financial\Message\Schema; -use DateTime; - /** * Class ISO8583 (1987 spec) * @@ -70,7 +68,7 @@ class ISO8583 implements MessageSchemaInterface protected $amountCardholderBilling; /** - * @var DateTime + * @var string * * @bit 7 * @display n @@ -121,7 +119,7 @@ class ISO8583 implements MessageSchemaInterface protected $systemsTraceAuditNumber; /** - * @var DateTime + * @var string * * @bit 12 * @display n @@ -132,7 +130,7 @@ class ISO8583 implements MessageSchemaInterface protected $localTransactionTime; /** - * @var DateTime + * @var string * * @bit 13 * @display n @@ -143,7 +141,7 @@ class ISO8583 implements MessageSchemaInterface protected $localTransactionDate; /** - * @var DateTime + * @var string * * @bit 14 * @display n @@ -154,7 +152,7 @@ class ISO8583 implements MessageSchemaInterface protected $expirationDate; /** - * @var DateTime + * @var string * * @bit 15 * @display n @@ -165,7 +163,7 @@ class ISO8583 implements MessageSchemaInterface protected $settlementDate; /** - * @var DateTime + * @var string * * @bit 16 * @display n @@ -176,7 +174,7 @@ class ISO8583 implements MessageSchemaInterface protected $conversionDate; /** - * @var DateTime + * @var string * * @bit 17 * @display n @@ -767,7 +765,7 @@ class ISO8583 implements MessageSchemaInterface protected $messageNumberLast; /** - * @var DateTime + * @var string * * @bit 73 * @display n @@ -1481,19 +1479,19 @@ public function setAmountCardholderBilling(int $amountCardholderBilling): ISO858 } /** - * @return DateTime + * @return string */ - public function getTransmissionDateTime(): DateTime + public function getTransmissionDateTime(): string { return $this->transmissionDateTime; } /** - * @param DateTime $transmissionDateTime + * @param string $transmissionDateTime * * @return ISO8583 */ - public function setTransmissionDateTime(DateTime $transmissionDateTime): ISO8583 + public function setTransmissionDateTime(string $transmissionDateTime): ISO8583 { $this->transmissionDateTime = $transmissionDateTime; return $this; @@ -1576,114 +1574,114 @@ public function setSystemsTraceAuditNumber(int $systemsTraceAuditNumber): ISO858 } /** - * @return DateTime + * @return string */ - public function getLocalTransactionTime(): DateTime + public function getLocalTransactionTime(): string { return $this->localTransactionTime; } /** - * @param DateTime $localTransactionTime + * @param string $localTransactionTime * * @return ISO8583 */ - public function setLocalTransactionTime(DateTime $localTransactionTime): ISO8583 + public function setLocalTransactionTime(string $localTransactionTime): ISO8583 { $this->localTransactionTime = $localTransactionTime; return $this; } /** - * @return DateTime + * @return string */ - public function getLocalTransactionDate(): DateTime + public function getLocalTransactionDate(): string { return $this->localTransactionDate; } /** - * @param DateTime $localTransactionDate + * @param string $localTransactionDate * * @return ISO8583 */ - public function setLocalTransactionDate(DateTime $localTransactionDate): ISO8583 + public function setLocalTransactionDate(string $localTransactionDate): ISO8583 { $this->localTransactionDate = $localTransactionDate; return $this; } /** - * @return DateTime + * @return string */ - public function getExpirationDate(): DateTime + public function getExpirationDate(): string { return $this->expirationDate; } /** - * @param DateTime $expirationDate + * @param string $expirationDate * * @return ISO8583 */ - public function setExpirationDate(DateTime $expirationDate): ISO8583 + public function setExpirationDate(string $expirationDate): ISO8583 { $this->expirationDate = $expirationDate; return $this; } /** - * @return DateTime + * @return string */ - public function getSettlementDate(): DateTime + public function getSettlementDate(): string { return $this->settlementDate; } /** - * @param DateTime $settlementDate + * @param string $settlementDate * * @return ISO8583 */ - public function setSettlementDate(DateTime $settlementDate): ISO8583 + public function setSettlementDate(string $settlementDate): ISO8583 { $this->settlementDate = $settlementDate; return $this; } /** - * @return DateTime + * @return string */ - public function getConversionDate(): DateTime + public function getConversionDate(): string { return $this->conversionDate; } /** - * @param DateTime $conversionDate + * @param string $conversionDate * * @return ISO8583 */ - public function setConversionDate(DateTime $conversionDate): ISO8583 + public function setConversionDate(string $conversionDate): ISO8583 { $this->conversionDate = $conversionDate; return $this; } /** - * @return DateTime + * @return string */ - public function getCaptureDate(): DateTime + public function getCaptureDate(): string { return $this->captureDate; } /** - * @param DateTime $captureDate + * @param string $captureDate * * @return ISO8583 */ - public function setCaptureDate(DateTime $captureDate): ISO8583 + public function setCaptureDate(string $captureDate): ISO8583 { $this->captureDate = $captureDate; return $this; @@ -2716,19 +2714,19 @@ public function setMessageNumberLast(int $messageNumberLast): ISO8583 } /** - * @return DateTime + * @return string */ - public function getDateAction(): DateTime + public function getDateAction(): string { return $this->dateAction; } /** - * @param DateTime $dateAction + * @param string $dateAction * * @return ISO8583 */ - public function setDateAction(DateTime $dateAction): ISO8583 + public function setDateAction(string $dateAction): ISO8583 { $this->dateAction = $dateAction; return $this; diff --git a/src/Message/Schema/Validator/FieldValidator.php b/src/Message/Schema/Validator/FieldValidator.php index 2e2ffa9..2ce0260 100644 --- a/src/Message/Schema/Validator/FieldValidator.php +++ b/src/Message/Schema/Validator/FieldValidator.php @@ -2,7 +2,6 @@ namespace Kaperys\Financial\Message\Schema\Validator; -use DateTime; use Kaperys\Financial\Container\PropertyAnnotationContainer; use Kaperys\Financial\Message\Constants\Display; use Kaperys\Financial\Message\Schema\Validator\Exception\FieldValidationException; @@ -45,21 +44,6 @@ public function validate(PropertyAnnotationContainer $propertyAnnotationContaine */ protected function validateType(PropertyAnnotationContainer $propertyAnnotationContainer, $data): bool { - if ($propertyAnnotationContainer->getType() == 'DateTime') { - if (!$data instanceof DateTime) { - $exception = new FieldValidationException( - 'Bit ' . $propertyAnnotationContainer->getBit() . ' should be an instance of DateTime' - ); - - $exception->setData($data); - $exception->setPropertyAnnotationContainer($propertyAnnotationContainer); - - throw $exception; - } - - return true; - } - // @todo: Complete this display-based validation if (Display::ALPHA == $propertyAnnotationContainer->getDisplay()) { @@ -158,10 +142,6 @@ protected function validateType(PropertyAnnotationContainer $propertyAnnotationC */ protected function validateFixedLengthField(PropertyAnnotationContainer $propertyAnnotationContainer, $data): bool { - if ('DateTime' == $propertyAnnotationContainer->getType()) { - return true; - } - if ($propertyAnnotationContainer->getLength() != strlen($data)) { throw new FieldValidationException( 'Bit ' . $propertyAnnotationContainer->getBit() . ' should be length ' . @@ -186,10 +166,6 @@ protected function validateVariableLengthField( PropertyAnnotationContainer $propertyAnnotationContainer, $data ): bool { - if ('DateTime' == $propertyAnnotationContainer->getType()) { - return true; - } - if (strlen($data) > $propertyAnnotationContainer->getMaxLength() || strlen($data) < $propertyAnnotationContainer->getMinLength() ) { diff --git a/src/Message/Unpacker/MessageUnpacker.php b/src/Message/Unpacker/MessageUnpacker.php index 6347a86..66d1d6d 100644 --- a/src/Message/Unpacker/MessageUnpacker.php +++ b/src/Message/Unpacker/MessageUnpacker.php @@ -67,12 +67,14 @@ public function parse(string $message): MessageUnpacker // Parse the bitmap $bitmap = $this->parseBitmap($message); + $numberOfBitmaps = 1; + if (strlen($bitmap) > 64) { $numberOfBitmaps = 2; - } elseif (strlen($bitmap) > 128) { + } + + if (strlen($bitmap) > 128) { $numberOfBitmaps = 3; - } else { - $numberOfBitmaps = 1; } // Message without bitmaps