Passport validation :: check digit example

 
<?php
 
    function calcCheckDigit($inputCode)
    {
        $btArray = str_split($inputCode);
        $total = 0;
 
        for ($index = 0; $index < sizeof($btArray); $index++) {
            $btChr = ord($btArray[$index]);
 
            if ($btChr == 60) {
                //convert spacer char < to 0
                $btArray[$index] = 0;
            } else if ($btChr >= 65) {
                //convert letters A-Z to 10-35
                $btArray[$index] = $btChr - 55;
            } else {
                //take numbers literally
                $btArray[$index] = $btChr - 48;
            }
 
            switch ($index % 3) {
                case 0:
                    $btArray[$index] *= 7;
                    break;
                case 1:
                    $btArray[$index] *= 3;
                    break;
                case 2:
                    $btArray[$index] *= 1;
                    break;
            }
 
            $total += $btArray[$index];
        }
 
        return (int) $total % 10;
    }
 
	$mrz2 = '7553279419RUS8712242M2104131>>>>>>>>>>>>>>02';
 
	// check the document number vs its check digit
	$documentNumber = substr($mrz2, 0, 9);
        $documentNumberCheckDigit = (int) substr($mrz2, 9, 1);
 
        if ($documentNumberCheckDigit != calcCheckDigit($documentNumber)) {
		echo "document number incorrect.\n";
        }
 
	// check the DOB vs its check digit
	$dob = substr($mrz2, 13, 6);
	$dobCheckDigit = (int) substr($mrz2, 19, 1);
 
        if ($dobCheckDigit != calcCheckDigit($dob)) {
		echo "DOB incorrect.\n";
        }
 
	// check the DateOfExpiry vs its check digit
	$dateOfExpiry = substr($mrz2, 21, 6);
	$dateOfExpiryCheckDigit = (int) substr($mrz2, 27, 1);
 
        if ($dateOfExpiryCheckDigit != calcCheckDigit($dateOfExpiry)) {
		echo "Date Of Expiry incorrect.\n";
        }
 
 
	// check the optional data field vs its check digit
	$optionalData = substr($mrz2, 28, 14);
 
        if ($optionalData != str_repeat(chr(60),14)) {
 
		$optionalDataCheckDigit = (int) substr($mrz2, 42, 1);
                if ($optionalDataCheckDigit != self::calcCheckDigit($optionalData)) {
			echo "Optional data incorrect.\n";
                }
        }
 
	//positions 1 to 10, 14 to 20, and 22 to 43
        $wholeLine = substr($mrz2, 0, 10) . substr($mrz2, 13, 7) . substr($mrz2, 21, 22);
	$wholeLineCheckDigit = (int) substr($mrz2, 43, 1);
 
	if ($wholeLineCheckDigit != self::calcCheckDigit($wholeLine)) {
			echo "Whole line incorrect.\n";
	}

PHP The static Keyword for caching and speeding up

Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job.
To do this use the static keyword.

PHP Recursive function

 
<?php
 
function recursive(&$myarray, $dir, $recursiveDir = null) {
 
    $items = scandir($dir);
 
    foreach ($items as $item) {
 
        if ($item != "." && $item != "..") {
 
            $file = $dir. DIRECTORY_SEPARATOR. $item;
 
            if (is_dir($file)) {
                definitions($routes, $file, $file);
            }
            else {
                // get name of api        
                $expl = explode(".", $item);
 
                $key = ($recursiveDir !== null) ? basename($recursiveDir). '.'. $expl[0] : $expl[0];
                $value = $expl[1];
 
                $myarray[$key] = $value;
	}
}
 
$myarray = array();
recursive($myarray, ROOT . "app/project". DIRECTORY_SEPARATOR);
 
print_r($myarray,1);

PHP Functional Programming, helper function, annonymous function, closure

 
<?php
$url = function($folder) use ($hostname) {
return $hostname . DIRECTORY_SEPARATOR . $folder;
};
 
$hostname = "https://" . $_SERVER['SERVER_NAME'];
 
$myurl = $url('pictures');
echo $myurl;
 
// output
// https://www.aghayev.com/pictures

Stateless – when the application doesn’t save anything to the disk or to the RAM that is required to process future workloads. The benefit of stateless application is that it is possible to run multiple instances of an application in order to distribute workload and increase availability.

Sample transactional stored procedure

 
CREATE DEFINER=`root`@`localhost` PROCEDURE `Sample`(IN inParameterID VARCHAR(10))
Sample:BEGIN
 
DECLARE httpStatus INT DEFAULT 500;
DECLARE errorCode INT DEFAULT NULL;
DECLARE errorDetails TEXT DEFAULT NULL;
 
DECLARE EXIT HANDLER FOR SQLEXCEPTION, SQLWARNING
BEGIN
    ROLLBACK;
    SELECT 500 AS statusCode, 'Failed' AS statusMessage;
END;
 
SET @variableID = 0;
 
SELECT  TableA.ID INTO @variableID
FROM TableA  
WHERE TableA.ParameterID = inParameterID;
 
IF @variableID = 0 THEN
    	SELECT 500 AS errorCode, 'Transaction failed' AS errorMessage;
	LEAVE T2activatePersonStandby;
END IF;
 
START TRANSACTION;
 
UPDATE TableA;
 
UPDATE TableB;
 
UPDATE TableC;
 
END IF;
 
COMMIT;
 
SELECT 200 AS statusCode, 'Success' AS statusMessage;
 
END

PHP PSR-4 and Namespaces

How-to add namespaces to old php project

https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md

Simple project with namespaces
https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md

Bitbucket is good for free private repos
Github is good for free public repos, for demonstration of your work

Difference between Strategy and Factory design patterns

A Strategy pattern is an operational pattern. Strategy pattern allows you to polymorphically change behavior of a class. Strategy pattern is to avoid conditionals switching in favor of polymorphism.
So, Strategy pattern = polymorphism.

A Factory pattern is a creational pattern. Factory pattern allows you to encapsulate object creation.
Factory pattern = encapsulation.

A difference between Simple Factory and Abstract Factory. Simple Factory – you only have one class which acts as a factory for object creation. Abstract Factory – you connect to an interface and then call the different factories that implement this interface which are supposed to have different implementations of the same method based on some criteria.

Abstract Factory looks like a strategy pattern, but it differs from it semantically because it is used for OBJECT CREATION rather than operations. So, basically you have object creation using different strategies.

PHP PSR-1 Explained

      Class names MUST be declared in StudlyCaps.
      Class constants MUST be declared in all UPPER_CASE with underscore separators.
      Method names MUST be declared in camelCase.