Skip to content

Instantly share code, notes, and snippets.

@felipevolpatto
felipevolpatto / StringExtensions.cs
Last active December 17, 2015 03:29
"Title case" helper.
public static class StringExtensions
{
public static string ToTitleCase(this string value)
{
return System.Globalization.CultureInfo.InvariantCulture.TextInfo.ToTitleCase(
value.ToLowerInvariant());
}
}
@felipevolpatto
felipevolpatto / ArrayToXmlHelper.php
Last active December 28, 2015 19:29
Array to XML helper.
<?php
class ArrayHelper {
const PARENT_NODE = "<return></return>";
private function __construct() { }
public static function arrayToXml($array, &$xml) {
foreach($array as $key => $value) {
if(is_array($value)) {
@felipevolpatto
felipevolpatto / ObjectHelper.php
Created November 19, 2013 19:11
PHP Object Helper. toArray() -> Object to array implementation.
<?php
class ObjectHelper {
private function __construct() { }
public static function toArray($obj) {
if (is_object($obj)) {
$obj = get_object_vars($obj);
}
@felipevolpatto
felipevolpatto / IPAddressHelper.php
Last active December 30, 2015 00:49
Shorter and cleaner way to get the IP address
<?php
function getIpAddress() {
foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key) {
if (array_key_exists($key, $_SERVER) === true) {
foreach (explode(',', $_SERVER[$key]) as $ip) {
$ip = trim($ip);
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false) {
return $ip;
@felipevolpatto
felipevolpatto / RequestParser.php
Last active August 29, 2015 14:01
Manually parse raw HTTP data with PHP
<?php
function parseRawHttpRequest(array &$arrayData) {
$input = file_get_contents('php://input');
preg_match('/boundary=(.*)$/', $_SERVER['CONTENT_TYPE'], $matches);
if (!count($matches)) {
// parse_str(urldecode($input), $arrayData);
parse_str($input, $arrayData);
@felipevolpatto
felipevolpatto / StatusCodes.php
Last active August 29, 2015 14:01
Status codes consts
<?php
final class StatusCodes {
const HTTP_100 = 'HTTP/1.1 100 Continue';
const HTTP_101 = 'HTTP/1.1 101 Switching Protocols';
const HTTP_200 = 'HTTP/1.1 200 OK';
const HTTP_201 = 'HTTP/1.1 201 Created';
const HTTP_202 = 'HTTP/1.1 202 Accepted';
const HTTP_203 = 'HTTP/1.1 203 Non-Authoritative Information';
const HTTP_204 = 'HTTP/1.1 204 No Content';
@felipevolpatto
felipevolpatto / DebugHelper.php
Last active August 29, 2015 14:02
Simples class to output PHP data to browser console
<?php
final class DebugHelper {
/**
* @param object $data
* @param string $name
* @param boolean $jsEval
* @return string
*/
public static function logConsole($data, $name = null, $jsEval = false) {
@felipevolpatto
felipevolpatto / ArrayCollection.php
Last active August 29, 2015 14:02
A class to work with the idea of collections in PHP
<?php
namespace Collections;
class ArrayCollection {
private $elements;
public function __construct(array $_elements = array()) {
$this->elements = $_elements;
}
@felipevolpatto
felipevolpatto / app.js
Created September 9, 2015 14:40
Positioning an element at the center of the actual screen
jQuery.fn.center = function () {
this.css("position","fixed");
this.css("top", ($(window).height() / 2) - (this.outerHeight() / 2));
this.css("left", ($(window).width() / 2) - (this.outerWidth() / 2));
return this;
}
@felipevolpatto
felipevolpatto / git-essentials.md
Last active July 11, 2017 00:28
Git essentials

Remember

Remove local branch
git branch -d (the_local_branch)

Diff files on stating