Skip to content

Instantly share code, notes, and snippets.

<?php
$query = RelationShipQuery::new(
'select * from `posts` where id = :id',
function (array $record) {
return new Post($record['id'], $record['title'], $record['body'], $record['author'], $record['comments']);
},
[
'author' => PostBelongsToAuthor::hydratedBy(
function (array $record) {
@n1215
n1215 / float2bin.php
Created November 15, 2018 16:23
float2bin.php
<?php
declare(strict_types=1);
function float2bin(float $float) {
return join('', array_map(function($char) {
return str_pad(decbin(ord($char)), 8, '0', STR_PAD_LEFT);
}, str_split(strrev(pack('d', $float)))));
}
var_dump(float2bin((float)1));
@n1215
n1215 / laravel-playground.php
Created February 9, 2019 02:06
simple Laravel play ground
#!/usr/bin/env php
<?php
declare(strict_types=1);
namespace MyPlayGround;
use App\User;
use Illuminate\Support\Facades\Hash;
/**
@n1215
n1215 / StringBladeRenderer.php
Last active December 5, 2022 09:16
Compile string by Blade template engine.
<?php
declare(strict_types=1);
namespace App\View;
use Illuminate\View\Compilers\BladeCompiler;
use Illuminate\View\Factory;
use Symfony\Component\Debug\Exception\FatalThrowableError;
/**
@n1215
n1215 / 01_usage.php
Last active May 9, 2019 12:07
LaraSeedGraph_idea
<?php
use FlowFactory as Flow;
use SourceFactory as Source;
use SinkFactory as Sink;
$builder = new GraphBuilder();
/** @var GraphInterface $seedGraph */
$seedGraph = $builder
@n1215
n1215 / 01_routing.php
Last active August 23, 2019 14:22
Event based notifications library idea
<?php
$container = new SomePSR11Container();
// $container->regiseter();
$notifications = new N1215\Notification\Registrar($container);
$notifications
->source(N1215\Events\UserRegistered::class)
->to(N1215\Notification\UserTargetSelector::class)
->inSingleFormOf(
@n1215
n1215 / lazyChunk.php
Last active October 16, 2019 14:11
BuildsQueries::lazyChunk()
<?php
// usage
/** @var \Illuminate\Support\LazyCollection<Post> $lazyPosts */
$lazyPosts = Post::query()
->lazyChunk(100)
->tapEach(function (Collection $posts) {
$posts->load('author');
})
->collapse();
@n1215
n1215 / StrBuilder.php
Created October 17, 2019 06:17
str builder
<?php
declare(strict_types=1);
namespace App\Support;
use Illuminate\Support\Str;
class StrBuilder
{
/**
#!/usr/bin/env bash
# コンテナを起動
function up {
docker-compose up -d
}
# コンテナ停止
function down {
docker-compose down
@n1215
n1215 / TimeTravel.php
Created November 21, 2019 06:21
Laravel Middleware for time travel debugging
<?php
declare(strict_types=1);
namespace App\Http\Middleware;
use Carbon\Carbon;
use Closure;
use Exception;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Cookie;