1: <?php
2: /**
3: * A factory for generating Kolab format handlers.
4: *
5: * PHP version 5
6: *
7: * @category Kolab
8: * @package Kolab_Format
9: * @author Gunnar Wrobel <wrobel@pardus.de>
10: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
11: * @link http://www.horde.org/libraries/Horde_Kolab_Format
12: */
13:
14: /**
15: * A factory for generating Kolab format handlers.
16: *
17: * Copyright 2010-2012 Horde LLC (http://www.horde.org/)
18: *
19: * See the enclosed file COPYING for license information (LGPL). If you did not
20: * receive this file, see
21: * http://www.horde.org/licenses/lgpl21.
22: *
23: * @category Kolab
24: * @package Kolab_Format
25: * @author Gunnar Wrobel <wrobel@pardus.de>
26: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
27: * @link http://www.horde.org/libraries/Horde_Kolab_Format
28: */
29: class Horde_Kolab_Format_Factory
30: {
31: /**
32: * Parameters for the parser construction.
33: *
34: * @var array
35: */
36: private $_params;
37:
38: /**
39: * Constructor.
40: *
41: * @param array $params Additional parameters for the creation of parsers.
42: */
43: public function __construct(array $params = array())
44: {
45: $this->_params = $params;
46: }
47:
48: /**
49: * Generates a handler for a specific Kolab object type.
50: *
51: * @param string $format The format that the handler should work with.
52: * @param string $type The object type that should be handled.
53: * @param array $params Additional parameters.
54: * <pre>
55: * 'version' - The format version.
56: * </pre>
57: *
58: * @return Horde_Kolab_Format The handler.
59: *
60: * @throws Horde_Kolab_Format_Exception If the specified handler does not
61: * exist.
62: */
63: public function create($format = 'Xml', $type = '', array $params = array())
64: {
65: switch ($type) {
66: case 'h-ledger':
67: $type_class = 'Envelope';
68: break;
69: default:
70: $type_class = ucfirst(strtolower(str_replace('-', '', $type)));
71: break;
72: }
73: $parser = ucfirst(strtolower($format));
74: $class = basename('Horde_Kolab_Format_' . $parser . '_' . $type_class);
75:
76: $params = array_merge($this->_params, $params);
77:
78: if (class_exists($class)) {
79: switch ($parser) {
80: case 'Xml':
81: $instance = new $class($this->createXmlParser(), $this, $params);
82: break;
83: default:
84: throw new Horde_Kolab_Format_Exception(
85: sprintf(
86: 'Failed to initialize the specified parser (Parser type %s does not exist)!',
87: $parser
88: )
89: );
90: }
91: } else {
92: throw new Horde_Kolab_Format_Exception(
93: sprintf(
94: 'Failed to load the specified Kolab Format handler (Class %s does not exist)!',
95: $class
96: )
97: );
98: }
99: if (!empty($params['memlog'])) {
100: if (!class_exists('Horde_Support_Memory')) {
101: throw new Horde_Kolab_Format_Exception('The Horde_Support package seems to be missing (Class Horde_Support_Memory is missing)!');
102: }
103: $instance = new Horde_Kolab_Format_Decorator_Memory(
104: $instance,
105: new Horde_Support_Memory(),
106: $params['memlog']
107: );
108: }
109: if (!empty($params['timelog'])) {
110: if (!class_exists('Horde_Support_Timer')) {
111: throw new Horde_Kolab_Format_Exception('The Horde_Support package seems to be missing (Class Horde_Support_Timer is missing)!');
112: }
113: $instance = new Horde_Kolab_Format_Decorator_Timed(
114: $instance,
115: new Horde_Support_Timer(),
116: $params['timelog']
117: );
118: }
119: return $instance;
120: }
121:
122: /**
123: * Generates a XML parser.
124: *
125: * @since Horde_Kolab_Format 1.1.0
126: *
127: * @return Horde_Kolab_Format_Xml_Parser The parser.
128: */
129: public function createXmlParser()
130: {
131: return new Horde_Kolab_Format_Xml_Parser(
132: new DOMDocument('1.0', 'UTF-8')
133: );
134: }
135:
136: /**
137: * Generates a XML helper instance.
138: *
139: * @since Horde_Kolab_Format 1.1.0
140: *
141: * @param DOMDocument $xmldoc The XML document the helper works with.
142: *
143: * @return Horde_Kolab_Format_Xml_Helper The helper utility.
144: */
145: public function createXmlHelper(DOMDocument $xmldoc)
146: {
147: return new Horde_Kolab_Format_Xml_Helper($xmldoc);
148: }
149:
150: /**
151: * Generates a XML type that deals with XML data modifications.
152: *
153: * @since Horde_Kolab_Format 1.1.0
154: *
155: * @param string $type The value type.
156: * @param array $params Additional parameters.
157: *
158: * @return Horde_Kolab_Format_Xml_Type The type.
159: *
160: * @throws Horde_Kolab_Format_Exception If the specified type does not
161: * exist.
162: */
163: public function createXmlType($type, $params = array())
164: {
165: if (isset($params['api-version'])) {
166: $class = $type . '_V' . $params['api-version'];
167: } else {
168: $class = $type;
169: }
170: if (class_exists($class)) {
171: return new $class($this);
172: } else if (class_exists($type)) {
173: return new $type($this);
174: } else {
175: throw new Horde_Kolab_Format_Exception(
176: sprintf('XML type %s not supported!', $type)
177: );
178: }
179: }
180: }
181: