Skip to content

Instantly share code, notes, and snippets.

View gskema's full-sized avatar

Gytis Šk. gskema

  • Kaunas, Lithuania
View GitHub Profile
@gskema
gskema / BaseTestCase.php
Last active April 4, 2017 13:32
PHP entity object factory method for tests. PHPUnit/Symfony
<?php
class BaseTestCase extends \PHPUnit_Framework_TestCase
{
/**
* Creates an arbitrary object with specified property values.
*
* @param string $className
* @param array $propertyValues
*
@gskema
gskema / random_alphanumeric_string.php
Last active March 10, 2017 10:21
PHP random alphanumeric string generation
<?php
class RandomStringGenerator
{
const DEFAULT_ALPHABET = 'abcdefghijklmnopqrstuvwxyz0123456789';
/**
* Generates a random string of requested length and from given alphabet.
*
* @param int $stringLength
* @param string $alphabet
@gskema
gskema / escape_date_math_index.php
Last active January 10, 2017 15:08
ElasticSearch escape date math index names in request path
<?php
// @see https://www.elastic.co/guide/en/elasticsearch/reference/5.x/date-math-index-names.html
// @TODO <elastic\\{ON\\}-{now/M}>
function escapePathWithDateMath($path)
{
$symbols = ['<', '>', '/', '{', '}', '|', '+', ':', ','];
$escaped = ['%3C', '%3E', '%2F', '%7B', '%7D', '%7C', '%2B', '%3A', '%2C'];
// Check if the string is already escaped
@gskema
gskema / html_trim_whitespace.php
Last active August 4, 2018 06:44
HTML whitespace trim RegEx (regular expression) for PHP. Useful for trimming HTML content before TCPDF processing.
<?php
// Trims whitespaces after any tags. Assumes that a tag ends with '/>', '\w>', '">'.
$html = preg_replace('#(["|\/|\w]>)(\s+)#', '$1', $html);
// Trims whitespaces before any tags. Assumes that tags start with '<\w+'
$html = preg_replace('#(\s+)(<\/?\w+)#', '$2', $html);
// Trims spaces between tags (both opening and closing).
// Assumes that a tag ends with '/>', '\w>', '">' and starts with '<\w+'
@gskema
gskema / float.js
Created October 24, 2016 12:55
Correctly rounds JavaScript number (float) to 2 decimals.
function roundFloat(number, decimals) {
return (Math.round(number * (Math.pow(10, decimals))) / Math.pow(10, decimals)).toFixed(decimals)
}
@gskema
gskema / letters.php
Last active October 19, 2016 11:07
Expresses a given decimal number in an arbitrary unique letter base. PHP
<?php
/**
* Expresses a given number in a a-z letter base, e.g.:
* 0 -> a, 27 -> ba, 932 -> bjw, ...
*
* @param int $number
*
* @return string
*/
@gskema
gskema / line_count.php
Created October 17, 2016 07:42
Counts the number of lines in a container where character count per line is known or approximate.
<?php
/**
* Counts the number of lines in a container where character count per line is known or approximate.
* It assumes that words can be broken.
*
* @param string $text
* @param int $perLine
*
* @return int
@gskema
gskema / script.js
Created October 16, 2016 12:58
Cached jQuery .find and .closest methods. On repeated invokation, returns jQuery objects / DOM references from cache.
// jQuery warriors, assemble!
$.fn.cachedFind = function (selector) {
var cache = this.data('cached-find') || {};
if (undefined === cache[selector]) {
cache[selector] = this.find(selector);
this.data('cached-find', cache);
}
return cache[selector];
};
@gskema
gskema / lpad.js
Created August 31, 2016 18:09
Left pad JavaScript string
export function lpad(pad, string) {
var trueString = '' + string;
return pad.substring(0, pad.length - trueString.length) + trueString;
}
@gskema
gskema / NotificationRepository.php
Last active August 5, 2016 12:39
[SQL, PHP]: Fetch rows from two column ordered lists by specifying IDs
<?php
/**
* Returns customer notifications
*
* @param int|null $limit
* @param int|null $afterId
* @param int|null $beforeId
*
* [0]*************************************[totalRows]