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 OIDplusAid extends OIDplusObject {
26
class OIDplusAid extends OIDplusObject {
27
        private $aid;
27
        private $aid;
28
 
28
 
-
 
29
        /**
-
 
30
         * @param $aid
-
 
31
         */
29
        public function __construct($aid) {
32
        public function __construct($aid) {
30
                // TODO: syntax checks
33
                // TODO: syntax checks
31
                $this->aid = $aid;
34
                $this->aid = $aid;
32
        }
35
        }
33
 
36
 
-
 
37
        /**
-
 
38
         * @param string $node_id
-
 
39
         * @return OIDplusAid|null
-
 
40
         */
34
        public static function parse($node_id) {
41
        public static function parse(string $node_id)/*: ?OIDplusAid*/ {
35
                @list($namespace, $aid) = explode(':', $node_id, 2);
42
                @list($namespace, $aid) = explode(':', $node_id, 2);
36
                if ($namespace !== self::ns()) return false;
43
                if ($namespace !== self::ns()) return null;
37
                return new self($aid);
44
                return new self($aid);
38
        }
45
        }
39
 
46
 
-
 
47
        /**
-
 
48
         * @return string
-
 
49
         */
40
        public static function objectTypeTitle() {
50
        public static function objectTypeTitle(): string {
41
                return _L('Application Identifier (ISO/IEC 7816)');
51
                return _L('Application Identifier (ISO/IEC 7816)');
42
        }
52
        }
43
 
53
 
-
 
54
        /**
-
 
55
         * @return string
-
 
56
         */
44
        public static function objectTypeTitleShort() {
57
        public static function objectTypeTitleShort(): string {
45
                return _L('AID');
58
                return _L('AID');
46
        }
59
        }
47
 
60
 
-
 
61
        /**
-
 
62
         * @return string
-
 
63
         */
48
        public static function ns() {
64
        public static function ns(): string {
49
                return 'aid';
65
                return 'aid';
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->aid == '';
79
                return $this->aid == '';
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->aid : $this->aid;
87
                return $with_ns ? self::root().$this->aid : $this->aid;
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
                $m = array();
96
                $m = array();
66
 
97
 
67
                $str = str_replace(' ','',$str);
98
                $str = str_replace(' ','',$str);
68
                $str = str_replace(':','',$str);
99
                $str = str_replace(':','',$str);
69
 
100
 
Line 92... Line 123...
92
                */
123
                */
93
 
124
 
94
                return $this->nodeId(true).strtoupper($str);
125
                return $this->nodeId(true).strtoupper($str);
95
        }
126
        }
96
 
127
 
-
 
128
        /**
-
 
129
         * @param OIDplusObject $parent
-
 
130
         * @return string
-
 
131
         */
97
        public function crudShowId(OIDplusObject $parent) {
132
        public function crudShowId(OIDplusObject $parent): string {
98
                return $this->chunkedNotation(false);
133
                return $this->chunkedNotation(false);
99
        }
134
        }
100
 
135
 
-
 
136
        /**
-
 
137
         * @return string
-
 
138
         */
101
        public function crudInsertPrefix() {
139
        public function crudInsertPrefix(): string {
102
                return $this->isRoot() ? '' : $this->chunkedNotation(false);
140
                return $this->isRoot() ? '' : $this->chunkedNotation(false);
103
        }
141
        }
104
 
142
 
-
 
143
        /**
-
 
144
         * @param OIDplusObject|null $parent
-
 
145
         * @return string
-
 
146
         */
105
        public function jsTreeNodeName(OIDplusObject $parent = null) {
147
        public function jsTreeNodeName(OIDplusObject $parent = null): string {
106
                if ($parent == null) return $this->objectTypeTitle();
148
                if ($parent == null) return $this->objectTypeTitle();
107
                return substr($this->nodeId(), strlen($parent->nodeId()));
149
                return substr($this->nodeId(), strlen($parent->nodeId()));
108
        }
150
        }
109
 
151
 
-
 
152
        /**
-
 
153
         * @return string
-
 
154
         */
110
        public function defaultTitle() {
155
        public function defaultTitle(): string {
111
                return $this->aid;
156
                return $this->aid;
112
        }
157
        }
113
 
158
 
-
 
159
        /**
-
 
160
         * @return bool
-
 
161
         */
114
        public function isLeafNode() {
162
        public function isLeafNode(): bool {
115
                // We don't know when an AID is "leaf", because an AID can have an arbitary length <= 16 Bytes.
163
                // We don't know when an AID is "leaf", because an AID can have an arbitary length <= 16 Bytes.
116
                // But if it is 16 bytes long (32 nibbles), then we are 100% certain that it is a leaf node.
164
                // But if it is 16 bytes long (32 nibbles), then we are 100% certain that it is a leaf node.
117
                return (strlen($this->nodeId(false)) == 32);
165
                return (strlen($this->nodeId(false)) == 32);
118
        }
166
        }
119
 
167
 
-
 
168
        /**
-
 
169
         * @param string $title
-
 
170
         * @param string $content
-
 
171
         * @param string $icon
-
 
172
         * @return void
-
 
173
         * @throws OIDplusException
-
 
174
         */
120
        public function getContentPage(&$title, &$content, &$icon) {
175
        public function getContentPage(string &$title, string &$content, string &$icon) {
121
                $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
176
                $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
122
 
177
 
123
                if ($this->isRoot()) {
178
                if ($this->isRoot()) {
124
                        $title = OIDplusAid::objectTypeTitle();
179
                        $title = OIDplusAid::objectTypeTitle();
125
 
180
 
Line 166... Line 221...
166
                }
221
                }
167
        }
222
        }
168
 
223
 
169
        # ---
224
        # ---
170
 
225
 
-
 
226
        /**
-
 
227
         * @param $withAbbr
-
 
228
         * @return string
-
 
229
         * @throws OIDplusException
-
 
230
         */
171
        public function chunkedNotation($withAbbr=true) {
231
        public function chunkedNotation($withAbbr=true) {
172
                $curid = self::root().$this->aid;
232
                $curid = self::root().$this->aid;
173
 
233
 
174
                $obj = OIDplusObject::findFitting($curid);
234
                $obj = OIDplusObject::findFitting($curid);
175
                if (!$obj) return $this->aid;
235
                if (!$obj) return $this->aid;
Line 202... Line 262...
202
                        $full[] = $withAbbr && ($hint !== '') ? '<abbr title="'.htmlentities($hint).'">'.$c.'</abbr>' : $c;
262
                        $full[] = $withAbbr && ($hint !== '') ? '<abbr title="'.htmlentities($hint).'">'.$c.'</abbr>' : $c;
203
                }
263
                }
204
                return implode(' ', $full);
264
                return implode(' ', $full);
205
        }
265
        }
206
 
266
 
-
 
267
        /**
-
 
268
         * @return OIDplusAid|null
-
 
269
         */
207
        public function one_up() {
270
        public function one_up()/*: ?OIDplusAid*/ {
208
                return OIDplusObject::parse($this->ns().':'.substr($this->aid,0,strlen($this->aid)-1));
271
                return self::parse($this->ns().':'.substr($this->aid,0,strlen($this->aid)-1));
209
        }
272
        }
210
 
273
 
-
 
274
        /**
-
 
275
         * @param $to
-
 
276
         * @return int|null
-
 
277
         */
211
        public function distance($to) {
278
        public function distance($to) {
212
                if (!is_object($to)) $to = OIDplusObject::parse($to);
279
                if (!is_object($to)) $to = OIDplusObject::parse($to);
213
                if (!($to instanceof $this)) return false;
280
                if (!($to instanceof $this)) return null;
214
 
281
 
215
                $a = $to->aid;
282
                $a = $to->aid;
216
                $b = $this->aid;
283
                $b = $this->aid;
217
 
284
 
218
                $ary = $a;
285
                $ary = $a;
219
                $bry = $b;
286
                $bry = $b;
220
 
287
 
221
                $min_len = min(strlen($ary), strlen($bry));
288
                $min_len = min(strlen($ary), strlen($bry));
222
 
289
 
223
                for ($i=0; $i<$min_len; $i++) {
290
                for ($i=0; $i<$min_len; $i++) {
224
                        if ($ary[$i] != $bry[$i]) return false;
291
                        if ($ary[$i] != $bry[$i]) return null;
225
                }
292
                }
226
 
293
 
227
                return strlen($ary) - strlen($bry);
294
                return strlen($ary) - strlen($bry);
228
        }
295
        }
229
 
296
 
-
 
297
        /**
-
 
298
         * @return array|OIDplusAltId[]
-
 
299
         * @throws OIDplusException
-
 
300
         */
230
        public function getAltIds() {
301
        public function getAltIds(): array {
231
                if ($this->isRoot()) return array();
302
                if ($this->isRoot()) return array();
232
                $ids = parent::getAltIds();
303
                $ids = parent::getAltIds();
233
 
304
 
234
                $aid = $this->nodeId(false);
305
                $aid = $this->nodeId(false);
235
                $aid = strtoupper($aid);
306
                $aid = strtoupper($aid);
Line 395... Line 466...
395
                // TODO: If it has no pix, then resolve it !!! but how do we know if there is a PIX or a part ID ?
466
                // TODO: If it has no pix, then resolve it !!! but how do we know if there is a PIX or a part ID ?
396
 
467
 
397
                return $ids;
468
                return $ids;
398
        }
469
        }
399
 
470
 
-
 
471
        /**
-
 
472
         * @return string
-
 
473
         */
400
        public function getDirectoryName() {
474
        public function getDirectoryName(): string {
401
                if ($this->isRoot()) return $this->ns();
475
                if ($this->isRoot()) return $this->ns();
402
                return $this->ns().'_'.$this->nodeId(false); // safe, because there are only AIDs
476
                return $this->ns().'_'.$this->nodeId(false); // safe, because there are only AIDs
403
        }
477
        }
404
 
478
 
-
 
479
        /**
-
 
480
         * @param string $mode
-
 
481
         * @return string
-
 
482
         */
405
        public static function treeIconFilename($mode) {
483
        public static function treeIconFilename(string $mode): string {
406
                return 'img/'.$mode.'_icon16.png';
484
                return 'img/'.$mode.'_icon16.png';
407
        }
485
        }
408
}
486
}