Skip to content

Instantly share code, notes, and snippets.

View simonhamp's full-sized avatar
💭
Accidentally learning Rust

Simon Hamp simonhamp

💭
Accidentally learning Rust
View GitHub Profile
@simonhamp
simonhamp / AppServiceProvider.php
Last active March 26, 2024 15:56
A pageable Collection implementation for Laravel
<?php
namespace App\Providers;
use Illuminate\Support\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
class AppServiceProvider extends ServiceProvider
{
public function boot()
@simonhamp
simonhamp / MigrateUsers.php
Last active February 1, 2024 10:16
Migrate Statamic v3 file-based users to a database
<?php
/**
* Statamic v3 User Migrator
* @author Simon Hamp <simon.hamp@me.com>
* @copyright Copyright (c) 2021, Simon Hamp
* @license MIT
*/
namespace App\Console\Commands;
use Exception;
@simonhamp
simonhamp / Reroutable.php
Created December 5, 2023 14:35
A trait to reroute Laravel Notifications at runtime
<?php
namespace App\Concerns\Notifications;
trait Reroutable
{
protected array $routes;
public function routes(string ...$routes): static
{
@simonhamp
simonhamp / WebhookController.php
Last active November 17, 2023 17:13
Handling Typeform Webhooks in Laravel
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class WebhookController
{
public function create(Request $request)
{
@simonhamp
simonhamp / AppServiceProvider.php
Last active February 6, 2023 03:19
Laravel: Str::csvToArray macro
<?php
namespace App\Providers;
use Illuminate\Support\Str;
class AppServiceProvider extends ServiceProvider
{
public function boot()
@simonhamp
simonhamp / VersionMatcher.php
Created January 18, 2018 12:16
A simple SemVer parser/matcher
<?php
class VersionMatcher
{
protected $specificity = 'major';
protected $originals = [];
protected $current;
protected $target;
protected static $padDigits;
@simonhamp
simonhamp / TreeSplit.php
Created May 26, 2022 02:06
Split a Statamic Tree into two collections
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Statamic\Facades\Collection;
use Statamic\Eloquent\Entries\UuidEntryModel as EntryModel;
use Statamic\Facades\Entry;
use Statamic\Structures\CollectionTree;
<?php
/**
* Return the common parts from 2 strings.
*
* @param string $str1
* @param string $str2
* @param bool $from_end
* return string
*/
@simonhamp
simonhamp / list.php
Last active June 15, 2021 21:23
Using list() in foreach in PHP
<?php
$items = [
['field' => 'field1', 'values' => [0, 1, 2, 3, 4]],
['field' => 'field2', 'values' => ['cat', 'dog', 'horse']]
];
foreach ($items as list('field' => $field, 'values' => $values)) {
// can use $field and $values here now!
}
@simonhamp
simonhamp / RemoteArtisan.php
Last active February 10, 2021 10:41
RemoteArtisan: A way to call another Laravel/Lumen application's artisan command from the context of the current application.
<?php
namespace App;
use Dotenv\Dotenv;
use Illuminate\Support\Str;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
class RemoteArtisan