PHP code example of infinityloop-dev / utils

1. Go to this page and download the library: Download infinityloop-dev/utils 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/ */

    

infinityloop-dev / utils example snippets


$json = Json::fromString($jsonString);      // (no decoding is done at this step)

$json['foo'] = 'bar';                       // adding/updating values (decoding is done on this step)
unset($json['foo2']);                       // removing value
$json->foo3 = 'bar3;                        // oop interface is also available

$jsonString = $json->toString();            // (encoding of updated array into string again)
$jsonString = $json->toString();            // (no encoding is done, because previously encoded string is up to date)

$string = 'foo-bar_bazFoo123baz';

CaseConverter::toCamelCase($string);        // fooBarBazFoo123Baz
CaseConverter::toPascalCase($string);       // FooBarBazFoo123Baz
CaseConverter::toSnakeCase($string);        // foo_bar_baz_foo_123_baz
CaseConverter::toKebabCase($string);        // foo-bar-baz-foo-123-baz
CaseConverter::splitWords($string);         // [ foo, bar, baz, foo, 123, baz ]

class Foo { public string $name; public function __construct(string $name) { $this->name = $name; } }
class FooSet extends ObjectSet { protected const INNER_CLASS = Foo::class; }

$set = new FooSet([new Foo(), new Bar(), new Baz()]); // error

// automaticaly generated index keys
$set = new FooSet([new Foo('foo1'), new Foo('foo2'), new Foo('foo3')]);
echo $set[0]->name; // foo1
echo $set[1]->name; // foo2
echo $set[2]->name; // foo3

class NamedFooSet extends ObjectSet 
{ 
    protected const INNER_CLASS = Foo::class; 

    protected function getKey($fooObject)
    {
        return $fooObject->name;
    }
}

// named keys
$set = new NamedFooSet([new Foo('foo1'), new Foo('foo2'), new Foo('foo3')]);
echo $set['foo1']->name; // foo1
echo $set['foo2']->name; // foo2
echo $set['foo3']->name; // foo3