1<?php
2
3namespace ComboStrap;
4
5use action_plugin_combo_metaprocessing;
6use dokuwiki\Extension\Event;
7
8class MetadataMutation
9{
10    /**
11     * When the value of a metadata has changed, an event is created
12     */
13    public const PAGE_METADATA_MUTATION_EVENT = "PAGE_METADATA_MUTATION_EVENT";
14    public const NEW_VALUE_ATTRIBUTE = "new_value";
15    const OLD_VALUE_ATTRIBUTE = "old_value";
16    const NAME_ATTRIBUTE = "name";
17    const PATH_ATTRIBUTE = PagePath::PROPERTY_NAME;
18
19    /**
20     *
21     * Metadata modification can happen:
22     * * on the whole set (ie after rendering the meta on references for instance)
23     * * or for scalar mutation
24     *
25     * This function is then used in two places.
26     *
27     * @param string $attribute
28     * @param $valueBefore
29     * @param $valueAfter
30     * @param Path $wikiPath
31     * @return void
32     *
33     * TODO: The data is now store dependent
34     *   * Can we also pass the store to decode
35     *   * or do we pass just the objects
36     */
37    public static function notifyMetadataMutation(string $attribute, $valueBefore, $valueAfter, Path $wikiPath)
38    {
39        if ($valueAfter !== $valueBefore) {
40            /**
41             * Event
42             */
43            $eventData = [
44                self::NAME_ATTRIBUTE => $attribute,
45                self::NEW_VALUE_ATTRIBUTE => $valueAfter,
46                self::OLD_VALUE_ATTRIBUTE => $valueBefore,
47                self::PATH_ATTRIBUTE => $wikiPath->toAbsoluteId()
48            ];
49            Event::createAndTrigger(self::PAGE_METADATA_MUTATION_EVENT, $eventData);
50        }
51    }
52}
53