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 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
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 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 / 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
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 / 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
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 August 27, 2018 14:47
array to xml
<?php
function isAssoc(array $arr)
{
if (array() === $arr) return false;
return array_keys($arr) !== range(0, count($arr) - 1);
}
function toXml($name, $a) {
$sReturn = "<$name>";
@kobus1998
kobus1998 / MyService.php
Created August 14, 2018 15:08
SImple service runner including adapter
<?php
class MyService implements ServiceInterface
{
public function boot() {
echo "booting..\n";
}
public function run() {
echo "running...\n";
@kobus1998
kobus1998 / index.php
Created August 13, 2018 08:50
PHP Reflection get type hint
<?php
class X {
public function get(array $arr) {
return $arr;
}
}
$reflect = new ReflectionMethod('X', 'get');
// first param for sake of example