Completed
Push — master ( b785b5...60f4f4 )
by Andreas
17:14
created

get_title_property_nonstatic()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * @package midcom.helper.reflector
4
 * @author The Midgard Project, http://www.midgard-project.org
5
 * @copyright The Midgard Project, http://www.midgard-project.org
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
use midgard\portable\storage\connection;
10
use midgard\portable\api\mgdobject;
11
12
/**
13
 * The Grand Unified Reflector
14
 *
15
 * @package midcom.helper.reflector
16
 */
17
class midcom_helper_reflector extends midcom_baseclasses_components_purecode
18
{
19
    public $mgdschema_class;
20
21
    /**
22
     * @var midgard_reflection_property
23
     */
24
    protected $_mgd_reflector;
25
26
    protected $_dummy_object;
27
28
    private static $_cache = [
29
        'l10n' => [],
30
        'instance' => [],
31
        'title' => [],
32
        'name' => [],
33
        'fieldnames' => [],
34
        'object_icon_map' => null,
35
        'create_icon_map' => null
36
    ];
37
38
    /**
39
     * Constructor, takes classname or object, resolved MgdSchema root class automagically
40
     *
41
     * @param string|mgdobject $src classname or object
42
     */
43 192
    public function __construct($src)
44
    {
45 192
        parent::__construct();
46
47
        // Resolve root class name
48 192
        $this->mgdschema_class = self::resolve_baseclass($src);
49
        // Could not resolve root class name
50 192
        if (empty($this->mgdschema_class)) {
51
            // Handle object vs string
52
            $original_class = (is_object($src)) ? get_class($src) : $src;
53
            throw new midcom_error("Could not determine MgdSchema baseclass for '{$original_class}'");
54
        }
55
56
        // Instantiate midgard reflector
57 192
        $this->_mgd_reflector = new midgard_reflection_property($this->mgdschema_class);
58
59
        // Instantiate dummy object
60 192
        $this->_dummy_object = new $this->mgdschema_class;
61 192
    }
62
63
    /**
64
     * Get cached reflector instance
65
     *
66
     * @param mixed $src Object or classname
67
     * @return static
68
     */
69 389
    public static function &get($src)
70
    {
71 389
        $identifier = get_called_class() . (is_object($src) ? get_class($src) : $src);
72
73 389
        if (!isset(self::$_cache['instance'][$identifier])) {
74 37
            self::$_cache['instance'][$identifier] = new static($src);
75
        }
76 389
        return self::$_cache['instance'][$identifier];
77
    }
78
79
    /**
80
     * Get object's (mgdschema) fieldnames.
81
     *
82
     * @param object $object Object The object to query
83
     */
84 108
    public static function get_object_fieldnames($object) : array
85
    {
86 108
        if (!is_object($object)) {
87
            throw new midcom_error('Invalid parameter type');
88
        }
89 108
        $classname = get_class($object);
90 108
        $metadata = false;
91
92 108
        if (midcom::get()->dbclassloader->is_midcom_db_object($object)) {
93 98
            $classname = $object->__mgdschema_class_name__;
94 89
        } elseif ($object instanceof midcom_helper_metadata) {
95 79
            $metadata = true;
96 79
            $classname = $object->object->__mgdschema_class_name__;
97
        }
98
99 108
        if (is_subclass_of($classname, mgdobject::class)) {
100 108
            $cm = connection::get_em()->getClassMetadata($classname);
101 108
            return $cm->get_schema_properties($metadata);
0 ignored issues
show
introduced by
The method get_schema_properties() does not exist on Doctrine\ORM\Mapping\ClassMetadata. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

101
            return $cm->/** @scrutinizer ignore-call */ get_schema_properties($metadata);
Loading history...
102
        }
103 5
        return array_keys(get_object_vars($object));
104
    }
105
106
    /**
107
     * @param string $property
108
     * @param boolean $metadata
109
     */
110 149
    public function property_exists($property, $metadata = false) : bool
111
    {
112 149
        return $this->_mgd_reflector->property_exists($property, $metadata);
113
    }
114
115
    /**
116
     * Gets a midcom_helper_l10n instance for component governing the type
117
     */
118 67
    public function get_component_l10n() : midcom_services_i18n_l10n
119
    {
120 67
        if (!isset(self::$_cache['l10n'][$this->mgdschema_class])) {
121 11
            if ($component = midcom::get()->dbclassloader->get_component_for_class($this->mgdschema_class)) {
0 ignored issues
show
Bug introduced by
It seems like $this->mgdschema_class can also be of type null; however, parameter $classname of midcom_services_dbclassl...t_component_for_class() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

121
            if ($component = midcom::get()->dbclassloader->get_component_for_class(/** @scrutinizer ignore-type */ $this->mgdschema_class)) {
Loading history...
122 10
                self::$_cache['l10n'][$this->mgdschema_class] = $this->_i18n->get_l10n($component);
123
            } else {
124 1
                debug_add("Could not resolve component for class {$this->mgdschema_class}, using our own l10n", MIDCOM_LOG_INFO);
125 1
                self::$_cache['l10n'][$this->mgdschema_class] = $this->_l10n;
126
            }
127
        }
128
129 67
        return self::$_cache['l10n'][$this->mgdschema_class];
130
    }
131
132
    /**
133
     * Get the localized label of the class
134
     *
135
     * @todo remove any hardcoded class names/prefixes
136
     */
137 67
    public function get_class_label() : string
138
    {
139 67
        $component_l10n = $this->get_component_l10n();
140 67
        $use_classname = $this->mgdschema_class;
141
142 67
        $midcom_dba_classname = midcom::get()->dbclassloader->get_midcom_class_name_for_mgdschema_object($use_classname);
143
144 67
        if (!empty($midcom_dba_classname)) {
145 67
            $use_classname = $midcom_dba_classname;
146
        }
147
148 67
        $use_classname = preg_replace('/_(db|dba)$/', '', $use_classname);
149
150 67
        $label = $component_l10n->get($use_classname);
151 67
        if ($label == $use_classname) {
152
            // Class string not localized, try Bergie's way to pretty-print
153 67
            $classname_parts = explode('_', $use_classname);
154 67
            if (count($classname_parts) >= 3) {
155
                // Drop first two parts of class name
156 67
                array_shift($classname_parts);
157 67
                array_shift($classname_parts);
158
            }
159
            // FIXME: Remove hardcoded class prefixes
160 67
            $use_label = preg_replace('/(openpsa|notifications)_/', '', implode('_', $classname_parts));
161
162 67
            $use_label = str_replace('_', ' ', $use_label);
163 67
            $label = $component_l10n->get($use_label);
164 67
            if ($use_label == $label) {
165 62
                $label = ucwords($use_label);
166
            }
167
        }
168 67
        return $label;
169
    }
170
171
    /**
172
     * Get property name to use as label
173
     *
174
     * @return string name of property to use as label (or false on failure)
175
     */
176 25
    public function get_label_property()
177
    {
178 25
        $midcom_class = midcom::get()->dbclassloader->get_midcom_class_name_for_mgdschema_object($this->mgdschema_class);
179 25
        $obj = ($midcom_class) ? new $midcom_class : new $this->mgdschema_class;
180
181 25
        if (method_exists($obj, 'get_label_property')) {
182 2
            return $obj->get_label_property();
183
        }
184 23
        if (midcom::get()->dbfactory->is_a($obj, midcom_db_person::class)) {
185 1
            return ['rname', 'id'];
186
        }
187 22
        return $this->get_title_property_nonstatic($obj) ??
188 6
            $this->get_name_property_nonstatic($obj) ??
189 22
            'guid';
190
    }
191
192
    /**
193
     * Get the object label property value
194
     *
195
     * @param mixed $object    MgdSchema object
196
     */
197 55
    public function get_object_label($object) : ?string
198
    {
199 55
        if ($object instanceof mgdobject) {
200
            try {
201 2
                $obj = midcom::get()->dbfactory->convert_midgard_to_midcom($object);
202
            } catch (midcom_error $e) {
203 2
                return null;
204
            }
205
        } else {
206 53
            $obj = $object;
207
        }
208 55
        if (method_exists($obj, 'get_label')) {
209 43
            return $obj->get_label();
210
        }
211
212 14
        $properties = array_flip($obj->get_properties());
213 14
        if (empty($properties)) {
214
            debug_add("Could not list object properties, aborting", MIDCOM_LOG_ERROR);
215
            return null;
216
        }
217 14
        if (isset($properties['title'])) {
218 12
            return $obj->title;
219
        }
220 3
        if (isset($properties['name'])) {
221 2
            return $obj->name;
222
        }
223 1
        if ($obj->id > 0) {
224
            return $this->get_class_label() . ' #' . $obj->id;
225
        }
226 1
        return '';
227
    }
228
229
    /**
230
     * Get the name of the create icon image
231
     *
232
     * @param string $type  Name of the type
233
     */
234 28
    public static function get_create_icon($type) : string
235
    {
236 28
        if (null === self::$_cache['create_icon_map']) {
237 1
            self::$_cache['create_icon_map'] = self::_get_icon_map('create_type_magic', 'file-o');
238
        }
239
240 28
        $icon_callback = [$type, 'get_create_icon'];
241
        switch (true) {
242
            // class has static method to tell us the answer ? great !
243 28
            case (is_callable($icon_callback)):
244
                $icon = call_user_func($icon_callback);
245
                break;
246
            // configuration icon
247 28
            case (isset(self::$_cache['create_icon_map'][$type])):
248 16
                $icon = self::$_cache['create_icon_map'][$type];
249 16
                break;
250
251
            // heuristics magic (instead of adding something here, take a look at config key "create_type_magic")
252 25
            case (str_contains($type, 'member')):
253 25
            case (str_contains($type, 'organization')):
254 3
                $icon = 'users';
255 3
                break;
256 24
            case (str_contains($type, 'person')):
257 1
                $icon = 'user-o';
258 1
                break;
259 23
            case (str_contains($type, 'event')):
260 1
                $icon = 'calendar-o';
261 1
                break;
262
263
            // Fallback default value
264
            default:
265 22
                $icon = self::$_cache['create_icon_map']['__default__'];
266 22
                break;
267
        }
268 28
        return $icon;
269
    }
270
271
    /**
272
     * Get the object icon
273
     *
274
     * @param mixed $obj          MgdSchema object
275
     */
276 12
    public static function get_object_icon($obj) : string
277
    {
278 12
        if (null === self::$_cache['object_icon_map']) {
279 1
            self::$_cache['object_icon_map'] = self::_get_icon_map('object_icon_magic', 'file');
280
        }
281
282 12
        $object_class = get_class($obj);
283 12
        $object_baseclass = self::resolve_baseclass($obj);
284
285
        switch (true) {
286
            // object knows it's icon, how handy!
287 12
            case (method_exists($obj, 'get_icon')):
288 4
                $icon = $obj->get_icon();
289 4
                break;
290
291
            // configuration icon
292 9
            case (isset(self::$_cache['object_icon_map'][$object_class])):
293 2
                $icon = self::$_cache['object_icon_map'][$object_class];
294 2
                break;
295 8
            case (isset(self::$_cache['object_icon_map'][$object_baseclass])):
296 2
                $icon = self::$_cache['object_icon_map'][$object_baseclass];
297 2
                break;
298
299
            // heuristics magic (instead of adding something here, take a look at config key "object_icon_magic")
300 7
            case (str_contains($object_class, 'person')):
301 3
                $icon = 'user';
302 3
                break;
303 6
            case (str_contains($object_class, 'event')):
304 1
                $icon = 'calendar-o';
305 1
                break;
306 5
            case (str_contains($object_class, 'member')):
307 5
            case (str_contains($object_class, 'organization')):
308 5
            case (str_contains($object_class, 'group')):
309 3
                $icon = 'users';
310 3
                break;
311 4
            case (str_contains($object_class, 'element')):
312 1
                $icon = 'file-code-o';
313 1
                break;
314
315
            // Fallback default value
316
            default:
317 3
                $icon = self::$_cache['object_icon_map']['__default__'];
318 3
                break;
319
        }
320
321 12
        return '<i class="fa fa-' . $icon . '"></i>';
322
    }
323
324 2
    private static function _get_icon_map(string $config_key, string $fallback) : array
325
    {
326 2
        $config = midcom_baseclasses_components_configuration::get('midcom.helper.reflector', 'config');
327 2
        $icons2classes = $config->get($config_key);
328 2
        $icon_map = [];
329
        //sanity
330 2
        if (!is_array($icons2classes)) {
331
            throw new midcom_error('Config key "' . $config_key . '" is not an array');
332
        }
333 2
        foreach ($icons2classes as $icon => $classes) {
334 2
            $icon_map = array_merge($icon_map, array_fill_keys($classes, $icon));
335
        }
336 2
        if (!isset($icon_map['__default__'])) {
337
            $icon_map['__default__'] = $fallback;
338
        }
339 2
        return $icon_map;
340
    }
341
342
    /**
343
     * Get class properties to use as search fields in choosers or other direct DB searches
344
     */
345 15
    public function get_search_properties() : array
346
    {
347
        // Return cached results if we have them
348 15
        static $cache = [];
349 15
        if (isset($cache[$this->mgdschema_class])) {
350 9
            return $cache[$this->mgdschema_class];
351
        }
352 7
        debug_add("Starting analysis for class {$this->mgdschema_class}");
353
354 7
        $properties = self::get_object_fieldnames($this->_dummy_object);
355
356
        $default_properties = [
357 7
            'title' => true,
358
            'tag' => true,
359
            'firstname' => true,
360
            'lastname' => true,
361
            'official' => true,
362
            'username' => true,
363
        ];
364
365 7
        $search_properties = array_intersect_key($default_properties, array_flip($properties));
366
367 7
        foreach ($properties as $property) {
368 7
            if (str_contains($property, 'name')) {
369 6
                $search_properties[$property] = true;
370
            }
371
            // TODO: More per property heuristics
372
        }
373
        // TODO: parent and up heuristics
374
375 7
        $label_prop = $this->get_label_property();
376
377 7
        if (    is_string($label_prop)
378 7
             && $label_prop != 'guid'
379 7
             && $this->_mgd_reflector->property_exists($label_prop)) {
380 6
            $search_properties[$label_prop] = true;
381
        }
382
383
        // Exceptions - always search these fields
384 7
        $always_search_all = $this->_config->get('always_search_fields') ?: [];
385 7
        if (!empty($always_search_all[$this->mgdschema_class])) {
386 1
            $fields = array_intersect($always_search_all[$this->mgdschema_class], $properties);
387 1
            $search_properties += array_flip($fields);
388
        }
389
390
        // Exceptions - never search these fields
391 7
        $never_search_all = $this->_config->get('never_search_fields') ?: [];
392 7
        if (!empty($never_search_all[$this->mgdschema_class])) {
393
            $search_properties = array_diff_key($search_properties, array_flip($never_search_all[$this->mgdschema_class]));
394
        }
395
396 7
        $search_properties = array_keys($search_properties);
397 7
        debug_print_r("Search properties for {$this->mgdschema_class}: ", $search_properties);
398 7
        $cache[$this->mgdschema_class] = $search_properties;
399 7
        return $search_properties;
400
    }
401
402
    /**
403
     * Gets a list of link properties and the links target info
404
     *
405
     * Link info key specification
406
     *     'class' string link target class name
407
     *     'target' string link target property (of target class)
408
     *     'parent' boolean link is link to "parent" in object tree
409
     *     'up' boolean link is link to "up" in object tree
410
     *
411
     * @return array multidimensional array keyed by property, values are arrays with link info (or false in case of failure)
412
     */
413 4
    public function get_link_properties() : array
414
    {
415
        // Return cached results if we have them
416 4
        static $cache = [];
417 4
        if (isset($cache[$this->mgdschema_class])) {
418 1
            return $cache[$this->mgdschema_class];
419
        }
420 3
        debug_add("Starting analysis for class {$this->mgdschema_class}");
421
422
        // Shorthands
423 3
        $ref = $this->_mgd_reflector;
424 3
        $obj = $this->_dummy_object;
425
426
        // Get property list and start checking (or abort on error)
427 3
        $properties = self::get_object_fieldnames($obj);
428
429 3
        $links = [];
430 3
        $parent_property = midgard_object_class::get_property_parent($this->mgdschema_class);
0 ignored issues
show
Bug introduced by
It seems like $this->mgdschema_class can also be of type null; however, parameter $classname of midgard_object_class::get_property_parent() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

430
        $parent_property = midgard_object_class::get_property_parent(/** @scrutinizer ignore-type */ $this->mgdschema_class);
Loading history...
431 3
        $up_property = midgard_object_class::get_property_up($this->mgdschema_class);
0 ignored issues
show
Bug introduced by
It seems like $this->mgdschema_class can also be of type null; however, parameter $classname of midgard_object_class::get_property_up() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

431
        $up_property = midgard_object_class::get_property_up(/** @scrutinizer ignore-type */ $this->mgdschema_class);
Loading history...
432 3
        foreach ($properties as $property) {
433 3
            if ($property == 'guid') {
434
                // GUID, even though of type MGD_TYPE_GUID, is never a link
435 3
                continue;
436
            }
437
438 3
            if (   !$ref->is_link($property)
439 3
                && $ref->get_midgard_type($property) != MGD_TYPE_GUID) {
440 3
                continue;
441
            }
442 3
            debug_add("Processing property '{$property}'");
443
            $linkinfo = [
444 3
                'class' => null,
445
                'target' => null,
446
                'parent' => false,
447
                'up' => false,
448 3
                'type' => $ref->get_midgard_type($property),
449
            ];
450 3
            if ($parent_property === $property) {
451 2
                debug_add("Is 'parent' property");
452 2
                $linkinfo['parent'] = true;
453
            }
454 3
            if ($up_property === $property) {
455 1
                debug_add("Is 'up' property");
456 1
                $linkinfo['up'] = true;
457
            }
458
459 3
            $type = $ref->get_link_name($property);
460 3
            debug_add("get_link_name returned '{$type}'");
461 3
            if (!empty($type)) {
462 2
                $linkinfo['class'] = $type;
463
            }
464
465 3
            $target = $ref->get_link_target($property);
466
467 3
            debug_add("get_link_target returned '{$target}'");
468 3
            if (!empty($target)) {
469 2
                $linkinfo['target'] = $target;
470 1
            } elseif ($linkinfo['type'] == MGD_TYPE_GUID) {
471 1
                $linkinfo['target'] = 'guid';
472
            }
473
474 3
            $links[$property] = $linkinfo;
475
        }
476
477 3
        debug_print_r("Links for {$this->mgdschema_class}: ", $links);
478 3
        $cache[$this->mgdschema_class] = $links;
479 3
        return $links;
480
    }
481
482
    /**
483
     * Map extended classes
484
     *
485
     * For example org.openpsa.* components often expand core objects,
486
     * in config we specify which classes we wish to substitute with which
487
     *
488
     * @param string $schema_type classname to check rewriting for
489
     * @return string new classname (or original in case no rewriting is to be done)
490
     */
491 39
    public static function class_rewrite(string $schema_type) : string
492
    {
493 39
        static $extends = false;
494 39
        if ($extends === false) {
495
            $extends = midcom_baseclasses_components_configuration::get('midcom.helper.reflector', 'config')->get('class_extends');
496
            // Safety against misconfiguration
497
            if (!is_array($extends)) {
498
                throw new midcom_error("config->get('class_extends') did not return array, invalid configuration ??");
499
            }
500
        }
501 39
        if (   isset($extends[$schema_type])
502 39
            && class_exists($extends[$schema_type])) {
503 2
            return $extends[$schema_type];
504
        }
505 38
        return $schema_type;
506
    }
507
508
    /**
509
     * See if two MgdSchema classes are the same
510
     *
511
     * NOTE: also takes into account the various extended class scenarios
512
     *
513
     * @param string $class_one first class to compare
514
     * @param string $class_two second class to compare
515
     */
516 12
    public static function is_same_class($class_one, $class_two) : bool
517
    {
518 12
        $one = self::resolve_baseclass($class_one);
519 12
        $two = self::resolve_baseclass($class_two);
520 12
        return $one == $two;
521
    }
522
523
    /**
524
     * Get the MgdSchema classname for given class
525
     *
526
     * @param mixed $classname either string (class name) or object
527
     * @return string the base class name
528
     */
529 226
    public static function resolve_baseclass($classname) : ?string
530
    {
531 226
        static $cached = [];
532
533 226
        if (is_object($classname)) {
534 182
            $class_instance = $classname;
535 182
            $classname = get_class($classname);
536
        }
537
538 226
        if (empty($classname)) {
539
            return null;
540
        }
541
542 226
        if (isset($cached[$classname])) {
543 205
            return $cached[$classname];
544
        }
545
546 37
        if (!isset($class_instance)) {
547 10
            $class_instance = new $classname();
548
        }
549
550
        // Check for decorators first
551 37
        if (!empty($class_instance->__mgdschema_class_name__)) {
552 30
            $parent_class = $class_instance->__mgdschema_class_name__;
553 30
            if (   !empty($class_instance->__object)
554 30
                && !$class_instance->__object instanceof $class_instance->__mgdschema_class_name__) {
555
                $parent_class = get_class($class_instance->__object);
556 30
                debug_add('mgdschema object class ' . $parent_class . ' is not an instance of ' . $class_instance->__mgdschema_class_name__, MIDCOM_LOG_INFO);
557
            }
558
        } else {
559 8
            $parent_class = $classname;
560
        }
561
562 37
        $cached[$classname] = self::class_rewrite($parent_class);
563
564 37
        return $cached[$classname];
565
    }
566
567
    /**
568
     * Resolve the "name" property of given object
569
     *
570
     * @see midcom_helper_reflector::get_name_property()
571
     * @param object $object the object to get the name property for
572
     * @todo when midgard_reflection_property supports flagging name fields use that instead of heuristics
573
     */
574 304
    public function get_name_property_nonstatic($object) : ?string
575
    {
576 304
        return $this->get_property('name', $object);
577
    }
578
579 383
    private function get_property(string $type, $object) : ?string
580
    {
581
        // Cache results per class within request
582 383
        $key = get_class($object);
583 383
        if (array_key_exists($key, self::$_cache[$type])) {
584 367
            return self::$_cache[$type][$key];
585
        }
586 55
        self::$_cache[$type][$key] = null;
587
588
        // Configured properties
589 55
        $exceptions = $this->_config->get($type . '_exceptions');
590 55
        foreach ($exceptions as $class => $property) {
591 55
            if (midcom::get()->dbfactory->is_a($object, $class)) {
592 8
                if (   $property !== false
593 8
                    && !$this->_mgd_reflector->property_exists($property)) {
594
                    debug_add("Matched class '{$key}' to '{$class}' via is_a but property '{$property}' does not exist", MIDCOM_LOG_ERROR);
595
                } else {
596 8
                    self::$_cache[$type][$key] = $property;
597
                }
598 8
                return self::$_cache[$type][$key];
599
            }
600
        }
601
        // The simple heuristic
602 51
        if ($this->_mgd_reflector->property_exists($type)) {
603 24
            self::$_cache[$type][$key] = $type;
604
        }
605 51
        return self::$_cache[$type][$key];
606
    }
607
608
    /**
609
     * Resolve the "name" property of given object
610
     *
611
     * @see midcom_helper_reflector::get_name_property_nonstatic()
612
     * @param object $object the object to get the name property for
613
     */
614 301
    public static function get_name_property($object) : ?string
615
    {
616 301
        return self::get($object)->get_name_property_nonstatic($object);
617
    }
618
619
    /**
620
     * Resolve the "title" of given object
621
     *
622
     * NOTE: This is distinctly different from get_object_label, which will always return something
623
     * even if it's just the class name and GUID, also it will for some classes include extra info (like datetimes)
624
     * which we do not want here.
625
     *
626
     * @param object $object the object to get the name property for
627
     * @param string $title_property property to use as "name", if left to default (null), will be reflected
628
     */
629 180
    public static function get_object_title($object, ?string $title_property = null) : ?string
630
    {
631 180
        if ($title_property === null) {
632 180
            $title_property = self::get_title_property($object);
633
        }
634 180
        if (empty($title_property)) {
635
            // Could not resolve valid property
636 3
            return null;
637
        }
638
639 177
        return (string) $object->{$title_property};
640
    }
641
642
    /**
643
     * Resolve the "title" property of given object
644
     *
645
     * NOTE: This is distinctly different from get_label_property, which will always return something
646
     * even if it's just the guid
647
     *
648
     * @param object $object The object to get the title property for
649
     */
650 202
    public static function get_title_property($object) : ?string
651
    {
652 202
        return self::get($object)->get_title_property_nonstatic($object);
653
    }
654
655
    /**
656
     * Resolve the "title" property of given object
657
     *
658
     * NOTE: This is distinctly different from get_label_property, which will always return something
659
     * even if it's just the guid
660
     *
661
     * @see midcom_helper_reflector::get_object_title()
662
     * @param object $object the object to get the title property for
663
     */
664 216
    public function get_title_property_nonstatic($object) : ?string
665
    {
666 216
        return $this->get_property('title', $object);
667
    }
668
}
669