Skip to content

Commit

Permalink
Allow attempted casting to boolean and array (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
indykoning authored Feb 22, 2024
1 parent 4706b98 commit e47af7b
Showing 1 changed file with 73 additions and 1 deletion.
74 changes: 73 additions & 1 deletion src/OptionalDeep.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use ArrayAccess;
use ArrayObject;
use Countable;
use Illuminate\Support\Arr;
use Illuminate\Support\Traits\Macroable;
use IteratorAggregate;
use JsonSerializable;
Expand Down Expand Up @@ -83,7 +84,7 @@ public function isset(): bool

public function __isNotEmpty(): bool
{
if (method_exists($this->value, 'value')) {
if (is_object($this->value) && method_exists($this->value, 'value')) {
return boolval($this->value->value());
}

Expand All @@ -95,6 +96,37 @@ public function __isEmpty(): bool
return !$this->isNotEmpty();
}

/**
* Attempt to cast the optionalDeep object to a boolean.
* This is not a real magic method, but common practice in other frameworks.
* see: https://wiki.php.net/rfc/objects-can-be-falsifiable
*/
public function __toBool(): bool
{
if (is_bool($this->value)) {
return $this->value;
}

if (!$this->isset()) {
return false;
}

if ($this->__isEmpty()) {
return false;
}

if (is_object($this->value) && method_exists($this->value, 'toBool')) {
return $this->value->toBool();
}

if (is_object($this->value) && method_exists($this->value, '__toBool')) {
return $this->value->__toBool();
}

// Is not empty, thus true.
return true;
}

// ArrayAccess interface
public function offsetGet(mixed $offset): mixed
{
Expand All @@ -116,6 +148,37 @@ public function offsetUnset(mixed $offset): void
$this->__unset($offset);
}

/**
* Attempt to cast the optionalDeep object to an array.
* This is not a real magic method, but common practice in other frameworks.
* see: https://wiki.php.net/rfc/to-array
*/
public function __toArray(): array
{
if (is_array($this->value)) {
return $this->value;
}

if ($this->value instanceof \Traversable) {
$return = [];
foreach ($this->value as $key => $value) {
$return[$key] = $value;
}

return $return;
}

if (is_object($this->value) && method_exists($this->value, 'toArray')) {
return $this->value->toArray();
}

if (is_object($this->value) && method_exists($this->value, '__toArray')) {
return $this->value->__toArray();
}

return Arr::wrap($this->value);
}

// IteratorAggregate interface
public function getIterator(): Traversable
{
Expand Down Expand Up @@ -155,6 +218,15 @@ public function __call($method, $parameters): mixed
if ($method == 'isEmpty') {
return $this->__isEmpty();
}
if ($method == 'toBool') {
return $this->__toBool();
}
if ($method == 'toArray') {
return $this->__toArray();
}
if ($method == 'toString') {
return $this->__toString();
}

return new static(null);
}
Expand Down

0 comments on commit e47af7b

Please sign in to comment.