|
| 1 | +# sweikenb/dirty |
| 2 | + |
| 3 | +Library for checking if an object or array has changes (is dirty) since the last check. |
| 4 | + |
| 5 | +License: MIT |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +```bash |
| 10 | +composer require sweikenb/dirty |
| 11 | +``` |
| 12 | + |
| 13 | +If you plan to use this library in a **Symfony** project, consider checking out the corresponding |
| 14 | +[DirtyBundle](https://github.com/sweikenb/dirty-bundle) instead. |
| 15 | + |
| 16 | +## Usage |
| 17 | + |
| 18 | +**How does it work?** |
| 19 | + |
| 20 | +In order to check if the test-subject has untracked changes, the given object or array will be normalized, flattened and |
| 21 | +stored using a configurable storage adapter. |
| 22 | + |
| 23 | +The next time the check is executed the current data will be compared to the data of the previous check. |
| 24 | +Which of the subjects fields will be tracked or ignored can be [configured](#configuration) (see below). |
| 25 | + |
| 26 | +**Usage:** |
| 27 | + |
| 28 | +As the result of the check, you will receive a detailed list of fields that changed and the corresponding previous and |
| 29 | +current value: |
| 30 | + |
| 31 | +```php |
| 32 | +$categoryId = 'category:123'; |
| 33 | +$categoryData = [ |
| 34 | + 'title' => 'My Category', |
| 35 | + 'tags' => ['Foo', 'Bar', 'Baz'], |
| 36 | + 'createdAt' => '2024-07-10 15:31:00' |
| 37 | +]; |
| 38 | + |
| 39 | +$service = new \Sweikenb\Library\Dirty\Service\DirtyCheckService(); |
| 40 | + |
| 41 | +$result = $service->execute($categoryId, $categoryData); |
| 42 | + |
| 43 | +if ($result->isDirty) { |
| 44 | + foreach ($result->diffs as $fieldPath => $diff) { |
| 45 | + echo sprintf("Field '%s' is dirty! '%s' -> '%s'\n", $fieldPath, $diff->previously, $diff->currently); |
| 46 | + } |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +### Configuration |
| 51 | + |
| 52 | +In some cases you might have data-structures that contain volatile values (e.g. dynamic timestamps) that will always |
| 53 | +trigger a false-positiv for the dirty-check: |
| 54 | + |
| 55 | +#### Ignore fields |
| 56 | + |
| 57 | +If you want to **ignore** certain fields, you can specify which fields should be ignored during the check. If the |
| 58 | +configured fields contain complex data _(object or array)_ the affected field and all of it subsequent data will be |
| 59 | +**ignored** _(the field acts like a **wildcard**)_: |
| 60 | + |
| 61 | +```php |
| 62 | +$userId = 'user:123'; |
| 63 | +$userData = [ |
| 64 | + 'username' => 'some-user' |
| 65 | + 'security' => [ |
| 66 | + 'password' => '...', |
| 67 | + 'passwordSalt' => '...', |
| 68 | + 'pgp-key' => '...' |
| 69 | + ] |
| 70 | + 'meta' => [ |
| 71 | + 'source' => 'sso' |
| 72 | + 'createdAt' => '2024-07-10 15:41:10' |
| 73 | + ] |
| 74 | +]; |
| 75 | + |
| 76 | +$config = new \Sweikenb\Library\Dirty\Model\ConfigModel(ignoreFieldPath: [ |
| 77 | + 'security', // will ignore the whole "security" subset |
| 78 | + 'meta.createdAt' // will only ignore the "createdAt" field under "meta" |
| 79 | +]); |
| 80 | + |
| 81 | +$service = new \Sweikenb\Library\Dirty\Service\DirtyCheckService(); |
| 82 | + |
| 83 | +$result = $service->execute($userId, $userData, $config); |
| 84 | + |
| 85 | +if ($result->isDirty) { |
| 86 | + foreach ($result->diffs as $fieldPath => $diff) { |
| 87 | + echo sprintf("Field '%s' is dirty! '%s' -> '%s'\n", $fieldPath, $diff->previously, $diff->currently); |
| 88 | + } |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +#### Check only certain fields |
| 93 | + |
| 94 | +You can also explicitly allow fields that should be checked, any other fields will be ignored. If the |
| 95 | +configured fields contain complex data _(object or array)_ the affected field and all of it subsequent data will be |
| 96 | +**checked** _(the field acts like a **wildcard**)_: |
| 97 | + |
| 98 | +```php |
| 99 | +$userId = 'user:123'; |
| 100 | +$userData = [ |
| 101 | + 'username' => 'some-user' |
| 102 | + 'security' => [ |
| 103 | + 'password' => '...', |
| 104 | + 'passwordSalt' => '...', |
| 105 | + 'pgp-key' => '...', |
| 106 | + ] |
| 107 | + 'meta' => [ |
| 108 | + 'source' => 'sso' |
| 109 | + 'createdAt' => '2024-07-10 15:41:10' |
| 110 | + ] |
| 111 | +]; |
| 112 | + |
| 113 | +$config = new \Sweikenb\Library\Dirty\Model\ConfigModel([ |
| 114 | + 'username', // check the "username" field |
| 115 | + 'meta', // check the "meta" field with all containing sub-fields |
| 116 | +]); |
| 117 | + |
| 118 | +$service = new \Sweikenb\Library\Dirty\Service\DirtyCheckService(); |
| 119 | + |
| 120 | +$result = $service->execute($userId, $userData, $config); |
| 121 | + |
| 122 | +if ($result->isDirty) { |
| 123 | + foreach ($result->diffs as $fieldPath => $diff) { |
| 124 | + echo sprintf("Field '%s' is dirty! '%s' -> '%s'\n", $fieldPath, $diff->previously, $diff->currently); |
| 125 | + } |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +#### Combine check and ignore fields |
| 130 | + |
| 131 | +Please note that the "ignore"-configuration will be applied after the "allow"-configuration, that means you can combine |
| 132 | +them to enable certain structures and then explicitly remove a single field or subset from it: |
| 133 | + |
| 134 | +```php |
| 135 | +$userId = 'user:123'; |
| 136 | +$userData = [ |
| 137 | + 'username' => 'some-user' |
| 138 | + 'security' => [ |
| 139 | + 'password' => '...', |
| 140 | + 'passwordSalt' => '...', |
| 141 | + 'pgp-key' => '...', |
| 142 | + ] |
| 143 | + 'meta' => [ |
| 144 | + 'source' => 'sso' |
| 145 | + 'createdAt' => '2024-07-10 15:41:10' |
| 146 | + ] |
| 147 | +]; |
| 148 | + |
| 149 | +$config = new \Sweikenb\Library\Dirty\Model\ConfigModel( |
| 150 | + [ |
| 151 | + 'username', // check the "username" field |
| 152 | + 'meta', // check the "meta" field with all containing sub-fields |
| 153 | + ], |
| 154 | + [ |
| 155 | + 'meta.createdAt', // ignore the "createdAt" sub-field even tough "meta" was explicitly configured to be checked |
| 156 | + ] |
| 157 | +); |
| 158 | + |
| 159 | +$service = new \Sweikenb\Library\Dirty\Service\DirtyCheckService(); |
| 160 | + |
| 161 | +$result = $service->execute($userId, $userData, $config); |
| 162 | + |
| 163 | +if ($result->isDirty) { |
| 164 | + foreach ($result->diffs as $fieldPath => $diff) { |
| 165 | + echo sprintf("Field '%s' is dirty! '%s' -> '%s'\n", $fieldPath, $diff->previously, $diff->currently); |
| 166 | + } |
| 167 | +} |
| 168 | +``` |
| 169 | + |
| 170 | +## Storage Adapters |
| 171 | + |
| 172 | +Storage adapters and their primary use-cases: |
| 173 | + |
| 174 | +* **Filesystem Adapter** _(default)_ |
| 175 | + * local **development** or stage environments |
| 176 | + * single-server setups |
| 177 | + * low amounts of data to check |
| 178 | + * this adapter is **NOT RECOMMENDED** to be used with a network storage mount and highly benefits from a fast |
| 179 | + underlying storage _(e.g. SSD)_ |
| 180 | + * files will not be cleaned up automatically, you need to write your own script for that! |
| 181 | +* **REDIS Adapter** _(or compatible such as "ValKey")_ |
| 182 | + * **Symfony** applications via [DirtyBundle](https://github.com/sweikenb/dirty-bundle) |
| 183 | + * **production** or stage environments |
| 184 | + * multi-server setups |
| 185 | + * any data-set size |
| 186 | + |
| 187 | +You can add custom storage adapters if needed by implementing the `Sweikenb\Library\Dirty\Api\StorageAdapterInterface`. |
| 188 | + |
| 189 | +## Configuration and customization |
| 190 | + |
| 191 | +* _TODO_ |
0 commit comments