Skip to content

Instantly share code, notes, and snippets.

@oplanre
oplanre / Sorter.php
Last active June 27, 2024 00:54
Light abstraction layer for sorting arrays in php
<?php
class Sorter {
public function __construct(private array $array) {}
/**
* Sort the array by a specific key in ascending or descending order.
*
* @param string $key The key to sort by.
* @param string $order The order direction, 'asc' for ascending or 'desc' for descending. Default is 'asc'.
* @return self
@oplanre
oplanre / Pipeline.php
Created June 13, 2024 22:17
Simple pipeline implementation in php as a class or function
<?php
class Pipeline {
public function __construct(
private mixed $data
) {}
public function pipe(callable ...$callbacks): static {
foreach ($callbacks as $callback) {
$this->data = $callback($this->data);
}
return $this;
// Marker type for hashmaps without a value (i.e. hashsets). These won't
// allocate space for the value in the entry.
struct NoHashMapValue {};
// HashMap entries are (key, value, hash) triplets, with a boolean indicating if
// they are an empty entry. Some clients may not need to use the value slot
// (e.g. implementers of sets, where the key is the value), in which case they
// should use NoHashMapValue.
template <typename Key, typename Value>
struct TemplateHashMapEntry {
static_assert((!std::is_same<Value, NoHashMapValue>::value));
template <typename T>
class Vector {
public:
using value_type = T;
using iterator = T *;
using const_iterator = const T *;
constexpr Vector() : start_(nullptr), length_(0) {}
constexpr Vector(T *data, size_t length) : start_(data), length_(length) { DCHECK(length == 0 || data != nullptr); }
static Vector<T> New(size_t length) { return Vector<T>(new T[length], length); }
constexpr Vector<T> Erase(size_t length) {
@oplanre
oplanre / QueryBuilder.php
Created May 12, 2024 00:07
PHP querybuilder WIP
<?php
readonly class RelationshipInverse
{
public function __construct(
public string $pivotTable,
public string $relatedForeignKey,
public string $localKey,
public string $type
) {
}
@oplanre
oplanre / TSGen.php
Last active May 28, 2024 12:22
Simple TS interface generator (php impl)
<?php
class TSGen
{
public static function fromJSON(string $json, string $interfaceName): string
{
try {
return self::generateInterface(json_decode($json, true, 512, JSON_THROW_ON_ERROR), $interfaceName);
} catch (JsonException $e) {
throw new InvalidArgumentException("Error parsing JSON: " . $e->getMessage());
@oplanre
oplanre / heap-object.h
Last active April 8, 2024 14:24
PHP Typesystem Extended (C++). This is part of a project i built at work to make it easier/more stable to build php extensions
#pragma once
#include "lend-forward.h"
#include "lend-type.h" // NOLINT(build/include_directory)
#include "lend-internal.h" // NOLINT(build/include_directory)
#include "lend-handle.h" // NOLINT(build/include_directory)
#include "lendconfig.h" // NOLINT(build/include_directory)
#include <cstdint>
namespace lend {
/**
* @class HeapObject