Skip to content

Instantly share code, notes, and snippets.

View nyamsprod's full-sized avatar
😴

ignace nyamagana butera nyamsprod

😴
View GitHub Profile
@nyamsprod
nyamsprod / version3.php
Created February 23, 2022 08:22
evolution of Period string representation API
<?php
use League\Period\Period;
$period = new Period('2021-01-01', '2021-04-01');
echo $period; //displays 2021-01-01T00:00:00.000000Z/2021-04-01T00:00:00.000000Z
@nyamsprod
nyamsprod / geolocation-basic.php
Last active June 4, 2020 06:16
named-constructors to the rescue
<?php
declare(strict_types=1);
namespace Example;
final class Geolocation
{
private float $latitude;
@nyamsprod
nyamsprod / uri-template-basic-usage.php
Last active January 29, 2020 12:11
Usage examples of the league uri template class
<?php
use League\Uri\UriTemplate;
//$template is a string which complies to RFC6570
$template = 'https://example.com/hotels/{hotel}/bookings/{booking}';
$uriTemplate = new UriTemplate($template);
//$variables are the parameters that will be replaced in the template
$variables = ['booking' => '42', 'hotel' => 'Rest & Relax'];
@nyamsprod
nyamsprod / awesome-improved.php
Last active February 19, 2019 20:53
great vs awesome
<?php
namespace Sunday\Dev
use Foobar\Great;
final class Awesome
{
public const AWESOME_NUMBER = 99;
@nyamsprod
nyamsprod / criteria-converted.php
Created August 15, 2018 18:43
Introducing Bakame CSV Doctrine Collection Bridge Post Example
<?php
use Doctrine\Common\Collections\Criteria;
use League\Csv\Reader;
use function Bakame\Csv\Extension\criteria_convert;
$criteria = Criteria::create()
->andWhere(Criteria::expr()->contains('email', '@gmail.com'))
->setFirstResult(0)
->setMaxResults(10);
@nyamsprod
nyamsprod / property_exists_recursive.php
Last active October 4, 2016 09:31
Property exists recursive version
<?php
/**
* Tell whether the propety or the inner property exists
* we do not use isset as isset($obj->foo->bar->baz) would return false
* if $obj->foo->bar->baz = null
*
* Usage:
*
* if (property_exists($obj, 'foo->bar->baz')) {
@nyamsprod
nyamsprod / fetchAssoc-iterator.php
Last active December 11, 2015 11:08
Examples for the release of League\Csv 8.0.0
<?php
use League\Csv\Reader;
$formatter = function (array $row) {
$row['create_at'] = new DatetimeImmutable($row['create_at']);
return $row;
};
@nyamsprod
nyamsprod / final-regexp.php
Last active November 7, 2015 13:49
RegularExpressionDocumented.php
<?php
$uri = 'http://www.ics.uci.edu/pub/ietf/uri/#Related';
$regexp = ',^
((?<scheme>[^:/?\#]+):)? # URI scheme component
(?<authority>//([^/?\#]*))? # URI authority part
(?<path>[^?\#]*) # URI path component
(?<query>\?([^\#]*))? # URI query component
(?<fragment>\#(.*))? # URI fragment component
,x';
@nyamsprod
nyamsprod / RemoveSequence.php
Last active July 18, 2016 20:54
How to enforce a enclosure character using the League\Csv package
<?php
namespace Example;
use League\Csv\AbstractCsv;
use php_user_filter;
class RemoveSequence extends php_user_filter
{
const FILTER_NAME = 'removesequence.';
@nyamsprod
nyamsprod / filtering-pdostatement.php
Last active August 29, 2015 14:26
Filtering a Traversable object using PHP SPL filtering capabilities in response to http://www.dragonbe.com/2015/07/speeding-up-database-calls-with-pdo-and.html
<?php
$pdo = new \PDO(
$config['db']['dsn'],
$config['db']['username'],
$config['db']['password']
);
$sql = 'SELECT * FROM `gen_contact` ORDER BY `contact_modified` DESC';
$stmt = $pdo->prepare($sql);