Skip to content

Instantly share code, notes, and snippets.

Avatar

Caleb Porzio calebporzio

View GitHub Profile
View refactor_1.php
<?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() {
View refactor_1_solution.php
<?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 Apr 8, 2018
Throw an error in a testing environment when trans('some-key') doesn't exist
View strict_localization_lookup.php
<?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 Apr 8, 2018
Simple helper macro for a common Laravel controller pattern (redirecting back with status message)
View redirect_message_macro.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
View auth_macro_hack.php
<?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 Apr 27, 2017
Simple Laravel Dusk macro that waits for a page change
View wait_for_reload.php
<?php
// Define in some ServiceProvider
Browser::macro('waitForReload', function () {
$this->script("window.duskPageIsStale = {}");
return $this->waitUntil("return typeof window.duskPageIsStale === 'undefined';");
});
// Usage:
View tinker.config.php
<?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
A modified Laravel composer.json for including a local "laravel/framework" repostory
View modified-composer.json
{
"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 Jun 1, 2017
A handy accessor and mutator for storing amounts in cents and accessing and setting them in dollars
View amount_in_dollars.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class [Insert Name Here] extends Model
{
protected $guarded = [];
@calebporzio
calebporzio / composer_versions_cheatsheet.md
Last active Sep 28, 2021
Composer version symbol cheatsheet
View composer_versions_cheatsheet.md
...
"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