$arr = ['foo', 'bar', 'baz'];
$arr = array_map( fn( $value ) => $value = '...' . $value, $arr );
// вывод: ['...foo', '...bar', '...baz']
<?php
declare(strict_types=1);
class Ellipsis
{
private string $char;
private int $count;
/**
* @param string $char
* @param int $count
*/
public function __construct(string $char, int $count)
{
$this->char = $char;
$this->count = $count;
}
/**
* @return string
*/
public function getChar(): string
{
return $this->char;
}
/**
* @return int
*/
public function getCount(): int
{
return $this->count;
}
}
class Ellipsisist
{
private Ellipsis $ellipsis;
/**
* @param Ellipsis $ellipsis
*/
public function __construct(Ellipsis $ellipsis)
{
$this->ellipsis = $ellipsis;
}
/**
* @param string $value
* @return string
*/
public function __invoke(string $value): string
{
return str_pad($value, mb_strlen($value) + $this->ellipsis->getCount(), $this->ellipsis->getChar(), STR_PAD_LEFT);
}
}
class EllipsisistFactory
{
/**
* @param string $char
* @param int $count
* @return Ellipsisist
*/
public static function make(string $char, int $count): Ellipsisist
{
return new Ellipsisist(new Ellipsis($char, $count));
}
}
$arr = ['foo', 'bar', 'baz'];
$ellipsisiatedArr = array_map(
EllipsisistFactory::make('.', 3),
$arr
);
var_dump($ellipsisiatedArr);