PHP code example of belca / support

1. Go to this page and download the library: Download belca/support library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

belca / support example snippets


use Belca\Support\AbstractConstants;

class FirstConstants extends AbstractConstants
{
    const USER = 'user';
    const SUPERUSER = 'superuser';
    const CLIENT = 'client';
    const MODERATOR = 'moderator';
    const SUPERMODERATOR = 'super'.self::USER;
}

class SecondConstants extends FirstConstants
{
    const VIEWER = 'viewer';
    const CHECKER = 'checker';
    const TESTER = 'tester';
    const SUPERUSER = 'root'; // заменяет предыдущее значение
    const SUPERMODERATOR = 'supermoderator'; // заменяет предыдущее значение
}

// Получим все константы классов
$allFirstConstants = FirstConstants::getConstants();
$allSecondConstants = SecondConstants::getConstants();

// Output $allFirstConstants: [
//    'USER' => 'user',
//    'SUPERUSER' => 'superuser',
//    'CLIENT' => 'client',
//    'MODERATOR' => 'moderator',
//    'SUPERMODERATOR' => 'superuser',
// ]
//
// Output $allSecondConstants: [
//    'USER' => 'user',
//    'SUPERUSER' => 'root',
//    'CLIENT' => 'client',
//    'MODERATOR' => 'moderator',
//    'SUPERMODERATOR' => 'superuser',
//    'VIEWER' => 'viewer',
//    'CHECKER' => 'checker',
// ]

// Получим конкретные константы FirstConstants
$user = FirstConstants::getConst('USER'); // 'user'
$superuser = FirstConstants::getConst('SUPERUSER'); // 'superuser'
$root = FirstConstants::getConst('ROOT'); // null

// Получим конкретные константы SecondConstants
$user = SecondConstants::getConst('USER'); // 'user'
$superuser = SecondConstants::getConst('SUPERUSER'); // 'root'
$root = SecondConstants::getConst('ROOT'); // null

// Проверим существование констант
SecondConstants::isDefined('SUPERUSER'); // true
SecondConstants::isDefined('ROOT'); // false

$superuser = SecondConstants::SUPERUSER; // 'root'
$root = SecondConstants::ROOT; // Error: Undefined class constant 'ROOT'

use Belca\Support\AbstractEnum;

class FirstConstants extends AbstractEnum
{
    const DEFAULT = self::USER;

    const USER = 'user';
    const SUPERUSER = 'root';
    const CLIENT = 'client';
}

$defaultValue = FirstConstants::getDefault(); // 'user'

use Belca\Support\Arr;

// или

$result = \Belca\Support\Arr::trim($array); // и другие функции

$array = [
    ' value ',
    'trim',
    'one ',
    ' two',
    '   three    ',
    1,
    2,
    'string',
    null,
    ['  no trim  '],
];

$result = Arr::trim($array);

// Output: ['value', 'trim', 'one', 'two', 'three', 1, 2, 'string', null, ['  no trim  ']];

$notArray = null;
$result = Arr::trim($notArray);

// Output: [];

$array4 = [1, 2, null, '', [], new stdClass, false, 0];

$result = Arr::removeEmpty($array);

// Output: [1, 2, 5 => new stdClass];

$array = [1, 2, null, '', [], new stdClass, false, 0];

$result = Arr::removeNull($array);  

// Output: [1, 2, 3 => '', [], new stdClass, false, 0];

$array = [1, 2, null, '', [], new stdClass, false, 0];
$result = Arr::removeNotScalar($array);

// Output: [0 => 1, 1 => 2, 3 => '', 6 => false, 7 => 0];

$array = [
    1 => [1, 2, 3 => [1, 2, 3, 4, [], null], 4, ''],
    -2,
    'a3' => [
        1,
        2,
        'a3.3' => [0, 1, 2, 3],
    ],
    null,
    '',
    0,
    false,
];

$result = Arr::removeEmptyRecurcive($array);

// Output:
// [
//    1 => [0 => 1, 1 => 2, 2 => [1, 2, 3, 4], 3 => 4],
//    2 => -2,
//    'a3' => [
//         0 => 1,
//         1 => 2,
//         'a3.3' => [0 => 1, 1 => 2, 2 => 3],
//    ],
// ]

$array = [
    1 => [1, 2, 3 => [1, 2, 3, 4, [], null], 4, ''],
    -2,
    'a3' => [
        1,
        2,
        'a3.3' => [0, 1, 2, 3],
    ],
    null,
    '',
    0,
    false,
];

$result = Arr::removeEmptyRecurcive($array, false);

// Output:
// [
//    1 => [1, 2, 3 => [1, 2, 3, 4], 4 => 4],
//    2 => -2,
//    'a3' => [
//         0 => 1,
//         1 => 2,
//         'a3.3' => [1 => 1, 2 => 2, 3 => 3],
//    ],
// ]

$array = [
    1 => [1, 2, 3 => [1, 2, 3, 4, [], null], 4, ''],
    -2,
    4 => [
        1 => 1,
        2 => 2,
        'a3.3' => [0, 1, 2, 3],
    ],
    null,
    '',
    0,
    false,
];

$result = Arr::removeNullRecurcive($array);

// Output:
// [
//    0 => [0 => 1, 1 => 2, 2 => [1, 2, 3, 4, []], 3 => 4, 4 => ''],
//    1 => -2,
//    2 => [
//         1 => 1,
//         2 => 2,
//         'a3.3' => [0 => 1, 1 => 2, 2 => 3],
//    ],
//    3 => '',
//    4 => 0,
//    5 => false,
// ]

$array = [
    1 => [1, 2, 3 => [1, 2, 3, 4, [], null], 4, ''],
    -2,
    4 => [
        1 => 1,
        2 => 2,
        'a3.3' => [0, 1, 2, 3],
    ],
    null,
    '',
    0,
    false,
];

$result = Arr::removeNullRecurcive($array, false);

// Output:
// [
//    1 => [1, 2, 3 => [1, 2, 3, 4, []], 4 => 4, 5 => ''],
//    2 => -2,
//    4 => [
//         1 => 1,
//         2 => 2,
//         'a3.3' => [1 => 1, 2 => 2, 3 => 3],
//    ],
//    6 => '',
//    7 => 0,
//    8 => false,
// ]

$array1 = [1, 2, 3, 4, 5, 6, 7, 8, 10]; // true
$array2 = []; // false
$array3 = 1; // false
$array4 = ['1' => 1, 2, 3, '4' => 4]; // true
$array5 = [50 => 1, 'a2' => 3, 'a3' => 4, 0 => 1]; // false

$result1 = Arr::isArrayWithIntKeys($array1); // true
$result2 = Arr::isArrayWithIntKeys($array2); // false, потому что пустой массив
$result3 = Arr::isArrayWithIntKeys($array3); // false, потому что не массив
$result4 = Arr::isArrayWithIntKeys($array4); // true, потому что числа в строке преобразованы в integer
$result5 = Arr::isArrayWithIntKeys($array5); // false

$normalArray = [1, 2, 3, 4, 5, 6, 7, 8, 10]; // true
$badArray = [50 => 1, 'a2' => 3, 'a3' => 4, 0 => 1]; // false

$result1 = Arr::isIntKeys($normalArray); // true
$result2 = Arr::isIntKeys($badArray); // false

$array1 = [1, 2, 3, 4, 5, 6, 7, 8, 10]; // true
$array2 = []; // false
$array3 = 1; // false
$array4 = ['1' => 1, 2, 3, '4' => 4]; // true
$array5 = [50 => 1, 'a2' => 3, 'a3' => 4, 0 => 1]; // true
$array6 = [50 => 1, 'a2' => 3, 'a3' => 4, 'one' => 1]; // false

$result1 = Arr::isFirstLastWithIntKeys($array1); // true
$result2 = Arr::isFirstLastWithIntKeys($array2); // false, потому что пустой массив
$result3 = Arr::isFirstLastWithIntKeys($array3); // false, потому что не массив
$result4 = Arr::isFirstLastWithIntKeys($array4); // true, потому что числа в строке преобразованы в integer
$result5 = Arr::isFirstLastWithIntKeys($array5); // true, потому что первый и последний ключ является числовым
$result6 = Arr::isFirstLastWithIntKeys($array6); // false, потому что последний ключ строка

$source = [1, 2, 3, 4, 5, 6];
$array = [6, 7, 8, 9, 10, 11, 12];

Arr::concatArray($source, $array);

// Output $source: [6, 7, 8, 9, 10, 11, 12];

$source = [1, 2, 3, 4, 5, 6];
$array = [6 => 6, 7, 8, 9, 10, 11, 12];

Arr::concatArray($source, $array);

// Output $source: [1, 2, 3, 4, 5, 6, 6, 7, 8, 9, 10, 11, 12];

$source = ['key1' => 1, 'key2' => 2];
$newValues = ['key2' => 3, 'key3' => 4];

Arr::concatArray($source, $newValues);

// Output $source: ['key1' => 1, 'key2' => 3, 'key3' => 4];

$source = ['key1' => 1, 'key2' => 2];
$newValues = ['key2' => 3, 'key3' => 4];

Arr::concatArray($source, $newValues, false);

// Output $source: ['key1' => 1, 'key2' => 2, 'key3' => 4];

$array = [
    1,
    2,
    3,
    'four' => 4,
    'five' => 5,
    'matrix' => [
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9],
    ],
    7,
    'eight' => 8,
    9,
    'symbols' => ['a', 'b', 'c'],
    'object' => new stdClass(),
];

$result = Arr::removeArrays($array);

// Output: [
//    1,
//    2,
//    3,
//    'four' => 4,
//    'five' => 5,
//    7,
//    'eight' => 8,
//    9,
//    'object' => new stdClass(),
// ];

$array = [
    1,
    2,
    3,
    'four' => 4,
    'five' => 5,
    'matrix' => [
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9],
    ],
    7,
    'eight' => 8,
    9,
    'symbols' => ['a', 'b', 'c'],
    'object' => new stdClass(),
];

$result = Arr::removeArrays($array);

// Output: [1, 2, 3, 4, 5, 7, 8, 9, new stdClass()];

$array = [5 => 1, 2, 3, 4, 5];

$last = Arr::last($array); // Output: 5

$array = [
    1,
    2,
    3,
    'four' => 4,
    'five' => 5,
    'matrix' => [
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9],
    ],
    7,
    'eight' => 8,
    9,
    'symbols' => ['a', 'b', 'c'],
    'object' => new stdClass(),
];

$output = Arr::unset($array, 0, 'four', 'eight', 4);
$output = Arr::unset($array, [0, 'four', 'eight', 4]);
$output = Arr::unset($array, [0, 'four'], ['eight', 4]);
$output = Arr::unset($array, [0, 'four'], [['eight'], [4], []]);

// Output:
// [
//    1 => 2,
//    2 => 3,
//   'five' => 5,
//    'matrix' => [
//        [1, 2, 3],
//        [4, 5, 6],
//         [7, 8, 9],
//    ],
//    3 => 7,
//    'symbols' => ['a', 'b', 'c'],
//    'object' => new stdClass(),
// ]

$source = [1, 2, 3, 'key1' => 1, 'key2' => 2, 'key3' => 3];
$array1 = [4, 5, 6];
$array2 = [1, 2, 3, 'key1' => 10];

Arr::pushArray($source, $array1, $array2);

// Output $source:
// [1, 2, 3, 'key1' => 10, 'key2' => 2, 'key3' => 3, 4, 5, 6, 1, 2, 3]

$array = ['finfo.created', 'finfo.size' => 'filesize', 'finfo.mime' => 'mime'];

$keys = SpecialArr::originalKeys($array);

// Output: ['finfo.created', 'finfo.size', 'finfo.mime']

$array = ['finfo.created', 'finfo.size', 'finfo.mime'];

$keys = SpecialArr::originalKeys($array);

// Output: ['finfo.created', 'finfo.size', 'finfo.mime']

$array = [
    'name' => 'phone',
    'maxlength' => 20,
    'class' => 'input input_type_primary input_width_medium',
    'put input_type_primary input_width_medium" 

$array = [
    'finfo' => [
        'size' => 998,
        'mime' => 'image/png',
    ],
];

$value = SpecialArr::pullThroughSeparator($array, 'finfo.size'); // Output: 998

$value = SpecialArr::pullThroughSeparator($array, 'finfo');
// Output: [
//     'size' => 998,
//     'mime' => 'image/png'
// ]

$value = SpecialArr::pullThroughSeparator($array, 'finfo.date'); // Output: null

use Belca\Support\HTML;

$html = "<p><b>Lorem ipsum dolor</b> sit amet, consectetur adipisicing elit, <hr> sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.<br><img src='/path/to/img.jpg'></p>";

$html = HTML::removeTags($html);

// Output: "Lorem ipsum dolor sit amet, consectetur adipisicing elit,  sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."

$html = "<p><b class='color_red'>Lorem ipsum dolor</b> sit amet, consectetur adipisicing elit, <hr> sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.<br><img src='/path/to/img.jpg'></p>";
$allowedTags = ['b', 'hr'];

$html = HTML::removeTags($html);

// Output: "<b>Lorem ipsum dolor</b> sit amet, consectetur adipisicing elit, <hr> sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."

$html = "<p><b class='color_red' style='color: green;'>Lorem ipsum dolor</b> sit amet, consectetur adipisicing elit, <hr> sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.<br><img src='/path/to/img.jpg'></p>";
$allowedTags = ['b', 'hr'];
$clearTags = true;
$allowedAttributes = ['class'];

$html = HTML::removeTags($html, $allowedTags, $clearTags, $allowedAttributes);

// Output: "<b class='color_red'>Lorem ipsum dolor</b> sit amet, consectetur adipisicing elit, <hr> sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."

$html = "<p><b class='color_red' style='color: green;'>Lorem ipsum dolor</b> sit amet, consectetur adipisicing elit, <hr> sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.<br><img src='/path/to/img.jpg'></p>";
$allowedTags = ['b', 'hr'];

$html = HTML::removeTags($html, $allowedTags, false);

// Output: "<b class='color_red' style='color: green;'>Lorem ipsum dolor</b> sit amet, consectetur adipisicing elit, <hr> sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."

$html = "<p><b>Lorem ipsum dolor</b> sit amet, consectetur adipisicing elit, <hr> sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.<br><img src='/path/to/img.jpg'></p>";

$html = HTML::removeTagContent($html, $tags = ['p', 'b', 'br', 'img']); // Output: ''

$html = "<p><b>Lorem ipsum dolor</b> sit amet, consectetur adipisicing elit, <hr> sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.<br><img src='/path/to/img.jpg'></p>";

$html = HTML::removeTagContent($html, $tags = ['b', 'br', 'img']);

// Output: "<p> sit amet, consectetur adipisicing elit, <hr> sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>";

$html = "<p><b>Lorem ipsum dolor</b> sit amet, consectetur adipisicing elit, <hr> sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.<br><img src='/path/to/img.jpg'></p>";

$html = HTML::removeSingleTags($html, $allowed = ['hr']);

// Output: "<p><b>Lorem ipsum dolor</b> sit amet, consectetur adipisicing elit, <hr> sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>";

use Belca\Support\HTTP;

$languages = HTTP::parseAcceptLanguage("ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3");

// Output $languages:
// [
//   "ru-RU" => 1,
//   "ru" => 0.8,
//   "en-US" => 0.5,
//   "en" => 0.3
// ];

use Belca\Support\Str;

$source = "AABC aabbcc A A aaaaAAAA A A A B B";

$result = Str::removeDuplicateSymbols($source, 'A ', 1); // 1+1 = 2 и более вхождений в подстроку

// Output: "ABC aabbcc A A aaaaA A A A B B"


$source = "AABC aabbcc A A aaaaAAAA A A A B B";

$result = Str::removeDuplicateSymbols($source, 'A', 1, true); // 1+1 = 2 вхождения

// Output: "ABC aabbcc A A aaaaAA A A A B B"

$source = "ABC aabbcc aaaaAAAA A A A B B B A A A";

$result = Str::reduceDuplicateSymbols($source, 'A', 2);

// Output: "ABC aabbcc aaaaAA A A A B B B A A A"

$source = "ABC aabbcc aaaaAAAA A A A B B B A A A";

$result = Str::reduceDuplicateSymbols($source, 'A ', 2);

// Output: "ABC aabbcc aaaaAAAA A A B B B A A A"

$path = "//path///to//directory/";
$path = Str::normalizeFilePath($path); // Output: "/path/to/directory/"

$sourceString = '/home/user/directory/';
$secondString = '/home/user/directory/subdirecotry/filename';
$result = Str::differenceSubstring($sourceString, $secondString);

// Output: 'subdirecotry/filename';

$sourceString = '/home/user/';
$secondString = '/user/directory/subdirecotry/filename';
$result = Str::differenceSubstring($sourceString, $secondString);

// Output: null

$sourceString = '';
$secondString = '/home/user/directory/subdirecotry/filename';
$result = Str::differenceSubstring($sourceString, $secondString);

// Output: '/home/user/directory/subdirecotry/filename'

$string = "Lorem ipsum dolor sit amet... dolore magna aliqua.";

# Example 1: Поиск значения без дополнительных параметров
$result = Str::findPositionFromEnd($string, "dolor"); // Output:  30

# Example 2: Поиск значения без отступа от конца строки, а возвращаемая позиция должна отсчитываться с конца строки с позиции '-1'.
$result = Str::findPositionFromEnd($string, "dolor", 0, true); // Output: -20

# Example 3: Поиск значения с отступом с конца в 20 символов.
$result = Str::findPositionFromEnd($string, "dolor", 20); // Output: 12

# Example 4: Поиск значения с отступом с конца в 20 символов и возврат значения с отчетом с конца строки.
$result = Str::findPositionFromEnd($string, "dolor", 20, true); // Output: -38

# Example 5: Поиск одного символа
$result = Str::findPositionFromEnd($string, "."); // Output: 49

# Example 6: Поиск одного символа с конца строки и возврат значения с отчетом с конца строки.
$result = Str::findPositionFromEnd($string, ".", 0, true); // Output: -1

# Example 7: Поиск несуществующего символа
$result = Str::findPositionFromEnd($string, "!"); // Output: null

$string = "Lorem ipsum dolor sit amet... dolore magna aliqua.";
$result = Str::findPositionFromEnd($string, "dolor", 0, true);

$result = ($result + 1) * (-1); // 19
// or
$result = abs($result + 1); // 19

$chain = "path.to.file";
$result = Str::firstElementOfChain($chain); // Output: 'path'

$chain = "/path/to/file";
$result = Str::firstElementOfChain($chain); // Output: 'path'

// Example 1
$chain = "path.to.file";
$nonStrict = Str::firstElementOfChain($chain, '.'); // Output: 'path'
$strict = Str::firstElementOfChain($chain, '.', true); // Output: 'path'

// Example 2
$chain = "/path/to/file";
$nonStrict = Str::firstElementOfChain($chain, '/'); // Output: 'path'
$strict = Str::firstElementOfChain($chain, '/', true); // Output: null

// Example 3
$chain = "////path/to/file";
$nonStrict = Str::firstElementOfChain($chain, '/'); // Output: 'path'
$strict = Str::firstElementOfChain($chain, '/', true); // Output: null

// Example 4
$chain = "path";
$nonStrict = Str::firstElementOfChain($chain, '/'); // Output: 'path'
$strict = Str::firstElementOfChain($chain, '/', true); // Output: 'path'

// Example 5
$chain = "==path==to==file";
$nonStrict = Str::firstElementOfChain($chain, '=='); // Output: 'path'
$strict = Str::firstElementOfChain($chain, '==', true); // Output: null

// Example 6
$chain = "path====to====file";
$nonStrict = Str::firstElementOfChain($chain, '=='); // Output: 'path'
$strict = Str::firstElementOfChain($chain, '==', true); // Output: 'path'

// Example 7
$chain = "===path==to==file";
$nonStrict = Str::firstElementOfChain($chain, '=='); // Output: '=path'

$chain = 'path.to.element';
$element = Str::lastElementOfChain($chain); // Output: 'element'

$chain = 'path/to/element';
$element = Str::lastElementOfChain($chain, '/'); // Output: 'element'

$chain = 'path/to/element/';
$element = Str::lastElementOfChain($chain, '/'); // Output: 'element'

$chain = 'path/to/element/';
$element = Str::lastElementOfChain($chain, '/', true); // Output: null

$chain = 'path/to/element////';
$element = Str::lastElementOfChain($chain, '/'); // Output: 'element'

# Example 1
$chain = '==path==to==any==object===';
$element = Str::lastElementOfChain($chain, '==', true); // Output: null

# Example 2
$chain = '==path==to==any==object===';
$element = Str::lastElementOfChain($chain, '=='); // Output: 'object='

// $this->mergeConfigFrom(__DIR__.'/../config/page.php', 'page'); // default in Laravel
$this->recurciveReplaceConfigFrom(__DIR__.'/../config/page.php', 'page');

$source = [
  'one' => [
      'one_one' => 1,
      'one_two' => 2,
      'one_three' => 3,
  ],
  'two' => [
      'two_one' => 1,
      'two_two' => 2,
      'two_three' => 3,
  ],
];

$newConfig = [
    'one' => [
        'one_one' => 10,
        'one_two' => 20,
        'one_three' => 30,
    ],
    'two' => [
        'two_two' => 20,
        'two_four' => 40,
    ],
    'three' => [
        'three_one' => 30,
    ],
];

// The result of merge by Laravel
// [
//     'one' => [
//         'one_one' => 10,
//         'one_two' => 20,
//         'one_three' => 30,
//     ],
//     'two' => [
//         // the old values are not left
//         'two_two' => 20,
//         'two_four' => 40,
//     ],
//     'three' => [
//         'three_one' => 30,
//     ],
// ]

// The result of merge by the new function
// [
//     'one' => [
//         'one_one' => 10,
//         'one_two' => 20,
//         'one_three' => 30,
//     ],
//     'two' => [
//         'two_one' => 1, // it is left
//         'two_two' => 20,
//         'two_three' => 3, // it is left
//         'two_four' => 40,
//     ],
//     'three' => [
//         'three_one' => 30,
//     ],
// ]