Skip to content

Instantly share code, notes, and snippets.

View m8rge's full-sized avatar
👨‍💻
Code is for developers first

Andrey m8rge

👨‍💻
Code is for developers first
View GitHub Profile
@m8rge
m8rge / TruncateString.php
Last active November 3, 2016 06:51
Truncate string on word break
<?php
/**
* @param string $string Subject
* @param int $length Max string length
* @param bool $exactLength Truncate string with exact $length
* @param string $append Ellipsis string
* @return string
*/
class TruncateString
@m8rge
m8rge / InMemoryCache.php
Last active February 3, 2016 07:36
Simple limited memory cache
<?php
class InMemoryCache
{
public $size = 10;
protected $cache = [];
/**
* @param string $key
@m8rge
m8rge / AsteriskMatch.php
Last active January 29, 2016 08:16
Test string against simple mask with asterisk *
<?php
class AsteriskMatch
{
/**
* @param string $mask string with asterisk
* @param string $string tested subject
* @return bool
* @throws \Exception
*/
@m8rge
m8rge / CronException.php
Last active October 18, 2018 04:14
Yii2 console controller behavior. Prevents double run console command
<?php
namespace console\components;
use Throwable;
use yii\base\Action;
class CronException extends \RuntimeException
{
/**
@m8rge
m8rge / DbTestCase.php
Created July 2, 2015 03:25
Yii2 db fixtures from array
<?php
namespace tests\codeception\common\unit;
use Codeception\Specify;
use yii\di\Instance;
use yii\test\InitDbFixture;
/**
* usage:
<?php
namespace console\components;
class XMLReader extends \XMLReader
{
/**
* @param string $nodeName
* @return $this
@m8rge
m8rge / HashBuilder.php
Last active August 29, 2015 14:21 — forked from mncaudill/similar.php
Image similarity algorithm
<?php
# Algorithm found here: http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html
class HashBuilder
{
private $buf;
function __construct()
{
$this->buf = imagecreatetruecolor(8, 8);
}
@m8rge
m8rge / WithoutTrait.php
Last active June 8, 2017 04:40
Without yii2 activerecord trait
<?php
namespace common\ActiveRecordTrait;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
trait WithoutTrait
{
@m8rge
m8rge / FindOrCreate.php
Last active June 26, 2018 04:37
FindOrCreate yii2 activerecord trait
<?php
namespace common\traits;
trait FindOrCreate
{
/**
* @param mixed $key Primary key or array with condition for \yii\db\Query::where(condition)
* @return static
* @throws \Exception
@m8rge
m8rge / CantSave.php
Last active May 19, 2018 18:13
Yii2 exception for non-saved active records
<?php
namespace common\exception;
use e96\sentry\ErrorHandler;
use yii\base\Exception;
use yii\db\ActiveRecord;
class CantSave extends Exception