Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/graphpql/utils
:hammer: Common utility classes for infinityloop packages.
https://github.com/graphpql/utils
json php utility
Last synced: about 2 months ago
JSON representation
:hammer: Common utility classes for infinityloop packages.
- Host: GitHub
- URL: https://github.com/graphpql/utils
- Owner: graphpql
- License: mit
- Created: 2020-03-02T14:26:15.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-03-16T14:37:22.000Z (10 months ago)
- Last Synced: 2024-11-02T14:51:50.134Z (2 months ago)
- Topics: json, php, utility
- Language: PHP
- Homepage: https://www.github.com/graphpql
- Size: 135 KB
- Stars: 2
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Utils [![PHP](https://github.com/graphpql/utils/actions/workflows/php.yml/badge.svg)](https://github.com/graphpql/utils/actions/workflows/php.yml)
:hammer: Common utility classes for infinityloop packages.
## Introduction
This component provides some utility classes which are used across organisation packages.
## Installation
Install package using composer
```
composer require infinityloop-dev/utils
```## Dependencies
- PHP >= 8.1
## Classes
### Json
Json wrapper which allows you to work with Json as if it was array. Decoding and encoding is fully lazy.
```php
$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)
```### CaseConverter
Simple class which transforms case of strings.
```php
$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 ]
```### ClassSet
Typesafe array of objects of a same type.
```php
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; // foo3class 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
```