for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php declare(strict_types=1);
/**
* @copyright (c) 2017-present brian ridley
* @author brian ridley <[email protected]>
* @license http://opensource.org/licenses/MIT MIT
*/
namespace ptlis\GrepDb\Replace\Strategy;
use ptlis\GrepDb\Metadata\MySQL\ColumnMetadata;
use ptlis\GrepDb\Replace\Result\FieldReplaceResult;
use ptlis\SerializedDataEditor\Editor;
* Perform replacement on a PHP-serialized string.
final class SerializedFieldReplaceStrategy implements FieldReplaceStrategy
{
* @inheritdoc
public function canReplace(string $searchTerm, string $subject): bool
$editor = new Editor();
try {
return $editor->containsCount($subject, $searchTerm) > 0;
} catch (\Throwable $e) {
return false;
}
public function replace(
ColumnMetadata $columnMetadata,
string $searchTerm,
string $replaceTerm,
string $subject
): FieldReplaceResult {
$matchCount = $editor->containsCount($subject, $searchTerm);
$errorList = [];
if (0 === $matchCount) {
$errorList[] = 'Search term "' . $searchTerm . '" not found in subject "' . $subject . '"';
return new FieldReplaceResult(
$columnMetadata,
$matchCount,
$errorList,
$subject,
$editor->replace($subject, $searchTerm, $replaceTerm)
);
0,
['Failed to deserialize field'],
$subject