PHP code example of sweikenb / dirty
1. Go to this page and download the library: Download sweikenb/dirty library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sweikenb / dirty example snippets
$categoryId = 'category:123';
$categoryData = [
'title' => 'My Category',
'tags' => ['Foo', 'Bar', 'Baz'],
'createdAt' => '2024-07-10 15:31:00'
];
$service = new \Sweikenb\Library\Dirty\Service\DirtyCheckService();
$result = $service->execute($categoryId, $categoryData);
if ($result->isDirty) {
foreach ($result->diffs as $fieldPath => $diff) {
echo sprintf("Field '%s' is dirty! '%s' -> '%s'\n", $fieldPath, $diff->previously, $diff->currently);
}
}
$userId = 'user:123';
$userData = [
'username' => 'some-user'
'security' => [
'password' => '...',
'passwordSalt' => '...',
'pgp-key' => '...'
]
'meta' => [
'source' => 'sso'
'createdAt' => '2024-07-10 15:41:10'
]
];
$config = new \Sweikenb\Library\Dirty\Model\ConfigModel(ignoreFieldPath: [
'security', // will ignore the whole "security" subset
'meta.createdAt' // will only ignore the "createdAt" field under "meta"
]);
$service = new \Sweikenb\Library\Dirty\Service\DirtyCheckService();
$result = $service->execute($userId, $userData, $config);
if ($result->isDirty) {
foreach ($result->diffs as $fieldPath => $diff) {
echo sprintf("Field '%s' is dirty! '%s' -> '%s'\n", $fieldPath, $diff->previously, $diff->currently);
}
}
$userId = 'user:123';
$userData = [
'username' => 'some-user'
'security' => [
'password' => '...',
'passwordSalt' => '...',
'pgp-key' => '...',
]
'meta' => [
'source' => 'sso'
'createdAt' => '2024-07-10 15:41:10'
]
];
$config = new \Sweikenb\Library\Dirty\Model\ConfigModel([
'username', // check the "username" field
'meta', // check the "meta" field with all containing sub-fields
]);
$service = new \Sweikenb\Library\Dirty\Service\DirtyCheckService();
$result = $service->execute($userId, $userData, $config);
if ($result->isDirty) {
foreach ($result->diffs as $fieldPath => $diff) {
echo sprintf("Field '%s' is dirty! '%s' -> '%s'\n", $fieldPath, $diff->previously, $diff->currently);
}
}
$userId = 'user:123';
$userData = [
'username' => 'some-user'
'security' => [
'password' => '...',
'passwordSalt' => '...',
'pgp-key' => '...',
]
'meta' => [
'source' => 'sso'
'createdAt' => '2024-07-10 15:41:10'
]
];
$config = new \Sweikenb\Library\Dirty\Model\ConfigModel(
[
'username', // check the "username" field
'meta', // check the "meta" field with all containing sub-fields
],
[
'meta.createdAt', // ignore the "createdAt" sub-field even tough "meta" was explicitly configured to be checked
]
);
$service = new \Sweikenb\Library\Dirty\Service\DirtyCheckService();
$result = $service->execute($userId, $userData, $config);
if ($result->isDirty) {
foreach ($result->diffs as $fieldPath => $diff) {
echo sprintf("Field '%s' is dirty! '%s' -> '%s'\n", $fieldPath, $diff->previously, $diff->currently);
}
}