Coercive App

0.0.47 2025-09-30 08:08 UTC

README

!!! IN WORKS !!!

Get

composer require coercive/app

Usage

# in works ...

Currency

Load class with basic config

$config = (new Config)
    ->setLanguage('fr_FR')
    ->setCurrency('EUR')
    ->setPriceDigits(2);

$currency = new Currency($config);

Detect decimal

$currency->hasDecimal(3.50)
# > true

$currency->hasDecimal(3.00)
# > false

Format

echo $currency->format(13.756)
# > 13,76 €

echo $currency->format(13.000, false)
# > 13,00

echo $currency->format(13.000, true, true)
# > 13 €

$currency->setTruncate(true);
echo $currency->format(13.000, false)
# > 13

Jason

JSON encode with float handler

$data = [528.56 * 100];

echo json_encode($data);
# > [52855.99999999999]

echo new Jason($data, JSON_NUMERIC_CHECK);
# > [52856]

Decimal option

$data = ['52855.99999999999', 52855.99, 52855.99999999999, 528.56 * 100];
$jason = new Jason($data, JSON_NUMERIC_CHECK);
$jason->setDecimals(2);

echo $jason;
# > [52855.99999999999,52855.99,52856,52856]

Truncate option

$data = ['52855.99999999999', 52855.99, 52855.99999999999, 528.56 * 100];
$jason = new Jason($data, JSON_NUMERIC_CHECK);
$jason->setTruncate(true);

echo $jason;
# > [52855.99999999999,52855.99,52855.99,52855.99]

Locale

Load class with minimal config

$config = (new Config)
    ->setLanguage('fr_FR')
    ->setLanguages(['fr_FR', 'en_GB']);

$locale = new Locale($config);

StrfTime backward compatibility

$config->setLanguage('fr_FR');
echo $locale->strftime('2025-09-16 09:32:17');
# > mardi 16 septembre 2025 09:32:17

$config->setLanguage('en_GB');
echo $locale->strftime('2025-09-16 09:32:17');
# > Tuesday 16 September 2025 09:32:17

$locale->setLocale('fr_FR');
echo $locale->strftime('2025-09-16 09:32:17', '%c');
# > mardi 16 septembre 2025 à 09:32:17 temps universel coordonné

$locale->setLocale();
echo $locale->strftime('2025-09-16 09:32:17', '%x');
# > Tuesday, 16 September 2025

$locale->setLocale();
echo $locale->strftime('2025-09-16 09:32:17', '%X');
# > '09:32:17 Coordinated Universal Time

Use new system IntlDateFormatter with ICU format

$locale->setLocale();
echo $locale->format($example, 'EEEE');
# > Tuesday

$locale->setLocale('fr_FR');
echo $locale->format($example, 'EEE e dd ww');
# > mar. 2 16 38

Export array of day names

$locale->setLocale('fr_FR');
$days = $locale->getDayNames(1);
foreach ($days as $day) {
    echo $day['full'] . "\n\n";
}

Export array of month names

$locale->setLocale('fr_FR');
$months = $locale->getMonthNames();
foreach ($months as $month) {
    echo $month['full'] . "\n\n";
}

ReCaptcha handler

Load class and set your keys

$recaptcha = new ReCaptcha;

$recaptcha
   ->setPublicKey("RECAPTCHA_PUBLIC_KEY")
   ->setPrivateKey("RECAPTCHA_SECRET_KEY")

Check if your token is valid

if (!$recaptcha->validateAndPersist($_POST['inputTokenToCheck'])) {
    die('invalid');
}

Optional: by default is V2 ; set threshold if you wan't to use reCaptcha V3

$recaptcha->threshold(0.5)

Optional: use storage data to optimize your quota

$recaptcha->setStoreCallback(function($result) {
	/* your storage logic here */
})

$recaptcha->setRetrieveCallback(function() {
	/* your retrieve logic here */
})

/* use `validateAndPersist()` to trigger your callbacks */
$recaptcha->validateAndPersist($_POST['inputTokenToCheck'])

Full example with session storage logic

$recaptcha
   ->setPublicKey("RECAPTCHA_PUBLIC_KEY")
   ->setPrivateKey("RECAPTCHA_SECRET_KEY")
   ->threshold(0.5)
   ->setStoreCallback(function($result) {
         $_SESSION['recaptcha']['result'] = $result;
         $_SESSION['recaptcha']['timestamp'] = time();
   })
   ->setRetrieveCallback(function () {
         if(!isset($_SESSION['recaptcha'])) {
            return null;
         }
         if(($_SESSION['recaptcha']['timestamp'] ?? 0) + (24 * 60 * 60) < time()) {
            return null; # example 1 day in second before recheck
         }
         return $_SESSION['recaptcha']['result'] ?? false;
   });

SqlTableName handler

Load class and set your environment name, and the prefix/sufix options.

$table = new SqlTableName('prod', ['test' => 'TEST_'], ['prod' => '_PROD']);

Get your table name with backticks

echo $table->database('HELLO')->table('WORLD');
# => `HELLO_PROD`.`WORLD`

Change your environment

$table->env('test');
		
echo $table->database('HELLO')->table('WORLD');
# => `TEST_HELLO`.`WORLD`

Add an alias

$table->alias('happy');

echo $table->database('HELLO')->table('WORLD');
# => `TEST_HELLO`.`WORLD` as happy

You can deactivate backticks in constructor, or in separated method

$table->backtick(false);

echo $table->database('HELLO')->table('WORLD');
# => TEST_HELLO.WORLD