This repository has been archived by the owner on Mar 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jamison Valenta
committed
Sep 2, 2022
1 parent
71be468
commit a07d641
Showing
31 changed files
with
18,662 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
namespace Tightenco\Collect\Conditionable; | ||
|
||
class HigherOrderWhenProxy | ||
{ | ||
/** | ||
* The target being conditionally operated on. | ||
* | ||
* @var mixed | ||
*/ | ||
protected $target; | ||
|
||
/** | ||
* The condition for proxying. | ||
* | ||
* @var bool | ||
*/ | ||
protected $condition; | ||
|
||
/** | ||
* Indicates whether the proxy has a condition. | ||
* | ||
* @var bool | ||
*/ | ||
protected $hasCondition = false; | ||
|
||
/** | ||
* Determine whether the condition should be negated. | ||
* | ||
* @var bool | ||
*/ | ||
protected $negateConditionOnCapture; | ||
|
||
/** | ||
* Create a new proxy instance. | ||
* | ||
* @param mixed $target | ||
* @return void | ||
*/ | ||
public function __construct($target) | ||
{ | ||
$this->target = $target; | ||
} | ||
|
||
/** | ||
* Set the condition on the proxy. | ||
* | ||
* @param bool $condition | ||
* @return $this | ||
*/ | ||
public function condition($condition) | ||
{ | ||
[$this->condition, $this->hasCondition] = [$condition, true]; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Indicate that the condition should be negated. | ||
* | ||
* @return $this | ||
*/ | ||
public function negateConditionOnCapture() | ||
{ | ||
$this->negateConditionOnCapture = true; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Proxy accessing an attribute onto the target. | ||
* | ||
* @param string $key | ||
* @return mixed | ||
*/ | ||
public function __get($key) | ||
{ | ||
if (! $this->hasCondition) { | ||
$condition = $this->target->{$key}; | ||
|
||
return $this->condition($this->negateConditionOnCapture ? ! $condition : $condition); | ||
} | ||
|
||
return $this->condition | ||
? $this->target->{$key} | ||
: $this->target; | ||
} | ||
|
||
/** | ||
* Proxy a method call on the target. | ||
* | ||
* @param string $method | ||
* @param array $parameters | ||
* @return mixed | ||
*/ | ||
public function __call($method, $parameters) | ||
{ | ||
if (! $this->hasCondition) { | ||
$condition = $this->target->{$method}(...$parameters); | ||
|
||
return $this->condition($this->negateConditionOnCapture ? ! $condition : $condition); | ||
} | ||
|
||
return $this->condition | ||
? $this->target->{$method}(...$parameters) | ||
: $this->target; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Tightenco\Collect\Contracts\Support; | ||
|
||
/** | ||
* @template TKey of array-key | ||
* @template TValue | ||
*/ | ||
interface Arrayable | ||
{ | ||
/** | ||
* Get the instance as an array. | ||
* | ||
* @return array<TKey, TValue> | ||
*/ | ||
public function toArray(); | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Collect/Contracts/Support/CanBeEscapedWhenCastToString.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Tightenco\Collect\Contracts\Support; | ||
|
||
interface CanBeEscapedWhenCastToString | ||
{ | ||
/** | ||
* Indicate that the object's string representation should be escaped when __toString is invoked. | ||
* | ||
* @param bool $escape | ||
* @return $this | ||
*/ | ||
public function escapeWhenCastingToString($escape = true); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Tightenco\Collect\Contracts\Support; | ||
|
||
interface Htmlable | ||
{ | ||
/** | ||
* Get content as a string of HTML. | ||
* | ||
* @return string | ||
*/ | ||
public function toHtml(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Tightenco\Collect\Contracts\Support; | ||
|
||
interface Jsonable | ||
{ | ||
/** | ||
* Convert the object to its JSON representation. | ||
* | ||
* @param int $options | ||
* @return string | ||
*/ | ||
public function toJson($options = 0); | ||
} |
Oops, something went wrong.