Skip to content

Instantly share code, notes, and snippets.

View devnix's full-sized avatar
🐢

Pablo Largo Mohedano devnix

🐢
View GitHub Profile
@devnix
devnix / handling-optional-input-fields-with-type-safe-abstractions.md
Created March 17, 2023 12:06 — forked from Ocramius/handling-optional-input-fields-with-type-safe-abstractions.md
Handling optional input parameters in PHP with `vimeo/psalm` and `azjezz/psl`

Handling optional input parameters in PHP with vimeo/psalm and azjezz/psl

I had an interesting use-case with a customer for which I provide consulting services: they needed multiple fields to be marked as "optional".

Example: updating a user

We will take a CRUD-ish example, for the sake of simplicity.

For example, in the following scenario, does a null $description mean "remove the description",

@devnix
devnix / _pagination.html.twig
Last active May 29, 2019 15:37 — forked from maxpou/_pagination.html.twig
Example of pagination with Twig
{#
Parameters:
* total (int): number of pages
* current (int): current pages
* url (string): route name & query (string): route parameter
ex: list/page-5?q=myFilter (5 = page and query = myFilter)
* nearbyPagesLimit (int) optional: limit of pages around the current page
#}
{% macro pagination(total, current, url, nearbyPagesLimit = 4) %}
@devnix
devnix / colormeter.php
Last active May 16, 2020 00:26 — forked from mailtruck/colormeter.js
Calculate difference in percentage between 2 hex colors. Port from Javascript to PHP. For calculating the perception difference, may would be better https://github.com/renasboy/php-color-difference
<?php
function color_meter($cwith, $ccolor) {
if (empty($cwith) || empty($ccolor)) return false;
$_cwith = ($cwith[0] === '#') ? substr($cwith, 1, 7) : $cwith;
$_ccolor = ($ccolor[0] === '#') ? substr($ccolor, 1, 7) : $ccolor;
$_r = intval(substr($_cwith, 0, 2), 16);
$_g = intval(substr($_cwith, 2, 2), 16);