InternalLink.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
12 
14 
15 /**
16  * InternalLink contains static methods for handling internal application links.
17  * These links are useful in a scenario, where an object represents a page and
18  * several subobjects represent page elements.
19  *
20  * @author ingo herwig <ingo@wemove.com>
21  */
22 class InternalLink {
23 
24  const PROTOCOL_STR = "link://";
25 
26  /**
27  * Make an internal link to an object
28  * @param $oid The id of the object to link to
29  * @return The link
30  */
31  public static function makeLink(ObjectId $oid) {
32  return self::PROTOCOL_STR.$oid->__toString();
33  }
34 
35  /**
36  * Make an internal link to an object
37  * @param $oid The object id of the object to link to
38  * @param $anchorOID The object id of the subobject to link to
39  * @param $anchorName The name inside the subobject to link to (null, if the object itself should be linked) (default: _null_)
40  * @return The link
41  */
42  public static function makeAnchorLink(ObjectId $oid, ObjectId $anchorOID, $anchorName=null) {
43  $str = self::makeLink($oid)."/".$anchorOID->__toString();
44  if ($anchorName != null) {
45  $str .= "#".$anchorName;
46  }
47  return $str;
48  }
49 
50  /**
51  * Test if a link is an internal link
52  * @param $link The link to test
53  * @return Boolean whether the link is an internal link or not
54  */
55  public static function isLink($link) {
56  return strpos($link, self::PROTOCOL_STR) === 0;
57  }
58 
59  /**
60  * Get the oid of the referenced object
61  * @param $link The link to process
62  * @return The oid or null if no valid oid is referenced
63  */
64  public static function getReferencedOID($link) {
65  preg_match_all("/([A-Za-z0-9]+(:[0-9]+)+)/", $link, $matches);
66  if (sizeof($matches) > 0 && sizeof($matches[1]) > 0) {
67  $oid = $matches[1][0];
68  return ObjectId::parse($oid);
69  }
70  return null;
71  }
72 
73  /**
74  * Get the oid of the referenced subobject if any
75  * @param $link The link to process
76  * @return The oid or null if no anchor is defined
77  */
78  public static function getAnchorOID($link) {
79  preg_match_all("/([A-Za-z0-9]+(:[0-9]+)+)/", $link, $matches);
80  if (sizeof($matches) > 0 && sizeof($matches[1]) > 1) {
81  $oid = $matches[1][1];
82  return ObjectId::parse($oid);
83  }
84  return null;
85  }
86 
87  /**
88  * Get the name of the anchor inside the referenced subobject if any
89  * @param $link The link to process
90  * @return The name or null if no anchor name is defined
91  */
92  public static function getAnchorName($link) {
93  preg_match_all("/#(.+)$/", $link, $matches);
94  if (sizeof($matches) > 0 && sizeof($matches[1]) > 0) {
95  $name = $matches[1][0];
96  return $name;
97  }
98  return null;
99  }
100 }
101 ?>
__toString()
Get a string representation of the object id.
Definition: ObjectId.php:215
ObjectId is the unique identifier of an object.
Definition: ObjectId.php:28
static parse($oid)
Parse a serialized object id string into an ObjectId instance.
Definition: ObjectId.php:135