Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View devbanana's full-sized avatar

Brandon Olivares devbanana

View GitHub Profile
@devbanana
devbanana / YNAB.php
Created July 19, 2018 18:27
Basic YNAB API implementation in PHP in Symfony 4
<?php
namespace App\Services;
use Symfony\Component\Security\Core\Security;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\User;
use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
@devbanana
devbanana / autocomplete.php
Last active December 26, 2019 17:12
This gist accepts a search query and calls the IEX Cloud API to fetch all stock tickers that match the query.
<?php
require_once 'vendor/autoload.php';
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
$request = Request::createFromGlobals();
if (!isset($_ENV['IEX_TOKEN'])) {
throw new Exception('IEX token not found.');
@devbanana
devbanana / autocomplete2.php
Last active December 26, 2019 17:13
This gist accepts a search query and uses the IEX Cloud API to fetch all stock tickers that match the query. It returns both the ticker symbol and company name.
<?php
require_once 'vendor/autoload.php';
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
$request = Request::createFromGlobals();
if (!isset($_ENV['IEX_TOKEN'])) {
throw new Exception('IEX token not found.');
@devbanana
devbanana / get-mwf-date.php
Created February 29, 2020 13:04
Gets the next Monday, Wednesday, or Friday after a given date.
<?php
function getNextMwf(DateTime $date): DateInterval
{
$m = (clone $date)->modify('next Monday');
$w = (clone $date)->modify('next Wednesday');
$f = (clone $date)->modify('next Friday');
return $date->diff(min($m, $w, $f));
}
@devbanana
devbanana / error-handler-new.php
Created August 4, 2021 11:22
In PHP 8, error_reporting() no longer returns 0 if error suppression is enabled using @. Here is how you detect if you should respond to an error in a custom error handler.
<?php
set_error_handler(function (int $errno, string $errstr): bool {
if (!(error_reporting() & $errno)) {
// Do nothing
return false;
}
echo "Received error with message: $errstr\n";
return true;
function getWord(text: string, word: number) {
if (word < 1) throw new Error('word must be no less than 1');
const wordIndex = word - 1;
// https://regex101.com/r/1zDG8a/2
const words = text.toLowerCase().match(/\w+(?:'\w+)*/g);
if (words === null) {
throw new Error('Please provide some text');
}