1: <?php
2: /**
3: * The Horde_Kolab_Storage_Folder_Namespace_Element:: class represents a namespace type.
4: *
5: * PHP version 5
6: *
7: * @category Kolab
8: * @package Kolab_Storage
9: * @author Gunnar Wrobel <wrobel@pardus.de>
10: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
11: * @link http://pear.horde.org/index.php?package=Kolab_Storage
12: */
13:
14: /**
15: * The Horde_Kolab_Storage_Folder_Namespace_Element:: class represents a namespace type.
16: *
17: * Copyright 2010-2012 Horde LLC (http://www.horde.org/)
18: *
19: * See the enclosed file COPYING for license information (LGPL). If you
20: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
21: *
22: * @category Kolab
23: * @package Kolab_Storage
24: * @author Gunnar Wrobel <wrobel@pardus.de>
25: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
26: * @link http://pear.horde.org/index.php?package=Kolab_Storage
27: */
28: abstract class Horde_Kolab_Storage_Folder_Namespace_Element
29: {
30: /**
31: * The prefix identifying this namespace.
32: *
33: * @var string
34: */
35: protected $_name;
36:
37: /**
38: * The delimiter used for this namespace.
39: *
40: * @var string
41: */
42: protected $_delimiter;
43:
44: /**
45: * The current user.
46: *
47: * @var string
48: */
49: protected $_user;
50:
51: /**
52: * Constructor.
53: *
54: * @param string $name The prefix identifying this namespace.
55: * @param string $delimiter The delimiter used for this namespace.
56: * @param string $user The current user.
57: */
58: public function __construct($name, $delimiter, $user)
59: {
60: if (substr($name, -1) == $delimiter) {
61: $name = substr($name, 0, -1);
62: }
63: $this->_name = $name;
64: $this->_delimiter = $delimiter;
65: $this->_user = $user;
66: }
67:
68: /**
69: * Return the type of this namespace (personal, other, or shared).
70: *
71: * @return string The type.
72: */
73: abstract public function getType();
74:
75: /**
76: * Return the name of this namespace.
77: *
78: * @return string The name/prefix.
79: */
80: public function getName()
81: {
82: return $this->_name;
83: }
84:
85: /**
86: * Return the delimiter for this namespace.
87: *
88: * @return string The delimiter.
89: */
90: public function getDelimiter()
91: {
92: return $this->_delimiter;
93: }
94:
95: /**
96: * Does the folder name lie in this namespace?
97: *
98: * @param string $name The name of the folder.
99: *
100: * @return boolean True if the folder is element of this namespace.
101: */
102: public function matches($name)
103: {
104: return (strpos($name, $this->_name) === 0);
105: }
106:
107: /**
108: * Return the owner of a folder.
109: *
110: * @param string $name The name of the folder.
111: *
112: * @return string|boolean The owner of the folder.
113: */
114: abstract public function getOwner($name);
115:
116: /**
117: * Return the title of a folder.
118: *
119: * @param string $name The name of the folder.
120: *
121: * @return string The title of the folder.
122: */
123: public function getTitle($name)
124: {
125: $subpath = $this->_subpath($name);
126: if (!empty($subpath)) {
127: return array_pop($subpath);
128: } else {
129: return '';
130: }
131: }
132:
133: /**
134: * Get the sub path for the given folder name.
135: *
136: * @param string $name The folder name.
137: *
138: * @return string The sub path.
139: */
140: public function getSubpath($name)
141: {
142: return join($this->_subpath($name), $this->_delimiter);
143: }
144:
145: /**
146: * Get the parent for the given folder name.
147: *
148: * @param string $name The parent folder name.
149: *
150: * @return string The parent.
151: */
152: public function getParent($name)
153: {
154: $path = explode($this->_delimiter, $name);
155: array_pop($path);
156: return join($path, $this->_delimiter);
157: }
158:
159: /**
160: * Return an array describing the path elements of the folder.
161: *
162: * @param string $name The name of the folder.
163: *
164: * @return array The path elements.
165: */
166: protected function _subpath($name)
167: {
168: $path = explode($this->_delimiter, $name);
169: if ($path[0] == $this->_name) {
170: array_shift($path);
171: }
172: //@todo: What about the potential trailing domain?
173: return $path;
174: }
175:
176: /**
177: * Generate a folder path for the given path in this namespace.
178: *
179: * @param array $path The path of the folder.
180: *
181: * @return string The name of the folder.
182: */
183: public function generateName($path)
184: {
185: if (!empty($this->_name)) {
186: array_unshift($path, $this->_name);
187: }
188: return join($path, $this->_delimiter);
189: }
190:
191: /**
192: * Generate a folder path for the given subpath and owner.
193: *
194: * @since Horde_Kolab_Storage 1.1.0
195: *
196: * @param string $subpath The subpath of the folder.
197: * @param string $owner The folder owner.
198: *
199: * @return string The name of the folder.
200: */
201: public function generatePath($subpath, $owner)
202: {
203: return empty($this->_name) ? $subpath : $this->_name . $this->_delimiter . $subpath;
204: }
205:
206: /**
207: * Convert the namespace description to a string.
208: *
209: * @return string The namespace description.
210: */
211: public function __toString()
212: {
213: return '"' . $this->_name . '" (' . $this->getType() . ', "' . $this->_delimiter . '")';
214: }
215: }