summaryrefslogtreecommitdiffstats
path: root/library/Icingadb/Model/Behavior/ActionAndNoteUrl.php
blob: e8f6799275b5783374b7eb12e9ab904bb43e4d8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php

/* Icinga DB Web | (c) 2021 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Icingadb\Model\Behavior;

use ipl\Orm\Contract\PropertyBehavior;

class ActionAndNoteUrl extends PropertyBehavior
{
    public function fromDb($value, $key, $_)
    {
        if (empty($value)) {
            return [];
        }

        $links = [];
        if (strpos($value, "' ") === false) {
            $links[] = $value;
        } else {
            foreach (explode("' ", $value) as $url) {
                $url = strpos($url, "'") === 0 ? substr($url, 1) : $url;
                $url = strpos($url, "'") === strlen($url) - 1 ? substr($url, 0, strlen($url) - 1) : $url;
                $links[] = $url;
            }
        }

        return $links;
    }

    public function toDb($value, $key, $_)
    {
        if (empty($value) || ! is_array($value)) {
            return $value;
        }

        if (count($value) === 1) {
            return $value[0];
        }

        $links = '';
        foreach ($value as $url) {
            if (! empty($links)) {
                $links .= ' ';
            }

            $links .= "'$url'";
        }

        return $links;
    }
}