for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace Yiisoft\Arrays\Collection\Modifier;
final class InsertValueBeforeKey implements DataModifierInterface
{
/**
* @var int|string|null
*/
private $key = null;
* @var mixed
private $value = null;
private $beforeKey = null;
* @param int|string $key
* @return self
public function withKey($key): self
$new = clone $this;
$new->key = $key;
return $new;
}
* @param mixed $value
public function setValue($value): self
$new->value = $value;
public function beforeKey($key): self
$new->beforeKey = $key;
public function apply(array $data): array
if ($this->key === null || $this->beforeKey === null) {
return $data;
$result = [];
foreach ($data as $k => $v) {
if ($k === $this->beforeKey) {
$result[$this->key] = $this->value;
$result[$k] = $v;
return $result;