Skip to content

Commit

Permalink
Added read-only classes (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
danijelgalic committed Sep 26, 2024
1 parent ba8edc0 commit 0c41237
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## [Unreleased](https://github.com/The-FireHub-Project/Core/compare/v0.2.2...develop-pre-alpha-m2)

- Created read-only Collections ([#95](https://github.com/The-FireHub-Project/Core/issues/95), [6a2c32f](https://github.com/The-FireHub-Project/Core/pull/91/commits/6a2c32f))
- Added a read-only collection as a possible Register type ([#95](https://github.com/The-FireHub-Project/Core/issues/95), [ba8edc0](https://github.com/The-FireHub-Project/Core/pull/91/commits/ba8edc0))

## [v0.2.2](https://github.com/The-FireHub-Project/Core/compare/v0.2.1...v0.2.2) - (2024-09-26)

### Added
Expand Down
18 changes: 17 additions & 1 deletion src/support/collection/helpers/firehub.Fill.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
};
use FireHub\Core\Support\Collection;
use FireHub\Core\Support\Collection\Type\ {
Indexed, Fix, Gen
Indexed, Fix, Gen, ReadOnlyIndexed
};
use FireHub\Core\Support\LowLevel\Arr;

Expand Down Expand Up @@ -82,6 +82,22 @@ public function list ():Indexed {

}

/**
* ### Fill as a read-only indexed collection
* @since 1.0.0
*
* @uses \FireHub\Core\Support\Collection::list() To create an indexed collection.
* @uses \FireHub\Core\Support\Collection\Type\ReadOnlyIndexed As return.
* @uses \FireHub\Core\Support\LowLevel\Arr::fill() To fill an array with values.
*
* @return \FireHub\Core\Support\Collection\Type\ReadOnlyIndexed<TValue> Indexed read-only collection type.
*/
public function readOnlyList ():ReadOnlyIndexed {

return Collection::readOnlyList(Arr::fill($this->value, 0, $this->length));

}

/**
* ### Fill as a fixed collection
* @since 1.0.0
Expand Down
18 changes: 17 additions & 1 deletion src/support/collection/helpers/firehub.FillKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
};
use FireHub\Core\Support\Collection;
use FireHub\Core\Support\Collection\Type\ {
Associative, Gen
Associative, Gen, ReadOnlyAssociative
};
use FireHub\Core\Support\LowLevel\Arr;

Expand Down Expand Up @@ -72,6 +72,22 @@ public function associative ():Associative {

}

/**
* ### Fill as a read-only associative collection
* @since 1.0.0
*
* @uses \FireHub\Core\Support\Collection::readOnlyAssociative() To create a read-only associative collection.
* @uses \FireHub\Core\Support\LowLevel\Arr::fillKeys() To fill an array with values, specifying keys.
* @uses \FireHub\Core\Support\Collection\Type\ReadOnlyAssociative As return.
*
* @return \FireHub\Core\Support\Collection\Type\ReadOnlyAssociative<TKey, TValue> Associative read-only collection type.
*/
public function readOnlyAssociative ():ReadOnlyAssociative {

return Collection::readOnlyAssociative(Arr::fillKeys($this->keys, $this->value));

}

/**
* ### Fill as a lazy collection
* @since 1.0.0
Expand Down
67 changes: 66 additions & 1 deletion src/support/firehub.Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
InitStatic, Trait\ConcreteStatic
};
use FireHub\Core\Support\Collection\Type\ {
Indexed, Associative, Fix, Gen, Obj
Indexed, Associative, Fix, Gen, Obj, ReadOnlyIndexed, ReadOnlyAssociative
};
use FireHub\Core\Support\Collection\Helpers\ {
Emp, Fill, FillAssoc, FillKeys, Range
Expand Down Expand Up @@ -72,6 +72,35 @@ public static function list (array|Closure $storage):Indexed {

}

/**
* ### Indexed read-only array collection type
*
* Collections which have numerical indexes in an ordered sequential manner (starting from 0 and ending with n-1).
* @since 1.0.0
*
* @uses \FireHub\Core\Support\Collection\Type\ReadOnlyIndexed As return.
*
* @template TValue
*
* @example
* ```php
* use FireHub\Core\Support\Collection;
*
* $collection = Collection::readOnlyList(fn ():array => ['one', 'two', 'three']);
* ```
*
* @param array<TValue>|Closure():array<TValue> $storage <p>
* Array underlying data.
* </p>
*
* @return \FireHub\Core\Support\Collection\Type\ReadOnlyIndexed<TValue> Indexed read-only collection.
*/
public static function readOnlyList (array|Closure $storage):ReadOnlyIndexed {

return new ReadOnlyIndexed($storage instanceof Closure ? $storage(): $storage);

}

/**
* ### Associative array collection type
*
Expand Down Expand Up @@ -108,6 +137,42 @@ public static function associative (array|Closure $storage):Associative {

}

/**
* ### Associative read-only array collection type
*
* Collections that use named keys that you assign to them.
* @since 1.0.0
*
* @uses \FireHub\Core\Support\Collection\Type\ReadOnlyAssociative As return.
*
* @template TKey of array-key
* @template TValue
*
* @example
* ```php
* use FireHub\Core\Support\Collection;
*
* $collection = Collection::readOnlyAssociative(fn ():array => [
* 'firstname' => 'John',
* 'lastname' => 'Doe',
* 'age' => 25,
* 'height' => '190cm',
* 'gender' => 'male'
* ]);
* ```
*
* @param array<TKey, TValue>|Closure():array<TKey, TValue> $storage <p>
* Array underlying data.
* </p>
*
* @return \FireHub\Core\Support\Collection\Type\ReadOnlyAssociative<TKey, TValue> Associative read-only collection.
*/
public static function readOnlyAssociative (array|Closure $storage):ReadOnlyAssociative {

return new ReadOnlyAssociative($storage instanceof Closure ? $storage(): $storage);

}

/**
* ### Fixed collection type
*
Expand Down

0 comments on commit 0c41237

Please sign in to comment.