Skip to content

Instantly share code, notes, and snippets.

View dg's full-sized avatar
🏠
Working from home

David Grudl dg

🏠
Working from home
View GitHub Profile
@dg
dg / html.regex
Last active January 10, 2024 08:30
Regular expression for parsing HTML
~
(?(DEFINE)
(?<entity>
&
(
[a-z][a-z0-9]+ # named entity
|
\#\d+ # decimal number
|
@dg
dg / classes.php
Created August 11, 2023 11:11
Why NEON is better than YAML for dependency injection configuration
<?php
class FooSettings
{
public function __construct(
public readonly int $id,
public readonly string $name,
) {
}
}
@dg
dg / Psr11ContainerAdapter.php
Created March 22, 2023 00:32
PSR-11 adapter for Nette DI Container
<?php
declare(strict_types=1);
use Psr\Container\ContainerInterface;
use Nette\DI\Container;
class Psr11ContainerAdapter implements ContainerInterface
{
public function __construct(
@dg
dg / verifyIC.php
Created June 30, 2015 15:03
Ověření rodného čísla a IČ
<?php
function verifyIC($ic)
{
// be liberal in what you receive
$ic = preg_replace('#\s+#', '', $ic);
// má požadovaný tvar?
if (!preg_match('#^\d{8}$#', $ic)) {
return FALSE;
@dg
dg / composer-frontline.php
Last active February 13, 2023 14:14
Composer Frontline: Updates all the version constraints of dependencies in the composer.json file to their latest version.
<?php
declare(strict_types=1);
// Updates all the version constraints of dependencies in the composer.json file to their latest version.
//
// usage: composer-frontline.php (updates all Nette packages)
// composer-frontline.php doctrine/* (updates all Doctrine packages)
// composer-frontline.php * (updates all packages)
@dg
dg / nette-events.php
Created February 8, 2023 17:22
Nette Events
<?php
class Circle
{
public array $onChange = [];
public float $radius = 0;
public function setRadius(float $radius): void
{
foreach ($this->onChange as $handler) {
@dg
dg / counter.php
Created April 6, 2022 14:26
Pair / unpair {label} counter
<?php
$path = getcwd();
echo "Scanning $path\n";
$it = new RecursiveDirectoryIterator($path);
$it = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::LEAVES_ONLY);
$it = new RegexIterator($it, '~\.latte$~');
$countPair = 0;
$countUnpair = 0;
foreach ($it as $file) {
@dg
dg / git2json.py
Last active October 27, 2021 19:50
Git log to JSON
# install pygit2: pip install pygit2
import pygit2
import json
repo = pygit2.Repository('path/to/repository')
last = repo[repo.head.target]
data = []
for commit in repo.walk(last.id):
@dg
dg / example.php
Created September 27, 2021 11:51
Standalone Nette Forms example [cs]
<?php
declare(strict_types=1);
if (@!include __DIR__ . '/../vendor/autoload.php') {
die('Nainstalujte balíčky pomocí `composer require nette/forms`');
}
use Nette\Forms\Form;
@dg
dg / example.php
Created September 27, 2021 11:51
Standalone Nette Forms example [en]
<?php
declare(strict_types=1);
if (@!include __DIR__ . '/../vendor/autoload.php') {
die('Install packages using `composer require nette/forms`');
}
use Nette\Forms\Form;