Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/maikenegreiros/iterable
https://github.com/maikenegreiros/iterable
Last synced: 4 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/maikenegreiros/iterable
- Owner: maikenegreiros
- Created: 2020-08-29T19:06:23.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2020-09-05T13:02:24.000Z (about 4 years ago)
- Last Synced: 2024-10-28T06:09:41.761Z (20 days ago)
- Language: PHP
- Size: 14.6 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Iterable\Iterator
![](https://img.shields.io/packagist/v/maike/iterable?style=for-the-badge)
![](https://img.shields.io/packagist/l/maike/iterable?style=for-the-badge)
![](https://img.shields.io/packagist/php-v/maike/iterable?style=for-the-badge)In other languages, like Java, you can use Collections that allows only one type. In TypeScript, for example we can do the following:
```ts
public getItems(): Array
{
return this.items;
}
```Unfortunatelly, PHP doesn't have this kind of thing, that's why I created this abstract class
## Installing
This package is installed via composer:```sh
composer require maike/iterable
```## Usage
In order to achieve a Collection that allows only one type in PHP, we have to create a class. In the following example, I'm creating a Collection Class extending the `Iterable\Iterator` abstract class
### Creating a Collection Class
```php
use Iterable\Iterator;class DateTimeCollection extends Iterator {
public function __construct(\DateTime ...$items)
{
parent::__construct($items);
}
}
```With the above code we achieved two things:
1. We have a collection that only allows `DateTime` Objects.
2. We can iterate over our Collection as we do in an array. See the following example:```php
$datesCollection = new DateTimeCollection(
new \DateTime,
new \DateTime,
new \DateTime,
);foreach ($datesCollection as $key => $date) {
// Do whatever you want
}
```
## Contributing### Run build
```sh
make build
```### Run tests
```sh
make tests
```