Unfortunately, after updating from zend-hydrator to version 3, our existing classes are no longer functional.
In the methods hydrate () and extract () the strict type "object" throws an error.
Apparently, it is not thought that these classes will be extended. How should we implement this and change our application?
class UserDbHydrator extends ClassMethodsHydrator implements HydratorInterface
{
protected $colPrefix = 'user_';
protected $colIgnoreList = ['username', 'password'];
public function hydrate(array $data, object $object)
{
$array = $this->removePrefix($data, $this->colPrefix);
$hydrated_array = parent::hydrate($array, $object);
return $hydrated_array;
}
public function extract(object $object): array
{
$extracted_array = parent::extract($object);
$array = $this->appendPrefix($extracted_array, $this->colPrefix, $this->colIgnoreList);
return $array;
}
public function appendPrefix($array, $prefix, $ignore)
{
$data = [];
foreach ($array as $key => $value) {
if ((!preg_match('/^' . $prefix . '/', $key)) and (!in_array($key, $ignore))) {
$key = $prefix . $key;
}
$data[$key] = $value;
}
return $data;
}
public function removePrefix($array, $prefix)
{
$data = [];
foreach ($array as $key => $value) {
if (preg_match('/^' . $prefix . '/', $key)) {
$key = preg_replace('/^' . $prefix . '/', '', $key);
}
$data[$key] = $value;
}
return $data;
}
}