PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/Kolab_Format/lib/Horde/Kolab/Format/Factory.php

https://github.com/sgtcarneiro/horde
PHP | 154 lines | 84 code | 6 blank | 64 comment | 9 complexity | 6990edef11ce128161ccd59a846df297 MD5 | raw file
  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.fsf.org/copyleft/lgpl.html LGPL
  11. * @link http://pear.horde.org/index.php?package=Kolab_Format
  12. */
  13. /**
  14. * A factory for generating Kolab format handlers.
  15. *
  16. * Copyright 2010-2011 The Horde Project (http://www.horde.org/)
  17. *
  18. * See the enclosed file COPYING for license information (LGPL). If you did not
  19. * receive this file, see
  20. * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  21. *
  22. * @category Kolab
  23. * @package Kolab_Format
  24. * @author Gunnar Wrobel <wrobel@pardus.de>
  25. * @license http://www.fsf.org/copyleft/lgpl.html LGPL
  26. * @link http://pear.horde.org/index.php?package=Kolab_Format
  27. */
  28. class Horde_Kolab_Format_Factory
  29. {
  30. /**
  31. * Parameters for the parser construction.
  32. *
  33. * @var array
  34. */
  35. private $_params;
  36. /**
  37. * Constructor.
  38. *
  39. * @param array $params Additional parameters for the creation of parsers.
  40. */
  41. public function __construct(array $params = array())
  42. {
  43. $this->_params = $params;
  44. }
  45. /**
  46. * Generates a handler for a specific Kolab object type.
  47. *
  48. * @param string $format The format that the handler should work with.
  49. * @param string $type The object type that should be handled.
  50. * @param array $params Additional parameters.
  51. * <pre>
  52. * 'version' - The format version.
  53. * </pre>
  54. *
  55. * @return Horde_Kolab_Format The handler.
  56. *
  57. * @throws Horde_Kolab_Format_Exception If the specified handler does not
  58. * exist.
  59. */
  60. public function create($format = 'Xml', $type = '', array $params = array())
  61. {
  62. switch ($type) {
  63. case 'h-ledger':
  64. $type_class = 'Envelope';
  65. break;
  66. default:
  67. $type_class = ucfirst(strtolower(str_replace('-', '', $type)));
  68. break;
  69. }
  70. $parser = ucfirst(strtolower($format));
  71. $class = basename('Horde_Kolab_Format_' . $parser . '_' . $type_class);
  72. $params = array_merge($this->_params, $params);
  73. if (class_exists($class)) {
  74. switch ($parser) {
  75. case 'Xml':
  76. $instance = new $class(
  77. new Horde_Kolab_Format_Xml_Parser(
  78. new DOMDocument('1.0', 'UTF-8')
  79. ),
  80. $this,
  81. $params
  82. );
  83. break;
  84. default:
  85. throw new Horde_Kolab_Format_Exception(
  86. sprintf(
  87. 'Failed to initialize the specified parser (Parser type %s does not exist)!',
  88. $parser
  89. )
  90. );
  91. }
  92. } else {
  93. throw new Horde_Kolab_Format_Exception(
  94. sprintf(
  95. 'Failed to load the specified Kolab Format handler (Class %s does not exist)!',
  96. $class
  97. )
  98. );
  99. }
  100. if (!empty($params['memlog'])) {
  101. if (!class_exists('Horde_Support_Memory')) {
  102. throw new Horde_Kolab_Format_Exception('The Horde_Support package seems to be missing (Class Horde_Support_Memory is missing)!');
  103. }
  104. $instance = new Horde_Kolab_Format_Decorator_Memory(
  105. $instance,
  106. new Horde_Support_Memory(),
  107. $params['memlog']
  108. );
  109. }
  110. if (!empty($params['timelog'])) {
  111. if (!class_exists('Horde_Support_Timer')) {
  112. throw new Horde_Kolab_Format_Exception('The Horde_Support package seems to be missing (Class Horde_Support_Timer is missing)!');
  113. }
  114. $instance = new Horde_Kolab_Format_Decorator_Timed(
  115. $instance,
  116. new Horde_Support_Timer(),
  117. $params['timelog']
  118. );
  119. }
  120. return $instance;
  121. }
  122. /**
  123. * Generates a XML type that deals with XML data modifications.
  124. *
  125. * @param string $type The value type.
  126. * @param DOMDocument $xmldoc The XML document the type should operate on.
  127. * @param array $params Additional parameters. See each time for
  128. * available options.
  129. *
  130. * @return Horde_Kolab_Format_Xml_Type The type.
  131. *
  132. * @throws Horde_Kolab_Format_Exception If the specified type does not
  133. * exist.
  134. */
  135. public function createXmlType($type, $xmldoc, array $params = array())
  136. {
  137. switch ($type) {
  138. case Horde_Kolab_Format_Xml::TYPE_XML:
  139. return new Horde_Kolab_Format_Xml_Type_XmlAppend(
  140. $xmldoc
  141. );
  142. default:
  143. throw new Horde_Kolab_Format_Exception(
  144. sprintf('XML type %s not supported!')
  145. );
  146. }
  147. }
  148. }