-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement HigherOrderWhenProxy (#32148)
- Loading branch information
1 parent
7bb7f02
commit eb0c397
Showing
2 changed files
with
70 additions
and
2 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,63 @@ | ||
<?php | ||
|
||
namespace Illuminate\Support; | ||
|
||
/** | ||
* @mixin \Illuminate\Support\Enumerable | ||
*/ | ||
class HigherOrderWhenProxy | ||
{ | ||
/** | ||
* The collection being operated on. | ||
* | ||
* @var \Illuminate\Support\Enumerable | ||
*/ | ||
protected $collection; | ||
|
||
/** | ||
* The condition for proxying. | ||
* | ||
* @var bool | ||
*/ | ||
protected $condition; | ||
|
||
/** | ||
* Create a new proxy instance. | ||
* | ||
* @param \Illuminate\Support\Enumerable $collection | ||
* @param bool $condition | ||
* @return void | ||
*/ | ||
public function __construct(Enumerable $collection, $condition) | ||
{ | ||
$this->condition = $condition; | ||
$this->collection = $collection; | ||
} | ||
|
||
/** | ||
* Proxy accessing an attribute onto the collection. | ||
* | ||
* @param string $key | ||
* @return mixed | ||
*/ | ||
public function __get($key) | ||
{ | ||
return $this->condition | ||
? $this->collection->{$key} | ||
: $this->collection; | ||
} | ||
|
||
/** | ||
* Proxy a method call onto the collection. | ||
* | ||
* @param string $method | ||
* @param array $parameters | ||
* @return mixed | ||
*/ | ||
public function __call($method, $parameters) | ||
{ | ||
return $this->condition | ||
? $this->collection->{$method}(...$parameters) | ||
: $this->collection; | ||
} | ||
} |
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