| 1: | <?php declare(strict_types=1); |
| 2: | |
| 3: | namespace Salient\Iterator; |
| 4: | |
| 5: | use Salient\Contract\Iterator\FluentIteratorInterface; |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | trait FluentIteratorTrait |
| 16: | { |
| 17: | |
| 18: | |
| 19: | |
| 20: | public function toArray(bool $preserveKeys = true): array |
| 21: | { |
| 22: | if ($preserveKeys) { |
| 23: | foreach ($this as $key => $value) { |
| 24: | $array[$key] = $value; |
| 25: | } |
| 26: | } else { |
| 27: | foreach ($this as $value) { |
| 28: | $array[] = $value; |
| 29: | } |
| 30: | } |
| 31: | return $array ?? []; |
| 32: | } |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | public function getFirstWith($key, $value, bool $strict = false) |
| 38: | { |
| 39: | foreach ($this as $current) { |
| 40: | if (is_array($current)) { |
| 41: | if (array_key_exists($key, $current)) { |
| 42: | $_value = $current[$key]; |
| 43: | } else { |
| 44: | continue; |
| 45: | } |
| 46: | } elseif (is_object($current)) { |
| 47: | if (property_exists($current, (string) $key)) { |
| 48: | $_value = $current->{$key}; |
| 49: | } else { |
| 50: | continue; |
| 51: | } |
| 52: | } else { |
| 53: | continue; |
| 54: | } |
| 55: | if ($strict) { |
| 56: | if ($_value === $value) { |
| 57: | return $current; |
| 58: | } |
| 59: | } elseif ($_value == $value) { |
| 60: | return $current; |
| 61: | } |
| 62: | } |
| 63: | return null; |
| 64: | } |
| 65: | } |
| 66: | |