Skip to content

Instantly share code, notes, and snippets.

View YaroslavB's full-sized avatar
🏠
Working from home

YaroslavB YaroslavB

🏠
Working from home
  • Kiev/Ukraine
View GitHub Profile
@YaroslavB
YaroslavB / Sorter.php
Created June 17, 2024 15:23 — forked from oplanre/Sorter.php
Light abstraction layer for sorting arrays in php
<?php
class Sorter {
public function __construct(private array $array) {}
/**
* Sort the array by a specific key in ascending or descending order.
*
* @param string $key The key to sort by.
* @param string $order The order direction, 'asc' for ascending or 'desc' for descending. Default is 'asc'.
* @return self
@YaroslavB
YaroslavB / rector.php
Created June 13, 2024 13:14 — forked from BusterNeece/rector.php
Rector script to migrate OpenAPI specifications from PHP Annotations to Attributes
<?php
use Rector\Php80\Rector\Class_\AnnotationToAttributeRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$openApiAnnotationsRaw = [
'AdditionalProperties',
@YaroslavB
YaroslavB / Array Objects in PHP.md
Created May 27, 2024 05:43 — forked from Jeff-Russ/Array Objects in PHP.md
PHP: ArrayObject, IteratorAggregate , ArrayAccess , Serializable , Countable

Array Objects in PHP

Array are not object in PHP but PHP does give us some ways to make object that act like arrays. First, there is the ArrayObject class which is pretty close to what you have with a normal array (you can iterate it, use [] etc.) If you use it as a parent for your class you can add methods to it.

class ArrObj extends ArrayObject{
	// add methods here
@YaroslavB
YaroslavB / bot.php
Created March 27, 2023 10:21
check if user agent is bot in PHP
<?php
function isBot() {
if(isset($_SERVER['HTTP_USER_AGENT'])){
if ( preg_match('/abacho|accona|AddThis|AdsBot|ahoy|AhrefsBot|AISearchBot|alexa|altavista|anthill|appie|applebot|arale|araneo|AraybOt|ariadne|arks|aspseek|ATN_Worldwide|Atomz|baiduspider|baidu|bbot|bingbot|bing|Bjaaland|BlackWidow|BotLink|bot|boxseabot|bspider|calif|CCBot|ChinaClaw|christcrawler|CMC\/0\.01|combine|confuzzledbot|contaxe|CoolBot|cosmos|crawler|crawlpaper|crawl|curl|cusco|cyberspyder|cydralspider|dataprovider|digger|DIIbot|DotBot|downloadexpress|DragonBot|DuckDuckBot|dwcp|EasouSpider|ebiness|ecollector|elfinbot|esculapio|ESI|esther|eStyle|Ezooms|facebookexternalhit|facebook|facebot|fastcrawler|FatBot|FDSE|FELIX IDE|fetch|fido|find|Firefly|fouineur|Freecrawl|froogle|gammaSpider|gazz|gcreep|geona|Getterrobo-Plus|get|girafabot|golem|googlebot|\-google|grabber|GrabNet|griffon|Gromit|gulliver|gulper|hambot|havIndex|hotwired|htdig|HTTrack|ia_archiver|iajabot|IDBot|Informant|InfoSeek|InfoSpiders|INGRID\/0\.1|inktomi|inspectorw
@YaroslavB
YaroslavB / readTime.php
Created March 27, 2023 10:18
Estimated read time in PHP
<?php
function readTime($content){
$words = str_word_count_utf8(strip_tags($content));
$minutes = floor($words / 200);
$seconds = floor($words % 200 / (200 / 60));
return $minutes . ' minute' . ($minutes == 1 ? '' : 's') . ', ' . $seconds . ' second' . ($seconds == 1 ? '' : 's');
}
function str_word_count_utf8($str) {
return count(preg_split('~[^\p{L}\p{N}\']+~u',$str));
}
@YaroslavB
YaroslavB / Instructions.sh
Created September 26, 2022 06:50 — forked from GhazanfarMir/Instructions.sh
Install PHP7.2 NGINX and PHP7.2-FPM on Ubuntu 16.04
########## Install NGINX ##############
# Install software-properties-common package to give us add-apt-repository package
sudo apt-get install -y software-properties-common
# Install latest nginx version from community maintained ppa
sudo add-apt-repository ppa:nginx/stable
# Update packages after adding ppa
@YaroslavB
YaroslavB / MyRepository.php
Created August 15, 2022 08:52 — forked from bulton-fr/MyRepository.php
Doctrine yield
<?php
namespace MyBundle\Repository;
use Doctrine\ORM\EntityRepository;
use MyBundle\Doctrine\RepositoryTrait;
class MyRepository extends EntityRepository
{
use RepositoryTrait;
@YaroslavB
YaroslavB / hellobot.php
Created December 7, 2021 11:35 — forked from ingria/hellobot.php
Simple PHP Telegram Bot
<?php
// @see https://core.telegram.org/bots/samples/hellobot
define('BOT_TOKEN', '12345678:replace-me-with-real-token');
define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');
function apiRequestWebhook($method, $parameters) {
if (!is_string($method)) {
error_log("Method name must be a string\n");
return false;
<?php
ini_set('display_errors', 'On');
require 'vendor/autoload.php';
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\CssSelector\CssSelectorConverter;
$client = new GuzzleHttp\Client();
$url = 'https://us.fotolia.com/search?k=cats&filters%5Bcontent_type%3Aphoto%5D=1&search-submit=Search&limit=5';
$data = $client->get($url);
@YaroslavB
YaroslavB / ArraySeparator.php
Last active July 8, 2021 12:46
This function is for divide/group/chunk an multidimensional array based on same key.
<?php
function arrayChunkByKeyValue($data, $groupByKey)
{
$groupArray = [];
foreach ($data as $singleData) {
$groupArray[$singleData[$groupByKey]][] = $singleData;
}
return $groupArray;