Skip to content

Instantly share code, notes, and snippets.

View calebporzio's full-sized avatar

Caleb Porzio calebporzio

View GitHub Profile
<?php
// Refactor the following functions using your new skills from 99-bottles.
// Sorry about the lack of tests - bonus points if you write tests to get started.
function getStartDate() {
return preg_match("/^\d\d\d\d-\d\d-\d\d$/", $_GET['dStartDate']) ? $_GET['dStartDate'] : date("Y-m-d", time() - 60 * 60 * 24 * 7);
}
function getEndDate() {
<?php
// Before:
function getStartDate() {
return preg_match("/^\d\d\d\d-\d\d-\d\d$/", $_GET['dStartDate']) ? $_GET['dStartDate'] : date("Y-m-d", time() - 60 * 60 * 24 * 7);
}
function getEndDate() {
return preg_match("/^\d\d\d\d-\d\d-\d\d$/", $_GET['dEndDate']) ? $_GET['dEndDate'] : date("Y-m-d");
@calebporzio
calebporzio / strict_localization_lookup.php
Last active April 8, 2018 02:28
Throw an error in a testing environment when trans('some-key') doesn't exist
<?php
namespace Tests;
use Illuminate\Translation\Translator;
use Laravel\BrowserKitTesting\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
@calebporzio
calebporzio / redirect_message_macro.php
Last active April 8, 2018 02:28
Simple helper macro for a common Laravel controller pattern (redirecting back with status message)
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
<?php
// Until SessionGuard Macros get pulled into Laravel core from a PR:
// Remove this when Laravel 5.4.18 is released.
$this->app->config->set('auth.guards.hack', [
'driver' => 'modified-session',
'provider' => 'users',
]);
@calebporzio
calebporzio / wait_for_reload.php
Created April 27, 2017 16:45
Simple Laravel Dusk macro that waits for a page change
<?php
// Define in some ServiceProvider
Browser::macro('waitForReload', function () {
$this->script("window.duskPageIsStale = {}");
return $this->waitUntil("return typeof window.duskPageIsStale === 'undefined';");
});
// Usage:
<?php
/**
* Automatically alias Laravel Model's to their base classname.
* Ex: "App\Models\User" now can just be accessed by "User"
*/
if (! function_exists('aliasModels')) {
function aliasModels() {
$finder = new \Symfony\Component\Finder\Finder();
$finder->files()->name('*.php')->in(base_path().'/app');
@calebporzio
calebporzio / modified-composer.json
Created May 22, 2017 13:36
A modified Laravel composer.json for including a local "laravel/framework" repostory
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"php": ">=5.6.4",
"laravel/framework": "5.4.x-dev",
"laravel/tinker": "~1.0"
@calebporzio
calebporzio / amount_in_dollars.php
Created June 1, 2017 19:07
A handy accessor and mutator for storing amounts in cents and accessing and setting them in dollars
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class [Insert Name Here] extends Model
{
protected $guarded = [];
@calebporzio
calebporzio / composer_versions_cheatsheet.md
Last active June 6, 2023 17:40
Composer version symbol cheatsheet
...
"require": {
    "vendor/package": "1.3.2", // exactly 1.3.2 (exact)

    // >, <, >=, <= | specify upper / lower bounds
    "vendor/package": ">=1.3.2", // anything above or equal to 1.3.2
    "vendor/package": "<1.3.2", // anything below 1.3.2

 // * | wildcard