Skip to content

Instantly share code, notes, and snippets.

View pavarov's full-sized avatar

Pavel Pivovarov pavarov

View GitHub Profile
@pavarov
pavarov / index.php
Created April 19, 2023 12:02 — forked from insideone/index.php
PHP date: вывод даты с русским названием месяца и возможностью склонения
<?php
function rdate($format, $timestamp = null, $case = 0)
{
if ($timestamp === null)
$timestamp = time();
static $loc =
'Январ,ь,я,е,ю,ём,е
Феврал,ь,я,е,ю,ём,е
@pavarov
pavarov / postgres-backup.sh
Created October 31, 2022 06:15 — forked from 4410287/postgres-backup.sh
Shell script for daily postgres database backup with basic archiving.
#!/bin/bash
# Written 2018-11-15 by 4410287
# This script will create a backup file of a postgres database and compress it. It is capable of access a local or remote server to pull the backup. After creating a new backup, it will delete backups that are older than 15 days, with the exception of backups created the first of every month. It is recommended to create a seperate database user specifically for backup purposes, and to set the permissions of this script to prevent access to the login details. Backup scripts for different databases should be run in seperate folders or they will overwrite each other.
HOSTNAME=
USERNAME=
PASSWORD=
DATABASE=
@pavarov
pavarov / JsonPrettyUnicodePrintFormatter.php
Created October 26, 2022 09:21
Extended json fromatter for monolog
<?php
declare(strict_types=1);
namespace App\Settings\Logger;
use Monolog\Formatter\JsonFormatter;
use Monolog\LogRecord;
class JsonPrettyUnicodePrintFormatter extends JsonFormatter
{
@pavarov
pavarov / sort array
Last active July 29, 2022 13:40
ciryllic sort array
<?php
uasort($employees, function($a, $b) {
$a = mb_strtolower($a['name'], 'utf-8');
$b = mb_strtolower($b['name'], 'utf-8');
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
@pavarov
pavarov / SplClassLoader.php
Created June 23, 2022 05:15 — forked from jwage/SplClassLoader.php
Add MIT license.
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
@pavarov
pavarov / errors_handler.php
Created June 22, 2022 05:26
handlers for uncaught errors and exceptions
<?php
declare(strict_types=1);
use App\Response;
set_exception_handler(static function (Throwable $ex) {
$msg = json_encode([
'error message' => $ex->getMessage(),
'error file' => str_replace($_SERVER['DOCUMENT_ROOT'], '', str_replace('\\', '/', $ex->getFile())),
'error line' => $ex->getLine(),
@pavarov
pavarov / sleep.js
Created June 1, 2022 07:48
js sleep function
function sleep(ms: number) {
return new Promise(resolve => window.setTimeout(resolve, ms));
}
<?php
public function getGUID(): string
{
if (function_exists('com_create_guid') === true) {
return trim(com_create_guid(), '{}');
}
return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
}
@pavarov
pavarov / FileInfo.php
Created September 1, 2021 12:59
Wrapper over SplFileInfo. added getMimeContentType, getMimeType methods. Notice! throws a FIleNotFoundException (you should have this class).
<?php
namespace app\helpers;
use app\exceptions\FileNotFoundException;
use SplFileInfo;
/**
* Class FileInfo
*
@pavarov
pavarov / CancelSoftDelete.php
Created April 13, 2021 13:25
Laravel. Cancel soft delete trait
<?php
declare(strict_types=1);
namespace App\Traits;
trait CancelSoftDelete
{
protected $forceDeleting = true;
public static function bootSoftDeletes(): void