1. Go to this page and download the library: Download starburst/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/ */
starburst / utils example snippets
$jsonString = \Starburst\Utils\Json::encode(["name" => "John Doe", "age" => 30]);
// behaviour is using encodeNull flag
\Starburst\Utils\Json::encode(null, encodeNull: true) === 'null';
\Starburst\Utils\Json::encode(null, encodeNull: false) === null;
class TestObject
{
use \Starburst\Utils\Traits\GetArrayCopyTrait;
public function __construct(
public int $id,
#[\Starburst\Utils\Attributes\DateFormat('Y-m-d')]
public \DateTimeImmutable $startDate,
public \DateTimeImmutable $createdOn,
#[\Starburst\Utils\Attributes\HiddenProperty]
public string $internalField,
#[\Starburst\Utils\Attributes\CustomName('parent')]
public ?TestObject $parentObject = null,
) {}
}
$obj = new TestObject(
1,
new DateTimeImmutable('2024-05-24 08:00:00'),
new DateTimeImmutable('2024-05-20 12:23:01'),
'internalValue'
);
$obj->getArrayCopy() === [
'id' => 1,
'startOn' => '2024-05-24',
'createdOn' => '2024-05-20 12:23:01',
'parent' => null,
];
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class CustomAttribute {}
class CustomResolver implements \Starburst\Utils\ValueResolvers\ValueResolver
{
/**
* @param \WeakMap<object, mixed> $tracker
*/
public function resolve(mixed $value, \WeakMap $tracker, ?\ReflectionProperty $reflectionProperty = null): mixed
{
$attrs = $reflectionProperty?->getAttributes(CustomAttribute::class);
if (!$attrs) {
return $value;
}
if ($value instanceof \DateTimeInterface) {
return $value->format('j.m k\l. H:i');
}
return 'Random string';
}
}
$collection = new \Starburst\Utils\ValueResolvers\ResolverCollection(
new \Starburst\Utils\Tests\Stubs\CustomValueResolver(),
);
$obj = new class (1) {
use \Starburst\Utils\Traits\GetArrayCopyTrait;
public function __construct(
#[CustomAttribute]
private mixed $value,
) {}
};
$obj->getArrayCopy() === [
'value' => 'Random string'
];
$obj = new class (new \DateTimeImmutable('2024-05-24 08:12:42')) {
use \Starburst\Utils\Traits\GetArrayCopyTrait;
public function __construct(
#[CustomAttribute]
private mixed $value,
) {}
};
$obj->getArrayCopy() === [
'value' => '24.05 kl. 08:12:42'
];
use Starburst\Utils\Path;
echo Path::normalize("\\xampp\\htdocs\\project\\images\\..\\css\\style.css");
// => /xampp/htdocs/project/images/../css/style.css