PHP code example of nvl / primitives
1. Go to this page and download the library: Download nvl/primitives library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
nvl / primitives example snippets
use Nvl\Primitives\ValueObjects\EmailAddress;
$email = EmailAddress::from('[email protected] ');
protected function casts(): array
{
return [
'email' => EmailAddress::class,
'phone' => PhoneNumber::class,
'coordinates' => Coordinates::class,
'price' => Money::class,
];
}
protected function casts(): array
{
return [
'price_minor' => Money::class.':minor,EUR',
'price_decimal' => Money::class.':decimal,EUR',
];
}
use Brick\Math\RoundingMode;
use Nvl\Primitives\Services\MoneyFormatter;
use Nvl\Primitives\ValueObjects\Money;
$price = Money::of('19.99', 'EUR');
$shipping = Money::minor(500, 'EUR');
$total = $price->add($shipping); // EUR 24.99
$triple = $price->multiply(3); // EUR 59.97
$share = $price->divide(3, RoundingMode::HalfUp);
$total->amount(); // "24.99"
$total->minorAmount(); // "2499"
$total->currency(); // CurrencyCode("EUR")
$formatter->format($total, 'de-DE'); // locale-aware with ext-intl
'exchange_rates' => [
'implementation' => ConfiguredExchangeRateProvider::class,
'rates' => [
'EUR/USD' => '1.10',
'EUR/BGN' => '1.95583',
],
],
$usd = $converter->convert(
money: Money::of('100.00', 'EUR'),
target: 'USD',
roundingMode: RoundingMode::HalfUp,
);
$phone = PhoneNumber::fromRegion('044 668 18 00', 'CH');
(string) $phone; // +41446681800
$phone->international(); // +41 44 668 18 00
$phone->national(); // 044 668 18 00
$phone->rfc3966(); // tel:+41-44-668-18-00
$phone->region(); // CountryCode("CH")
$iban = Iban::from('DE89 3704 0044 0532 0130 00');
$iban->storageValue(); // DE89370400440532013000
$iban->formatted();
$iban->masked();
$iban->country(); // CountryCode("DE")
$country = CountryCode::from('bg'); // BG
$currency = CurrencyCode::from('eur'); // EUR
$locale = LocaleCode::from('zh_hans_cn'); // zh-Hans-CN
$country->name('en');
$currency->symbol('de');
$currency->fractionDigits();
$locale->language();
$locale->script();
$locale->regionCode(); // alpha country or numeric UN M49 region
$locale->region();
$discount = Percentage::fromPercent(20);
$discount->decimal(); // "0.2"
$discount->of('125.00'); // "25"
$discount->ofRounded(
'125.00',
2,
RoundingMode::HalfUp,
); // "25.00"
$weight = Weight::kilograms('1.5');
$weight->inGrams(); // "1500.000"
$weight->inOunces(); // "52.911"
$length = Length::from('12', 'in');
$length->in('cm', 2, RoundingMode::Unnecessary); // "30.48"
$sofia = Coordinates::from('42.6977', '23.3219');
$plovdiv = Coordinates::from('42.1354', '24.7453');
$sofia->distanceTo($plovdiv); // kilometres
$sofia->googleMapsUrl();
'email' => [
'itive(EmailAddress::class, 'email address'),
],
'iban' => [
'nullable',
new ValidPrimitive(Iban::class, 'IBAN'),
],
'phone' => [
'
$countries = $catalog->countries('Bulg', 'en', 20);
$currencies = $catalog->currencies('Euro', 'en');
$languages = $catalog->languages('Bulgarian', 'en');
$locales = $catalog->locales('bg', 'en');
$countryCode = $countries[0]['code'];
'reference' => [
'cities' => [
'sofia-bg' => [
'label' => 'Sofia',
'country' => 'BG',
'region' => 'Sofia City',
],
],
'banks' => [
'UNCRBGSF' => [
'label' => 'UniCredit Bulbank',
'country' => 'BG',
],
],
],
use Brick\Math\RoundingMode;
use Nvl\Primitives\Contracts\ExchangeRateProvider;
use Nvl\Primitives\Services\CurrencyConverter;
use Nvl\Primitives\ValueObjects\CurrencyCode;
use Nvl\Primitives\ValueObjects\Money;
$rates = Mockery::mock(ExchangeRateProvider::class);
$rates->shouldReceive('rate')->once()
->withArgs(fn (CurrencyCode $from, CurrencyCode $to, $at): bool =>
(string) $from === 'EUR' && (string) $to === 'USD' && $at === null)
->andReturn('1.10');
$this->app->instance(ExchangeRateProvider::class, $rates);
$money = Money::of('10.00', 'EUR');
$converted = $this->app->make(CurrencyConverter::class)
->convert($money, 'USD', RoundingMode::HalfUp);
expect($converted->amount())->toBe('11.00');
bash
composer hout ext-intl, also install:
composer bash
composer vendor:publish --tag=nvl-primitives-config
php artisan vendor:publish --tag=nvl-primitives-translations
php artisan vendor:publish --tag=nvl-primitives-skills