Skip to content

Commit

Permalink
Merged develop into master.
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
2 parents 79956d2 + 40dcd55 commit 794cff2
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 15 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
11 changes: 11 additions & 0 deletions Collection/InvalidPropertyOrMethodException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Qubus\Support\Collection;

use RuntimeException;

class InvalidPropertyOrMethodException extends RuntimeException
{
}
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
8 changes: 4 additions & 4 deletions DataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

namespace Qubus\Support;

/**
* @property ArrayHelper $array
* @property StringHelper $string
*/
class DataType implements DataObjectCollection
{
/**
Expand All @@ -22,10 +26,6 @@ class DataType implements DataObjectCollection
*/
protected array $instances = [];

/**
* @property ArrayHelper $array
* @property StringHelper $string
*/
public function __construct()
{
$this->add('array', new ArrayHelper());
Expand Down

0 comments on commit 794cff2

Please sign in to comment.