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 / index.php
Created September 3, 2018 17:48
Model to string
<?php
class Model
{
public function __toString() {
return json_encode(get_object_vars($this));
}
}
$model = new Model();
@kobus1998
kobus1998 / index.php
Created October 16, 2018 12:41
Strip XML namespaces
<?php
// namespaces can be a pain in the ass, well NO LONGER!
function stripNamespaces($xml)
{
return preg_replace([
"/(\<(\w*|\-)*\:)/", // start node namespaces
"/(\<(\/{0,1})(\w*|\-)*\:)/", // end node namespaces
"/(\s\w*\:)/" // attribute namespaces
], [
@kobus1998
kobus1998 / index.php
Last active January 5, 2021 15:15
Export trait
<?php
trait export
{
public static function getHeader()
{
$sCsv = '';
foreach(get_class_vars(self::class) as $key => $value) {
$sCsv .= $key . ',';
}
@kobus1998
kobus1998 / Generator.php
Created December 13, 2018 11:48
Xml generator
<?php
namespace Util\Xml;
class Generator
{
/**
* @param string $name
* @param string|array $inner (null)
* @param array $attrs (array)
@kobus1998
kobus1998 / index.php
Created January 16, 2019 13:55
Create xml
<?php
function addXml($name, $input = null, $attrs = [])
{
$xml = "";
$xml .= "<{$name}";
if (!empty($attrs)) {
foreach($attrs as $key => $value) {
$xml .= " {$key}=\"{$value}\"";
}
@kobus1998
kobus1998 / index.php
Last active January 16, 2019 16:11
Force https PHP
<?php
/**
* Force to redirect to https
*
* @return void
*/
function forceHttps()
{
// check already https
@kobus1998
kobus1998 / index.php
Created January 17, 2019 10:49
Force foreach loop
<?php
/**
* @param array
* @return bool
*/
function isAssoc($array)
{
if (!is_array($array)) {
return false;
@kobus1998
kobus1998 / index.php
Created January 17, 2019 12:07
Implode assoc
<?php
/**
* @param string|array
* @param string|array
* @param string
*/
function implodeAssoc($glue, $arr, $key = null)
{
if (is_array($l)) {
@kobus1998
kobus1998 / NonStaticTest.php
Created January 28, 2019 16:07
Performance test static vs non-static methods
<?php
class NonStaticTest
{
public function execute()
{
array_fill(0, 100, 'a');
}
}
@kobus1998
kobus1998 / CustomFunctionality.php
Created February 25, 2019 11:59
Trait that catches custom methods that are not in the class
<?php
trait CustomFunctionality
{
protected $functions = [];
public function addMethod(string $name, callable $function)
{
$this->functions[$name] = $function;
return $this;