Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add comments, fix code on HasIterator and TypeClassification #26

Merged
merged 8 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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