Skip to content

Instantly share code, notes, and snippets.

View nrctkno's full-sized avatar

Nicolás Castro nrctkno

View GitHub Profile
@nrctkno
nrctkno / AppstoreConnectClient.php
Last active January 22, 2024 16:08
Appstore connect API client method to get prices from group ID and provider ID (which are provided by Appstore server API subscriptionStatuses's response).
<?php
class AppStoreConnectAPIClient
{
...
/**
* Return all prices indexed by currency code, given a subscription group ID and a product ID.
* @return array<int|string, array<string,mixed>>
*/
<php
//uses Zumba Core HTTP as facade
function getToken(): string
{
$zoomAccountId = '';
$zoomClientId = '';
$zoomClientSecret = '';
$zoomOAuthUrl = 'https://zoom.us/oauth/token'; //note that this is not the same URL as the one for the API
1. Exception handlers
- Handle Exception hierarchy and translate into suitable HTTP response codes (or whatever the protocol is)
2. SRP
- One file per user story
- Fight against query hell
3. Materialize inputs and outputs (requests and responses)
3. Achieve a clear separation of config files
- External services
- Data stores
- Constants
@nrctkno
nrctkno / WithDynamicAssignment.php
Last active February 23, 2023 22:39
Trait to implement dynamic assignment of object variables from array with undefined keys. Useful for Request objects.
<?php
trait WithDynamicAssignment
{
public function set(
mixed $dest,
array $props,
string $field,
string $type = 'string',
mixed $default = null
@nrctkno
nrctkno / typescript_check_domain_imports.sh
Last active May 22, 2022 23:39
Checks whether all imported modules belong to domain objects in a typescript project
# Checks whether all imported modules belong to domain objects inside domain
# Imported files must
# 1. have a relative path,
# 2. have the module's name enclosed by single or double quotes and
# 3. not propagate outside the domain
filename=check_domain_imports.txt
basedir="src/Domain/"
grep -r -n 'import ' src/Domain/ > $filename
<?php
class Random
{
public static function distributed($elements)
{
if (array_sum($elements) !== 100) {
throw new \Exception('randomWithDistribution: sum of probabilities must be 100');
@nrctkno
nrctkno / randomWithDistribution.php
Last active April 25, 2023 23:53
Get distribution-based random element in PHP
<?php
function randomWithDistribution(array $elements) {
if(array_sum($elements) !== 100) {
throw new \Exception('randomWithDistribution: sum of probabilities must be 100');
}
$random = rand(1, 100);
$tot = 0;
@nrctkno
nrctkno / show_composer_lock_reqs.php
Last active March 2, 2021 13:38
List all non-repeated dependency versions in a composer.lock file by package
<?php
/*
usage:
$ php -f .\show_composer_lock_reqs.php
$ php -f .\show_composer_lock_reqs.php /my/base/dir/ (note the slash at the end)
*/
array_walk(
array_reduce (