Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sebastianbergmann/shaku
Tool to automatically generate type-safe Collection and CollectionIterator classes
https://github.com/sebastianbergmann/shaku
Last synced: about 2 months ago
JSON representation
Tool to automatically generate type-safe Collection and CollectionIterator classes
- Host: GitHub
- URL: https://github.com/sebastianbergmann/shaku
- Owner: sebastianbergmann
- License: other
- Archived: true
- Created: 2018-11-07T16:50:02.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-05-04T13:04:07.000Z (over 4 years ago)
- Last Synced: 2024-09-21T10:02:57.603Z (2 months ago)
- Language: PHP
- Homepage:
- Size: 1.88 MB
- Stars: 32
- Watchers: 6
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: ChangeLog.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Shaku, the Collection Generator
`shaku` can automatically generate type-safe `Collection` and `CollectionIterator` classes.
## Installation
The recommended way to use this tool is a [PHP Archive (PHAR)](https://php.net/phar):
```bash
$ wget https://phar.phpunit.de/shaku.phar$ php shaku.phar --version
```Furthermore, it is recommended to use [Phive](https://phar.io/) for installing and updating the tool dependencies of your project.
Alternatively, you may use [Composer](https://getcomposer.org/) to download and install this tool as well as its dependencies. [This is not recommended, though.](https://twitter.com/s_bergmann/status/999635212723212288)
## Usage
Consider you have a class named `Value` (declared in `src/Value.php`) and need a type-safe `ValueCollection` for objects of this type:
```php
namespace vendor;final class Value
{
// ...
}
```### Generating the `Collection` and `CollectionIterator`
You can use this tool to automatically generate the code for the `ValueCollection` and `ValueCollectionIterator` class like so:
```
$ php shaku.phar --immutable vendor Value src
```The above results in the generation of the code shown below:
```php
add($item);
}return $collection;
}public static function fromList(Value ...$items): self
{
return self::fromArray($items);
}private function __construct()
{
}private function add(Value $item): void
{
$this->items[] = $item;
}/**
* @return Value[]
*/
public function toArray(): array
{
return $this->items;
}public function getIterator(): ValueCollectionIterator
{
return new ValueCollectionIterator($this);
}public function count(): int
{
return \count($this->items);
}public function isEmpty(): bool
{
return empty($this->items);
}public function contains(Value $item): bool
{
foreach ($this->items as $_item) {
if ($_item === $item) {
return true;
}
}return false;
}
}
``````php
items = $collection->toArray();
}public function count(): int
{
return \iterator_count($this);
}public function rewind(): void
{
$this->position = 0;
}public function valid(): bool
{
return $this->position < \count($this->items);
}public function key(): int
{
return $this->position;
}public function current(): Value
{
return $this->items[$this->position];
}public function next(): void
{
$this->position++;
}
}
```### Using the generated `Collection` and `CollectionIterator`
#### Creating a collection from an array of objects
```php
$values = ValueCollection::fromArray([new Value, new Value]);
```#### Creating a collection from a list of objects
```php
$values = ValueCollection::fromList(new Value, new Value);
```#### Creating an empty collection and adding objects to it
```php
$values = new ValueCollection;$values->add(new Value);
$values->add(new Value);
```# Shaku?
This tool is named after "Shaku, the Collector", Copyright Blizzard Entertainment, Inc.