Skip to content

Commit

Permalink
Merge pull request #26 from adamusgr/master
Browse files Browse the repository at this point in the history
Add comments, fix code on HasIterator and TypeClassification
  • Loading branch information
firebed authored Oct 20, 2024
2 parents 470a939 + 8529dd9 commit a8fe1f7
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/Enums/FeesPercentCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ enum FeesPercentCategory: int


/**
* ΤΕισφορά δακοκτονίας [ποσό]
* Εισφορά δακοκτονίας [ποσό]
*/
case TYPE_12 = 12;

Expand Down
2 changes: 1 addition & 1 deletion src/Services/CategoryClassificationCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function getIterator(): Traversable

public function isEmpty(): bool
{
return empty($this->toArray());
return empty($this->classifications);
}

private function toEnum(mixed $value): IncomeClassificationCategory|ExpenseClassificationCategory|null
Expand Down
95 changes: 85 additions & 10 deletions src/Services/TypeClassificationCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,27 @@

class TypeClassificationCollection implements ArrayAccess, IteratorAggregate
{
/**
* @var array<int|string, IncomeClassificationType|ExpenseClassificationType|string> $classifications
*/
private array $classifications;
private bool $isIncome;

/**
* @param array<int|string, IncomeClassificationType|ExpenseClassificationType|string> $classifications
* @param bool $isIncome
*/
public function __construct(array $classifications, bool $isIncome)
{
$this->classifications = $classifications;
$this->isIncome = $isIncome;
}

/**
* Converts classifications to an associative array with keys and labels.
*
* @return array<string, string>
*/
public function toKeyLabel(): array
{
$results = [];
Expand All @@ -34,64 +46,127 @@ public function toKeyLabel(): array
return $results;
}

/**
* Returns the keys of the classifications array.
*
* @return array<int|string>
*/
public function keys(): array
{
return $this->toArray();
return $this->classifications;
}

/**
* Returns the classifications as an array.
*
* @return array<int|string, IncomeClassificationType|ExpenseClassificationType|string>
*/
public function toArray(): array
{
return $this->classifications;
}

/**
* Checks if an offset exists in the classifications array.
*
* @param mixed $offset
* @return bool
*/
public function offsetExists(mixed $offset): bool
{
return isset($this->classifications[$offset]);
}

public function offsetGet(mixed $offset): mixed
/**
* Gets the value at the given offset.
*
* @param mixed $offset
* @return string|IncomeClassificationType|ExpenseClassificationType|null
*/
public function offsetGet(mixed $offset): string|null|IncomeClassificationType|ExpenseClassificationType
{
return $this->classifications[$offset];
return $this->classifications[$offset] ?? null;
}

/**
* Checks if the classifications array contains a specific value.
*
* @param mixed $value
* @return bool
*/
public function contains(mixed $value): bool
{
if ($value === null) {
return empty($this->toArray()) || in_array($value, $this->toArray(), true);
return empty($this->classifications) || in_array($value, $this->classifications, true);
}

if ($value instanceof BackedEnum) {
$value = $value->value;
}
return in_array($value, $this->toArray(), true);

return in_array($value, $this->classifications, true);
}

/**
* Gets the classification as an enum if possible.
*
* @param mixed $offset
* @return IncomeClassificationType|ExpenseClassificationType|null
*/
public function get(mixed $offset): IncomeClassificationType|ExpenseClassificationType|null
{
return $this->toEnum($offset);
}

/**
* Sets the value at the given offset.
*
* @param mixed $offset
* @param IncomeClassificationType|ExpenseClassificationType|string $value
* @return void
*/
public function offsetSet(mixed $offset, mixed $value): void
{
$this->classifications[$offset] = $value;
}

/**
* Unsets the value at the given offset.
*
* @param mixed $offset
* @return void
*/
public function offsetUnset(mixed $offset): void
{
unset($this->classifications[$offset]);
}

/**
* Returns an iterator for the classifications array.
*
* @return Traversable<int|string, IncomeClassificationType|ExpenseClassificationType|string>
*/
public function getIterator(): Traversable
{
return new ArrayIterator($this->classifications);
}

/**
* Checks if the classifications array is empty.
*
* @return bool
*/
public function isEmpty(): bool
{
return empty($this->toArray());
return empty($this->classifications);
}


/**
* Converts a value to the appropriate enum type if possible.
*
* @param mixed $value
* @return IncomeClassificationType|ExpenseClassificationType|null
*/
private function toEnum(mixed $value): IncomeClassificationType|ExpenseClassificationType|null
{
if ($value === null) {
Expand All @@ -106,4 +181,4 @@ private function toEnum(mixed $value): IncomeClassificationType|ExpenseClassific
? IncomeClassificationType::tryFrom($value)
: ExpenseClassificationType::tryFrom($value);
}
}
}
40 changes: 39 additions & 1 deletion src/Traits/HasIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,80 @@

namespace Firebed\AadeMyData\Traits;

use ArrayAccess;
use ArrayIterator;
use IteratorAggregate;
use Traversable;

/**
* Trait HasIterator
*
* @template Model
* @implements ArrayAccess<int|string, Model>
* @implements IteratorAggregate<int|string, Model>
*/
trait HasIterator
{
/**
* Returns an iterator for the attributes.
*
* @return Traversable<int|string, Model>
*/
public function getIterator(): Traversable
{
return new ArrayIterator($this->attributes);
}

/**
* Returns the count of attributes.
*
* @return int
*/
public function count(): int
{
return count($this->attributes);
}

/**
* Checks if the given offset exists in the attributes.
*
* @param int|string $offset
* @return bool
*/
public function offsetExists(mixed $offset): bool
{
return array_key_exists($offset, $this->attributes);
}

/**
* @return Model
* Gets the value at the given offset.
*
* @param int|string $offset
* @return Model|null
*/
public function offsetGet(mixed $offset): mixed
{
return $this->get($offset);
}

/**
* Sets the value at the given offset.
*
* @param int|string $offset
* @param Model $value
* @return void
*/
public function offsetSet(mixed $offset, mixed $value): void
{
$this->set($offset, $value);
}

/**
* Unsets the value at the given offset.
*
* @param int|string $offset
* @return void
*/
public function offsetUnset(mixed $offset): void
{
unset($this->attributes[$offset]);
Expand Down

0 comments on commit a8fe1f7

Please sign in to comment.