Subversion Repositories oidplus

Rev

Rev 1086 | Rev 1121 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1086 Rev 1116
Line 24... Line 24...
24
// phpcs:enable PSR1.Files.SideEffects
24
// phpcs:enable PSR1.Files.SideEffects
25
 
25
 
26
class OIDplusDomain extends OIDplusObject {
26
class OIDplusDomain extends OIDplusObject {
27
        private $domain;
27
        private $domain;
28
 
28
 
-
 
29
        /**
-
 
30
         * @param string $domain
-
 
31
         */
29
        public function __construct($domain) {
32
        public function __construct(string $domain) {
30
                // TODO: syntax checks
33
                // TODO: syntax checks
31
                $this->domain = $domain;
34
                $this->domain = $domain;
32
        }
35
        }
33
 
36
 
-
 
37
        /**
-
 
38
         * @param string $node_id
-
 
39
         * @return OIDplusDomain|null
-
 
40
         */
34
        public static function parse($node_id) {
41
        public static function parse(string $node_id)/*: ?OIDplusDomain*/ {
35
                @list($namespace, $domain) = explode(':', $node_id, 2);
42
                @list($namespace, $domain) = explode(':', $node_id, 2);
36
                if ($namespace !== self::ns()) return false;
43
                if ($namespace !== self::ns()) return null;
37
                return new self($domain);
44
                return new self($domain);
38
        }
45
        }
39
 
46
 
-
 
47
        /**
-
 
48
         * @return string
-
 
49
         */
40
        public static function objectTypeTitle() {
50
        public static function objectTypeTitle(): string {
41
                return _L('Domain Names');
51
                return _L('Domain Names');
42
        }
52
        }
43
 
53
 
-
 
54
        /**
-
 
55
         * @return string
-
 
56
         */
44
        public static function objectTypeTitleShort() {
57
        public static function objectTypeTitleShort(): string {
45
                return _L('Domain');
58
                return _L('Domain');
46
        }
59
        }
47
 
60
 
-
 
61
        /**
-
 
62
         * @return string
-
 
63
         */
48
        public static function ns() {
64
        public static function ns(): string {
49
                return 'domain';
65
                return 'domain';
50
        }
66
        }
51
 
67
 
-
 
68
        /**
-
 
69
         * @return string
-
 
70
         */
52
        public static function root() {
71
        public static function root(): string {
53
                return self::ns().':';
72
                return self::ns().':';
54
        }
73
        }
55
 
74
 
-
 
75
        /**
-
 
76
         * @return bool
-
 
77
         */
56
        public function isRoot() {
78
        public function isRoot(): bool {
57
                return $this->domain == '';
79
                return $this->domain == '';
58
        }
80
        }
59
 
81
 
-
 
82
        /**
-
 
83
         * @param bool $with_ns
-
 
84
         * @return string
-
 
85
         */
60
        public function nodeId($with_ns=true) {
86
        public function nodeId(bool $with_ns=true): string {
61
                return $with_ns ? self::root().$this->domain : $this->domain;
87
                return $with_ns ? self::root().$this->domain : $this->domain;
62
        }
88
        }
63
 
89
 
-
 
90
        /**
-
 
91
         * @param string $str
-
 
92
         * @return string
-
 
93
         * @throws OIDplusException
-
 
94
         */
64
        public function addString($str) {
95
        public function addString(string $str): string {
65
                if ($this->isRoot()) {
96
                if ($this->isRoot()) {
66
                        return self::root().$str;
97
                        return self::root().$str;
67
                } else {
98
                } else {
68
                        if (strpos($str,'.') !== false) throw new OIDplusException(_L('Please only submit one arc.'));
99
                        if (strpos($str,'.') !== false) throw new OIDplusException(_L('Please only submit one arc.'));
69
                        return self::root().$str.'.'.$this->nodeId(false);
100
                        return self::root().$str.'.'.$this->nodeId(false);
70
                }
101
                }
71
        }
102
        }
72
 
103
 
-
 
104
        /**
-
 
105
         * @param OIDplusObject $parent
-
 
106
         * @return string
-
 
107
         */
73
        public function crudShowId(OIDplusObject $parent) {
108
        public function crudShowId(OIDplusObject $parent): string {
74
                return $this->domain;
109
                return $this->domain;
75
        }
110
        }
76
 
111
 
-
 
112
        /**
-
 
113
         * @return string
-
 
114
         * @throws OIDplusException
-
 
115
         */
77
        public function crudInsertSuffix() {
116
        public function crudInsertSuffix(): string {
78
                return $this->isRoot() ? '' : substr($this->addString(''), strlen(self::ns())+1);
117
                return $this->isRoot() ? '' : substr($this->addString(''), strlen(self::ns())+1);
79
        }
118
        }
80
 
119
 
-
 
120
        /**
-
 
121
         * @param OIDplusObject|null $parent
-
 
122
         * @return string
-
 
123
         */
81
        public function jsTreeNodeName(OIDplusObject $parent = null) {
124
        public function jsTreeNodeName(OIDplusObject $parent = null): string {
82
                if ($parent == null) return $this->objectTypeTitle();
125
                if ($parent == null) return $this->objectTypeTitle();
83
                return $this->domain;
126
                return $this->domain;
84
        }
127
        }
85
 
128
 
-
 
129
        /**
-
 
130
         * @return string
-
 
131
         */
86
        public function defaultTitle() {
132
        public function defaultTitle(): string {
87
                return $this->domain;
133
                return $this->domain;
88
        }
134
        }
89
 
135
 
-
 
136
        /**
-
 
137
         * @return bool
-
 
138
         */
90
        public function isLeafNode() {
139
        public function isLeafNode(): bool {
91
                return false;
140
                return false;
92
        }
141
        }
93
 
142
 
-
 
143
        /**
-
 
144
         * @param string $title
-
 
145
         * @param string $content
-
 
146
         * @param string $icon
-
 
147
         * @return void
-
 
148
         * @throws OIDplusException
-
 
149
         */
94
        public function getContentPage(&$title, &$content, &$icon) {
150
        public function getContentPage(string &$title, string &$content, string &$icon) {
95
                $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
151
                $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
96
 
152
 
97
                if ($this->isRoot()) {
153
                if ($this->isRoot()) {
98
                        $title = OIDplusDomain::objectTypeTitle();
154
                        $title = OIDplusDomain::objectTypeTitle();
99
 
155
 
Line 128... Line 184...
128
                                $content .= '%%CRUD%%';
184
                                $content .= '%%CRUD%%';
129
                        }
185
                        }
130
                }
186
                }
131
        }
187
        }
132
 
188
 
-
 
189
        /**
-
 
190
         * @return OIDplusDomain|null
-
 
191
         */
133
        public function one_up() {
192
        public function one_up()/*: ?OIDplusDomain*/ {
134
                $oid = $this->domain;
193
                $oid = $this->domain;
135
 
194
 
136
                $p = strpos($oid, '.');
195
                $p = strpos($oid, '.');
137
                if ($p === false) return self::parse('');
196
                if ($p === false) return self::parse('');
138
 
197
 
139
                $oid_up = substr($oid, $p+1);
198
                $oid_up = substr($oid, $p+1);
140
 
199
 
141
                return self::parse(self::ns().':'.$oid_up);
200
                return self::parse(self::ns().':'.$oid_up);
142
        }
201
        }
143
 
202
 
-
 
203
        /**
-
 
204
         * @param $to
-
 
205
         * @return int|null
-
 
206
         */
144
        public function distance($to) {
207
        public function distance($to) {
145
                if (!is_object($to)) $to = OIDplusObject::parse($to);
208
                if (!is_object($to)) $to = OIDplusObject::parse($to);
146
                if (!($to instanceof $this)) return false;
209
                if (!($to instanceof $this)) return null;
147
 
210
 
148
                $a = $to->domain;
211
                $a = $to->domain;
149
                $b = $this->domain;
212
                $b = $this->domain;
150
 
213
 
151
                if (substr($a,-1) == '.') $a = substr($a,0,strlen($a)-1);
214
                if (substr($a,-1) == '.') $a = substr($a,0,strlen($a)-1);
Line 158... Line 221...
158
                $bry = array_reverse($bry);
221
                $bry = array_reverse($bry);
159
 
222
 
160
                $min_len = min(count($ary), count($bry));
223
                $min_len = min(count($ary), count($bry));
161
 
224
 
162
                for ($i=0; $i<$min_len; $i++) {
225
                for ($i=0; $i<$min_len; $i++) {
163
                        if ($ary[$i] != $bry[$i]) return false;
226
                        if ($ary[$i] != $bry[$i]) return null;
164
                }
227
                }
165
 
228
 
166
                return count($ary) - count($bry);
229
                return count($ary) - count($bry);
167
        }
230
        }
168
 
231
 
-
 
232
        /**
-
 
233
         * @return string
-
 
234
         */
169
        public function getDirectoryName() {
235
        public function getDirectoryName(): string {
170
                if ($this->isRoot()) return $this->ns();
236
                if ($this->isRoot()) return $this->ns();
171
                return $this->ns().'_'.md5($this->nodeId(false));
237
                return $this->ns().'_'.md5($this->nodeId(false));
172
        }
238
        }
173
 
239
 
-
 
240
        /**
-
 
241
         * @param string $mode
-
 
242
         * @return string
-
 
243
         */
174
        public static function treeIconFilename($mode) {
244
        public static function treeIconFilename(string $mode): string {
175
                return 'img/'.$mode.'_icon16.png';
245
                return 'img/'.$mode.'_icon16.png';
176
        }
246
        }
177
}
247
}