PHP code example of cancio-labs / php-bag
1. Go to this page and download the library: Download cancio-labs/php-bag 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/ */
cancio-labs / php-bag example snippets
use CancioLabs\Ds\Bag\Bag;
$bag = new Bag([
'name' => 'Ada Lovelace',
'contact' => [
'email' => '[email protected] ',
],
'active' => '1',
]);
$bag->getString('name'); // 'Ada Lovelace'
$bag->getString('contact.email'); // '[email protected] '
$bag->getBool('active'); // true
$bag->getString('missing', 'N/A'); // 'N/A'
$bag = new Bag(['name' => 'Ada']);
$bag
->add('role', 'Mathematician')
->merge(['active' => true])
->set([
'name' => 'Ada Lovelace',
'active' => true,
]);
$bag->getAll();
// ['name' => 'Ada Lovelace', 'active' => true]
count($bag);
foreach ($bag as $key => $value) {
// ...
}
use CancioLabs\Ds\Bag\Bag;
enum Status: string
{
case Draft = 'draft';
case Published = 'published';
}
$bag = new Bag([
'attempts' => '3',
'metadata' => ['source' => 'import'],
'published_at' => '2026-09-21 12:30:00',
'status' => 'published',
]);
$bag->getInt('attempts'); // 3
$bag->getBag('metadata')->getString('source'); // 'import'
$bag->getDateTime('published_at'); // DateTimeInterface
$bag->getEnum('status', Status::class); // Status::Published
$bag = new Bag([
'user' => [
'profile' => [
'name' => 'Ada',
],
],
]);
$bag->has('user.profile.name'); // true
$bag->getString('user.profile.name'); // 'Ada'
$bag->remove('user.profile.name');
$bag->has('user.profile.name'); // false
$bag = new Bag([
'user' => [
'name' => 'Ada',
'address' => ['city' => 'London'],
],
]);
$bag->toDotNotation()->toArray();
// [
// 'user.name' => 'Ada',
// 'user.address.city' => 'London',
// ]
$scores = new Bag([
'ada' => 100,
'grace' => 95,
'linus' => 80,
]);
$passing = $scores->filter(
fn (string $name, int $score): bool => $score >= 90
);
$labels = $passing->map(
fn (string $name, int $score): string => "$name: $score"
);
$scores->every(fn (string $name, int $score): bool => $score >= 80); // true
$scores->some(fn (string $name, int $score): bool => $score === 100); // true
bash
composer