Sep 20, 2018

Swift4 - php hash_hmac to iOS hash

While working on APIs this could be one problem when the docs only written in php. :(

While looking the solution I found CryptoSwift which could help and solve my problem. Please check CryptoSwift on how to install.

Hear's same php to iOS conversion code.
PHP hash_hmac:
$hash = hash_hmac( 'SHA512', "YOUR_STRING_DATA_TO_HASH_HERE", 'YOUR_SECRET_KEY' // SHARED KEY ); print ("HMAC:" . $hash);

XCODE hmac
let yourData: Array = Array("YOUR_STRING_DATA_TO_HASH_HERE".utf8)
do{
   let myhmac = try HMAC(key: "YOUR_SECRET_KEY", variant: .sha512)

    // great your found your hash
   let iosHash = try myhmac.authenticate(password).toHexString()
   print ("HMAC:", iosHash)
}catch let err as NSError{
    
}


PHP hash:
$hash = hash( 'SHA512', "YOUR_STRING_DATA_TO_HASH_HERE" ); print ("HMAC:" . $hash);

XCODE Hash
print ("Hash:", "YOUR_STRING_DATA_TO_HASH_HERE".sha512())


NOTED: Be sure you import the CryptoSwift in your swift code.

Thanks for passing by.

Aug 9, 2018

Xcode - Rendering HTML code to your UILabel View

Im have been search on the net but most I see was still on Objective-c Language. Here what I discovered solution.
let htmlText = "<ul><li><b>Heloo</b></li><li>World</li></ul><hr>
<ol><li>Im <b>Heloo</b></li><li>World</li></ol>"
if let htmlData = htmlText.data(using: String.Encoding.unicode) {
  do {

    let attributedText = try NSAttributedString(data: htmlData,
    options: [
NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html
],
    documentAttributes: nil)

    //Setting htmltext to uilable
    detailLabel.attributedText = attributedText

  } catch let e as NSError {
    //setting plane text to uilable cause of err
    detailLabel.text = htmlText
    print("Couldn't translate \(htmlText): \(e.localizedDescription) ")
  }
}

Update:
* Some reported that it will affect the scroll ability of table view. I never tested yet since I didn't work on tableview for this purpose.

Oct 9, 2017

Magento2 - Unittest with Object manager

As much as possible avoid using object manager to create object on magento if your going to create unittest on it. Use the Factory instead but in some case some plugins(Unirgy) love the object manager. Then when you override there class you will getting headache on creating unittest.

One of the error you will encounter was
"Expectation failed for method name is equal to <string:create> when invoked zero or more times" this  happen  you call create method  multiple times.
Sample:

 /** @var \Winz\Sales\Api\Data\OrderInvoiceResultInterface */
$orderInvoiceResultInterface = $this->getMockBuilder('Winz\Sales\Api\Data\OrderInvoiceResultInterface')
    ->disableOriginalConstructor()              
    ->setMethods(['setOrder', 'setInvoice', 'getOrder', 'getInvoice', ])
    ->getMock();      
$orderInvoiceResult = $objectManagerInterface
    ->expects($this->any())
    ->method('create')
    ->with('Storm\Sales\Api\Data\OrderInvoiceResultInterface')
    ->willReturn($orderInvoiceResultInterface);
/** @var \Winz\Sales\Api\Data\OrderResultInterface */
$orderResultInterface = $this->getMockBuilder('Winz\Sales\Api\Data\OrderResultInterface')
    ->disableOriginalConstructor()
    ->setMethods(['setId', 'setNo', 'getId', 'getNo'])
    ->getMock();      
$orderResult = $objectManagerInterface
    ->expects($this->once())
    ->method('create') 
  ->with('Winz\Sales\Api\Data\OrderResultInterface')
    ->willReturn($orderResultInterface);

Fixed of the above code.
$orderResultInterface = $this->getMockBuilder('Storm\Sales\Api\Data\OrderResultInterface')
    ->disableOriginalConstructor()
    ->getMock();
$orderInvoiceResultInterface = $this->getMockBuilder('Winz\Sales\Api\Data\OrderInvoiceResultInterface')
    ->disableOriginalConstructor()              
    ->setMethods(['setOrder', 'setInvoice', 'getOrder', 'getInvoice', ])
    ->getMock();      
$objectManagerInterface
    ->expects($this->any())
    ->method('create')
    ->withConsecutive(
            ['Storm\Sales\Api\Data\OrderResultInterface'],
            ['Storm\Sales\Api\Data\OrderInvoiceResultInterface']
    )
    ->willReturnOnConsecutiveCalls(
            $this->returnValue($orderResultInterface),
            $this->returnValue($orderInvoiceResultInterface)
    );


Sep 26, 2017

Magento2 - List of Validation Rules

List of form validation rules
jQuery rules:
required,
remote,
email,
url,
date,
dateISO,
number,
digits,
creditcard,
equalTo,
maxlength,
minlength,
rangelength,
range,
max,
min
Magento rules:
max-words
min-words
range-words
letters-with-basic-punc
alphanumeric
letters-only
no-whitespace
zip-range
integer
vinUS
dateITA
dateNL
time
time12h
phoneUS
phoneUK
mobileUK
stripped-min-length
email2
url2
credit-card-types
ipv4
ipv6
pattern
allow-container-className
validate-no-html-tags
validate-select
validate-no-empty
validate-alphanum-with-spaces
validate-data
validate-street
validate-phoneStrict
validate-phoneLax
validate-fax
validate-email
validate-emailSender
validate-password
validate-admin-password
validate-customer-password
validate-url
validate-clean-url
validate-xml-identifier
validate-ssn
validate-zip-us
validate-date-au
validate-currency-dollar
validate-not-negative-number
validate-zero-or-greater
validate-greater-than-zero
validate-css-length
validate-number
required-number
validate-number-range
validate-digits
validate-digits-range
validate-range
validate-alpha
validate-code
validate-alphanum
validate-date
validate-date-range
validate-cpassword
validate-identifier
validate-zip-international
validate-one-required
validate-state
required-file
validate-ajax-error
validate-optional-datetime
validate-required-datetime
validate-one-required-by-name
less-than-equals-to
greater-than-equals-to
validate-emails
validate-cc-type-select
validate-cc-number
validate-cc-type
validate-cc-exp
validate-cc-cvn
validate-cc-ukss
validate-length
required-entry
not-negative-amount
validate-per-page-value-list
validate-per-page-value
validate-new-password
required-if-not-specified
required-if-all-sku-empty-and-file-not-loaded
required-if-specified
required-number-if-specified
datetime-validation
required-text-swatch-entry
required-visual-swatch-entry
required-dropdown-attribute-entry
Validate-item-quantity
validate-grouped-qty
validate-one-checkbox-required-by-name
validate-date-between
validate-dob