Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View coreymcmahon's full-sized avatar

Corey McMahon coreymcmahon

View GitHub Profile
<?php namespace Acme\Repositories;
/**
* RepositoryInterface provides the standard functions to be expected of ANY
* repository.
*/
interface RepositoryInterface {
public function create(array $attributes);
<?php namespace Acme\Repositories;
use Acme\Abstracts\Repository as AbstractRepository;
class UserRepository extends AbstractRepository implements UserRepositoryInterface
{
// This is where the "magic" comes from:
protected $modelClassName = 'User';
// This class only implements methods specific to the UserRepository
@coreymcmahon
coreymcmahon / example.php
Created February 8, 2014 08:51
A Pattern for Reusable Repository Design in Laravel - http://www.slashnode.com/reusable-repository-design-in-laravel/
<?php
$repo = new Acme\Repositories\UserRepository;
// echo the name of the user with id 1
echo $repo->find(1)->name;
// echo the number of users with username = johnsmith
echo count($repo->findByUsername('johnsmith'));
@coreymcmahon
coreymcmahon / example.php
Created February 20, 2014 08:09
Use a repository to update an object
<?php
// resolve the dependency
$repo = App::make('Acme\Repositories\ObjectRepository');
// update the entity
$repo->update($object->id, array(
'attribute' => 'new value',
));
<?php
// etc...
// find the payment if it already exists
$payment = Payment::where('txn_id', '=', Input::get('txn_id'))->get()->first();
// if it doesn't exist, create the payment using all the fields in the POST
if (empty($payment)) $payment = Payment::create(Input::all());
// if in debug mode, add the raw body of the request and also save it
@coreymcmahon
coreymcmahon / DateTimeExample.php
Created March 7, 2014 07:03
DateTime example
<?php
/* 00:00am (midnight) in Vietnam */
$datetimeOne = new DateTime('2014-03-07 00:00:00+07:00');
/* 04:00am in Australia */
$datetimeTwo = new DateTime('2014-03-07 04:00:00+11:00');
echo ($datetimeOne == $datetimeTwo) ? 'equal' : 'not equal';
// prints 'equal'
<?php
/**
* function to check whether a date is within the last 30 days
*/
function withinOneMonth ($checkDate) {
$now = new DateTime();
$thirtyDayInterval = new DateInterval('P30D');
@coreymcmahon
coreymcmahon / Response.php
Created March 25, 2014 02:36
Custom Response facade for additional response types in Laravel 4
<?php namespace Acme\Extensions\Facades;
class Response extends \Illuminate\Support\Facades\Response
{
public static function csv($data, $filename = 'data.csv', $status = 200, $delimiter = "|", $linebreak = "\n", $headers = array())
{
return static::stream(function () use ($data, $delimiter, $linebreak) {
foreach ($data as $row) {
$keys = array(); $values = array();
@coreymcmahon
coreymcmahon / responses.php
Created March 25, 2014 02:46
Examples of different response types that can be sent back using the Response facade in Laravel 4 - www.slashnode.com
<?php
// send back some HTML, rendered from a template
return Response::view('users.index', array(
'name' => 'Steve Brule',
'title' => 'Brule\'s Rules',
));
// send back some json
return Response::json(array(
@coreymcmahon
coreymcmahon / app.php
Created March 25, 2014 02:55
Registering a custom Response facade - www.slashnode.com
<?php
// ...etc
/* comment out the standard response registration */
//'Response' => 'Illuminate\Support\Facades\Response',
/* add our new one...*/
'Response' => 'Acme\Extensions\Facades\Response',