-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create new Base object and Collection is now extending it and final.
- Loading branch information
Showing
30 changed files
with
168 additions
and
202 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,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace drupol\collection; | ||
|
||
use drupol\collection\Contract\Base as BaseInterface; | ||
use drupol\collection\Iterator\ClosureIterator; | ||
|
||
/** | ||
* Class Base. | ||
*/ | ||
abstract class Base implements BaseInterface | ||
{ | ||
/** | ||
* @var \Closure | ||
*/ | ||
protected $source; | ||
|
||
/** | ||
* Base constructor. | ||
* | ||
* @param \Closure|iterable|mixed $data | ||
*/ | ||
public function __construct($data = []) | ||
{ | ||
switch (true) { | ||
case $data instanceof \Closure: | ||
$this->source = $data; | ||
|
||
break; | ||
case \is_iterable($data): | ||
$this->source = static function () use ($data) { | ||
foreach ($data as $k => $v) { | ||
yield $k => $v; | ||
} | ||
}; | ||
|
||
break; | ||
|
||
default: | ||
$this->source = static function () use ($data) { | ||
foreach ((array) $data as $k => $v) { | ||
yield $k => $v; | ||
} | ||
}; | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getIterator(): ClosureIterator | ||
{ | ||
return new ClosureIterator($this->source); | ||
} | ||
|
||
/** | ||
* @param array $data | ||
* | ||
* @return \drupol\collection\Contract\Base | ||
*/ | ||
public static function with($data = []): BaseInterface | ||
{ | ||
return new static($data); | ||
} | ||
} |
Oops, something went wrong.