Skip to content

Commit

Permalink
Remove comparison mode and value already exists mode
Browse files Browse the repository at this point in the history
  • Loading branch information
steevanb committed May 15, 2024
1 parent 0e045f6 commit 5444ece
Show file tree
Hide file tree
Showing 26 changed files with 67 additions and 737 deletions.
73 changes: 12 additions & 61 deletions src/AbstractCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Steevanb\PhpCollection\{
Exception\KeyNotFoundException,
Exception\ReadOnlyException,
Exception\ValueAlreadyExistsException,
ScalarCollection\IntegerCollection,
ScalarCollection\StringCollection
};
Expand All @@ -18,16 +17,16 @@
*/
abstract class AbstractCollection implements CollectionInterface
{
abstract protected function assertValueType(mixed $value): static;

/** @var array<TValueType> */
protected array $values = [];

protected bool $readOnly = false;

/** @param iterable<TValueType> $values */
public function __construct(
iterable $values = [],
private readonly ValueAlreadyExistsModeEnum $valueAlreadyExistsMode = ValueAlreadyExistsModeEnum::ADD
) {
public function __construct(iterable $values = [])
{
$this->replace($values);
}

Expand Down Expand Up @@ -80,11 +79,6 @@ public function isReadOnly(): bool
return $this->readOnly;
}

public function getValueAlreadyExistsMode(): ValueAlreadyExistsModeEnum
{
return $this->valueAlreadyExistsMode;
}

/** @return array<int, string|int> */
public function getKeys(): array
{
Expand Down Expand Up @@ -139,11 +133,11 @@ public function toArray(): array
/** @param TValueType $value */
public function set(string|int $key, mixed $value): static
{
$this->assertIsNotReadOnly();
$this
->assertIsNotReadOnly()
->assertValueType($value);

if ($this->canAddValue($value)) {
$this->values[$key] = $value;
}
$this->values[$key] = $value;

return $this;
}
Expand Down Expand Up @@ -182,11 +176,11 @@ public function contains(mixed $value): bool
/** @param TValueType $value */
public function add(mixed $value): static
{
$this->assertIsNotReadOnly();
$this
->assertIsNotReadOnly()
->assertValueType($value);

if ($this->canAddValue($value)) {
$this->values[] = $value;
}
$this->values[] = $value;

return $this;
}
Expand All @@ -205,47 +199,4 @@ protected function assertIsNotReadOnly(): static

return $this;
}

/**
* @param TValueType $firstValue
* @param TValueType $secondValue
*/
protected function isSameValues(mixed $firstValue, mixed $secondValue): bool
{
return $firstValue === $secondValue;
}

/** @param TValueType $value */
protected function castValueToString(mixed $value): string
{
return is_null($value) ? 'NULL' : (string) $value;
}

protected function canAddValue(mixed $value): bool
{
$return = true;

if (
in_array(
$this->getValueAlreadyExistsMode(),
[ValueAlreadyExistsModeEnum::DO_NOT_ADD, ValueAlreadyExistsModeEnum::EXCEPTION],
true
)
) {
foreach ($this->values as $internalValue) {
if ($this->isSameValues($value, $internalValue)) {
if ($this->getValueAlreadyExistsMode() === ValueAlreadyExistsModeEnum::EXCEPTION) {
throw new ValueAlreadyExistsException(
'Value "' . $this->castValueToString($value) . '" already exists.'
);
}

$return = false;
break;
}
}
}

return $return;
}
}
2 changes: 0 additions & 2 deletions src/CollectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ public function setReadOnly(): static;

public function isReadOnly(): bool;

public function getValueAlreadyExistsMode(): ValueAlreadyExistsModeEnum;

/** @return array<string|int, TValueType> */
public function toArray(): array;
}
9 changes: 0 additions & 9 deletions src/Exception/ValueAlreadyExistsException.php

This file was deleted.

36 changes: 6 additions & 30 deletions src/ObjectCollection/AbstractObjectCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@

namespace Steevanb\PhpCollection\ObjectCollection;

use Steevanb\PhpCollection\{
AbstractCollection,
Exception\PhpCollectionException,
ValueAlreadyExistsModeEnum
};
use Steevanb\PhpCollection\AbstractCollection;

/**
* @template TValueType of object
Expand All @@ -23,40 +19,20 @@ abstract class AbstractObjectCollection extends AbstractCollection
abstract public static function getValueFqcn(): string;

/** @param iterable<TValueType> $values */
public function __construct(
iterable $values = [],
private readonly ComparisonModeEnum $comparisonMode = ComparisonModeEnum::HASH,
ValueAlreadyExistsModeEnum $valueAlreadyExistsMode = ValueAlreadyExistsModeEnum::ADD
) {
parent::__construct($values, $valueAlreadyExistsMode);
public function __construct(iterable $values = [])
{
parent::__construct($values);
}

protected function getAssertInstanceOfError(mixed $value): string
{
return 'Value should be an instance of ' . static::getValueFqcn() . ', ' . get_debug_type($value) . ' given.';
}

protected function canAddValue(mixed $value): bool
protected function assertValueType(mixed $value): static
{
$this->assertInstanceOf($value);

return parent::canAddValue($value);
}

protected function castValueToString(mixed $value): string
{
if ($value instanceof \BackedEnum) {
$return = (string) $value->value;
} elseif ($value instanceof \UnitEnum) {
$return = $value->name;
} elseif ($value instanceof \Stringable === false) {
throw new PhpCollectionException(
'Error while converting an instance of ' . $value::class . ' to string. Add __toString() to do it.'
);
} else {
$return = parent::castValueToString($value);
}

return $return;
return $this;
}
}
36 changes: 6 additions & 30 deletions src/ObjectCollection/AbstractObjectNullableCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@

namespace Steevanb\PhpCollection\ObjectCollection;

use Steevanb\PhpCollection\{
AbstractCollection,
Exception\PhpCollectionException,
ValueAlreadyExistsModeEnum
};
use Steevanb\PhpCollection\AbstractCollection;

/**
* @template TValueType of object|null
Expand All @@ -23,12 +19,9 @@ abstract class AbstractObjectNullableCollection extends AbstractCollection
abstract public static function getValueFqcn(): string;

/** @param iterable<TValueType> $values */
public function __construct(
iterable $values = [],
private readonly ComparisonModeEnum $comparisonMode = ComparisonModeEnum::HASH,
ValueAlreadyExistsModeEnum $valueAlreadyExistsMode = ValueAlreadyExistsModeEnum::ADD
) {
parent::__construct($values, $valueAlreadyExistsMode);
public function __construct(iterable $values = [])
{
parent::__construct($values);
}

protected function getAssertInstanceOfError(mixed $value): string
Expand All @@ -38,29 +31,12 @@ protected function getAssertInstanceOfError(mixed $value): string
. get_debug_type($value) . ' given.';
}

protected function canAddValue(mixed $value): bool
protected function assertValueType(mixed $value): static
{
if (is_null($value) === false) {
$this->assertInstanceOf($value);
}

return parent::canAddValue($value);
}

protected function castValueToString(mixed $value): string
{
if ($value instanceof \BackedEnum) {
$return = (string) $value->value;
} elseif ($value instanceof \UnitEnum) {
$return = $value->name;
} elseif (is_object($value) && $value instanceof \Stringable === false) {
throw new PhpCollectionException(
'Error while converting an instance of ' . $value::class . ' to string. Add __toString() to do it.'
);
} else {
$return = parent::castValueToString($value);
}

return $return;
return $this;
}
}
11 changes: 0 additions & 11 deletions src/ObjectCollection/ComparisonModeEnum.php

This file was deleted.

41 changes: 1 addition & 40 deletions src/ObjectCollection/ObjectCollectionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

namespace Steevanb\PhpCollection\ObjectCollection;

use Steevanb\PhpCollection\{
Exception\InvalidTypeException,
Exception\PhpCollectionException
};
use Steevanb\PhpCollection\Exception\InvalidTypeException;

/** @template TValueType of object|null */
trait ObjectCollectionTrait
Expand All @@ -17,11 +14,6 @@ abstract public static function getValueFqcn(): string;

abstract protected function getAssertInstanceOfError(mixed $value): string;

public function getComparisonMode(): ComparisonModeEnum
{
return $this->comparisonMode;
}

protected function assertInstanceOf(mixed $value): static
{
if (
Expand All @@ -34,35 +26,4 @@ protected function assertInstanceOf(mixed $value): static

return $this;
}

/**
* @param TValueType $firstValue
* @param TValueType $secondValue
*/
protected function isSameValues(mixed $firstValue, mixed $secondValue): bool
{
if ($this->getComparisonMode() === ComparisonModeEnum::STRING) {
$return = parent::isSameValues(
// @phpstan-ignore-next-line This code will be removed in #142
$this->castValueToString($firstValue),
// @phpstan-ignore-next-line This code will be removed in #142
$this->castValueToString($secondValue)
);
/**
* He is right, this if is useless for now, but if one day we add a value I prefer throw the exception
* @phpstan-ignore-next-line
*/
} elseif ($this->getComparisonMode() === ComparisonModeEnum::HASH) {
$return = parent::isSameValues(
// @phpstan-ignore-next-line This code will be removed in #142
is_object($firstValue) ? spl_object_hash($firstValue) : null,
// @phpstan-ignore-next-line This code will be removed in #142
is_object($secondValue) ? spl_object_hash($secondValue) : null
);
} else {
throw new PhpCollectionException('Unknown comparison mode "' . $this->getComparisonMode()->value . '".');
}

return $return;
}
}
4 changes: 2 additions & 2 deletions src/ScalarCollection/FloatCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
/** @extends AbstractCollection<float> */
class FloatCollection extends AbstractCollection
{
protected function canAddValue(mixed $value): bool
protected function assertValueType(mixed $value): static
{
if (is_float($value) === false) {
throw new InvalidTypeException('$value should be of type float.');
}

return parent::canAddValue($value);
return $this;
}
}
4 changes: 2 additions & 2 deletions src/ScalarCollection/FloatNullableCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
/** @extends AbstractCollection<float|null> */
class FloatNullableCollection extends AbstractCollection
{
protected function canAddValue(mixed $value): bool
protected function assertValueType(mixed $value): static
{
if (is_null($value) === false && is_float($value) === false) {
throw new InvalidTypeException('$value should be of type float or null.');
}

return parent::canAddValue($value);
return $this;
}
}
4 changes: 2 additions & 2 deletions src/ScalarCollection/IntegerCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
/** @extends AbstractCollection<int> */
class IntegerCollection extends AbstractCollection
{
protected function canAddValue(mixed $value): bool
protected function assertValueType(mixed $value): static
{
if (is_int($value) === false) {
throw new InvalidTypeException('$value should be of type int.');
}

return parent::canAddValue($value);
return $this;
}
}
4 changes: 2 additions & 2 deletions src/ScalarCollection/IntegerNullableCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
/** @extends AbstractCollection<int|null> */
class IntegerNullableCollection extends AbstractCollection
{
protected function canAddValue(mixed $value): bool
protected function assertValueType(mixed $value): static
{
if (is_null($value) === false && is_int($value) === false) {
throw new InvalidTypeException('$value should be of type int or null.');
}

return parent::canAddValue($value);
return $this;
}
}
Loading

0 comments on commit 5444ece

Please sign in to comment.