1<?php
2
3namespace dokuwiki\plugin\prosemirror\schema;
4
5/**
6 * Class Mark
7 *
8 * @package dokuwiki\plugin\prosemirror\schema
9 * @link    http://prosemirror.net/ref.html#model.Mark
10 */
11class Mark implements \JsonSerializable
12{
13
14    /** @var  string The type of this mark */
15    protected $type;
16
17    /** @var array The attributes associated with this mark */
18    protected $attrs = [];
19
20    /**
21     * Mark constructor.
22     *
23     * @param string $type
24     */
25    public function __construct($type)
26    {
27        $this->type = $type;
28    }
29
30    /**
31     * @param string $key   Attribute key to get or set
32     * @param null   $value Attribute value to set, null to get
33     *
34     * @return $this|mixed Either the wanted value or the Mark itself
35     */
36    public function attr($key, $value = null)
37    {
38        if (is_null($value)) {
39            if (isset($this->attrs[$key])) {
40                return $this->attrs[$key];
41            } else {
42                return null;
43            }
44        }
45
46        $this->attrs[$key] = $value;
47        return $this;
48    }
49
50    /**
51     * Specify data which should be serialized to JSON
52     *
53     * @link  http://php.net/manual/en/jsonserializable.jsonserialize.php
54     * @return mixed data which can be serialized by <b>json_encode</b>,
55     * which is a value of any type other than a resource.
56     * @since 5.4.0
57     */
58    function jsonSerialize()
59    {
60        $json = [
61            'type' => $this->type,
62        ];
63        if ($this->attrs) {
64            $json['attrs'] = $this->attrs;
65        }
66
67        return $json;
68    }
69}
70