Skip to content

Commit

Permalink
Fixed bug.
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Parker <[email protected]>
  • Loading branch information
nomadicjosh committed Feb 16, 2025
1 parent 8a27100 commit 507f27c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
11 changes: 5 additions & 6 deletions Collection/BaseCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Qubus\Exception\Data\TypeException;
use Qubus\Support\ArrayHelper;
use Qubus\Support\DataType;
use ReflectionException;

use function count;
Expand Down Expand Up @@ -152,11 +153,10 @@ public function filter(callable $callable): BaseCollection
* @param mixed $key
* @return mixed
* @throws TypeException
* @throws ReflectionException
*/
public function get(mixed $key): mixed
{
return ArrayHelper::getInstance()->get($this->items, $key);
return (new DataType())->array->get($this->items, $key);
}

/**
Expand Down Expand Up @@ -251,11 +251,10 @@ public function values(): static
* flattened array of items.
*
* @return self
* @throws ReflectionException
*/
public function flatten(): static
{
return new static(ArrayHelper::getInstance()->flatten($this->items));
return new static((new DataType())->array->flatten($this->items));
}

/**
Expand Down Expand Up @@ -444,11 +443,11 @@ public function sum(mixed $callback = null): int|float
/**
* Merge items with current collection.
*
* @throws TypeException|ReflectionException
* @throws TypeException
*/
public function merge(array $items): static
{
return new static(ArrayHelper::getInstance()->merge($this->items, $items));
return new static((new DataType())->array->merge($this->items, $items));
}

/**
Expand Down
23 changes: 18 additions & 5 deletions Collection/ValueExtractorAware.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,32 @@ trait ValueExtractorAware
* Extracts the value of the given property or method from the object.
*
* @param mixed $object The object to extract the value from.
* @param string $propertyOrMethod The property or method for which the
* value should be extracted.
* @param string|null $propertyOrMethod The property or method for which the
* value should be extracted.
* @return mixed the value extracted from the specified property or method.
* @throws ValueExtractionException if the method or property is not defined.
*/
protected function extractValue(mixed $object, string $propertyOrMethod): mixed
protected function extractValue(mixed $object, ?string $propertyOrMethod = null): mixed
{
if (!is_object(value: $object)) {
if ($propertyOrMethod === null) {
return $object;
}

if (!is_object(value: $object) && !is_array($object)) {
throw new ValueExtractionException(message: 'Unable to extract a value from a non-object.');
}

if (is_array($object)) {
return $object[$propertyOrMethod] ?? throw new InvalidPropertyOrMethodException(
sprintf(
'Key or index "%s" not found in collection elements',
$propertyOrMethod,
)
);
}

if (property_exists($object, $propertyOrMethod)) {
return $object->$propertyOrMethod;
return $object->{$propertyOrMethod};
}

if (method_exists($object, $propertyOrMethod)) {
Expand Down
1 change: 1 addition & 0 deletions Collection/ValueToStringAware.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ protected function toolValueToString(mixed $value): string
}

// From here, $value should be an object.
assert(is_object($value));

// __toString() is implemented
if (is_callable([$value, '__toString'])) {
Expand Down

0 comments on commit 507f27c

Please sign in to comment.