Remove untracked file

git reset

Phpunit Mock vs Stub

Моcк — имитация объекта
Stub — заглушка для метода

Git using subtree

1. Adding subtree to current project

git remote add mysubproject git@github.com:iaghayev/mysubproject.git
 
git subtree add --prefix=path/to/somedir mysubproject master

2. Committing changes to subproject from path/to/somedir folder

git subtree push --prefix=path/to/somedir mysubproject master

3. Getting latest code changes from subproject

git subtree pull --prefix=path/to/somedir mysubproject master

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