Skip to content

Instantly share code, notes, and snippets.

View kobus1998's full-sized avatar
😳
-10x programmer

Kobus kobus1998

😳
-10x programmer
View GitHub Profile
@kobus1998
kobus1998 / checkout.php
Created December 11, 2019 15:39
abstraction shopping cart checkout
<?php
class Product
{
protected $id;
protected $name;
public function __construct($id, $name)
{
$this->id = $id;
<?php
interface PluginServiceProvider
{
public function register(Plugin $plugin): void;
}
class Plugin
{
protected $methods = [];
@kobus1998
kobus1998 / Entity.php
Created November 25, 2019 15:56
entity
<?php
abstract class Entity
{
abstract public function config(): array;
public function validate()
{
$config = $this->config();
$errors = [];
@kobus1998
kobus1998 / index.php
Created November 11, 2019 12:39
function in parameter with already set parameters
<?php
$i = 1;
function plus1(int $i) {
return $i + 1;
}
$f = function () use ($i) {
echo plus1($i);
@kobus1998
kobus1998 / ComplexPeriod.php
Created October 24, 2019 07:49
added functionality to period
<?php
class ComplexPeriod extends DatePeriod
{
public function __construct($start, $end)
{
parent::__construct(
new \DateTime(date("Y-m-d", strtotime($start))),
new \DateInterval('P1D'),
new \DateTime(date("Y-m-d", strtotime($end)))
@kobus1998
kobus1998 / parseCsv.php
Created October 23, 2019 12:56
simple no abstraction csv parser
<?php
function parseCsv($csv, $delimiter = ',')
{
// parse rows
$rows = explode("\n", trim($csv));
// parse header and header columns
$header = explode($delimiter, trim(reset($rows)));
@kobus1998
kobus1998 / slugify.php
Created October 22, 2019 13:47
slugify for url, id, var etc
<?php
function slugify($sVal, $sDelimiter = '-')
{
$sVal = preg_replace("/[^a-zA-Z0-9_-]/", $sDelimiter, $sVal);
$sVal = preg_replace("/$sDelimiter{2,}/", $sDelimiter, $sVal);
return trim($sVal, $sDelimiter);
}
@kobus1998
kobus1998 / simpleCalendar.php
Created October 16, 2019 13:49
simple calendar
<?php
// start date current month
$day = 01;
$month = date('m');
$year = date('Y');
$today = date('d-m-Y');
$period = new DatePeriod(
@kobus1998
kobus1998 / index.php
Created October 2, 2019 08:40
regex matching
<?php
class Match
{
public function __construct($pattern, $data, $match)
{
$this->pattern = $pattern;
$this->data = $data;
$this->match = $match;
}
@kobus1998
kobus1998 / index.php
Created September 16, 2019 09:25
Manage user timezones
<?php
$timezones = [];
foreach(DateTimeZone::listIdentifiers() as $timezone)
{
$timezoneWithContinent = explode('/', $timezone);
if (count($timezoneWithContinent) == 1) {
// skip UTC
continue;