Issues (109)

schema/Mark.php (3 issues)

1
<?php
2
3
namespace 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
 */
11
class Mark implements \JsonSerializable
12
{
13
    /** @var  string The type of this mark */
14
    protected $type;
15
16
    /** @var array The attributes associated with this mark */
17
    protected $attrs = [];
18
19
    /**
20
     * Mark constructor.
21
     *
22
     * @param string $type
23
     */
24
    public function __construct($type)
25
    {
26
        $this->type = $type;
27
    }
28
29
    /**
30
     * @param string $key   Attribute key to get or set
31
     * @param null   $value Attribute value to set, null to get
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $value is correct as it would always require null to be passed?
Loading history...
32
     *
33
     * @return $this|mixed Either the wanted value or the Mark itself
34
     */
35
    public function attr($key, $value = null)
36
    {
37
        if (is_null($value)) {
0 ignored issues
show
The condition is_null($value) is always true.
Loading history...
38
            if (isset($this->attrs[$key])) {
39
                return $this->attrs[$key];
40
            } else {
41
                return null;
42
            }
43
        }
44
45
        $this->attrs[$key] = $value;
46
        return $this;
47
    }
48
49
    /**
50
     * Specify data which should be serialized to JSON
51
     *
52
     * @link  http://php.net/manual/en/jsonserializable.jsonserialize.php
53
     * @return mixed data which can be serialized by <b>json_encode</b>,
54
     * which is a value of any type other than a resource.
55
     * @since 5.4.0
56
     */
57
    public function jsonSerialize()
58
    {
59
        $json = [
60
            'type' => $this->type,
61
        ];
62
        if ($this->attrs) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->attrs of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
63
            $json['attrs'] = $this->attrs;
64
        }
65
66
        return $json;
67
    }
68
}
69