Skip to content

Commit

Permalink
Added get and exist methods to ReadonlyAssociative collection (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
danijelgalic committed Sep 27, 2024
1 parent 87e43e1 commit 6d75fcc
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/support/collection/type/firehub.ReadonlyAssociative.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace FireHub\Core\Support\Collection\Type;

use FireHub\Core\Support\Collection\Type\Traits\Associative as AssociativeTrait;
use Error;

/**
* ### Read-only associative array collection type
Expand Down Expand Up @@ -50,4 +51,62 @@ public function __construct (
protected array $storage
) {}

/**
* ### Gets item from collection
* @since 1.0.0
*
* @uses \FireHub\Core\Support\Collection\Type\ReadonlyAssociative::exist() To check if an item exists in a collection.
*
* @param TKey $key <p>
* Collection key.
* </p>
*
* @throws Error If the key doesn't exist in a collection.
*
* @return TValue Item from a collection.
*/
public function get (int|string $key):mixed {

return $this->exist($key)
? $this->storage[$key]
: throw new Error("Key $key doesn't exist in collection.");

}

/**
* ### Check if item exist in collection
* @since 1.0.0
*
* @param TKey $key <p>
* Collection key.
* </p>
*
* @return bool True on success, false otherwise.
*/
public function exist (int|string $key):bool {

return isset($this->storage[$key]);

}

/**
* ### Check if item exist in collection
* @since 1.0.0
*
* @uses \FireHub\Core\Support\Collection\Type\ReadonlyAssociative::exist() To check if an item exists in a collection.
*
* @param TKey $key <p>
* Collection key.
* </p>
*
* @return bool True on success, false otherwise.
*
* @note This method is an alias of has method.
*/
public function has (int|string $key):bool {

return $this->exist($key);

}

}

0 comments on commit 6d75fcc

Please sign in to comment.