/libraries/vendor/joomla/registry/src/AbstractRegistryFormat.php

https://gitlab.com/vitaliylukin91/idea-rating · PHP · 79 lines · 22 code · 9 blank · 48 comment · 2 complexity · 7676f2c559cc92cd5fc05dcda3eb23a3 MD5 · raw file

  1. <?php
  2. /**
  3. * Part of the Joomla Framework Registry Package
  4. *
  5. * @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
  6. * @license GNU General Public License version 2 or later; see LICENSE
  7. */
  8. namespace Joomla\Registry;
  9. /**
  10. * Abstract Format for Registry
  11. *
  12. * @since 1.0
  13. */
  14. abstract class AbstractRegistryFormat
  15. {
  16. /**
  17. * @var array Format instances container.
  18. * @since 1.0
  19. */
  20. protected static $instances = array();
  21. /**
  22. * Returns a reference to a Format object, only creating it
  23. * if it doesn't already exist.
  24. *
  25. * @param string $type The format to load
  26. *
  27. * @return AbstractRegistryFormat Registry format handler
  28. *
  29. * @since 1.0
  30. * @throws \InvalidArgumentException
  31. */
  32. public static function getInstance($type)
  33. {
  34. // Sanitize format type.
  35. $type = strtolower(preg_replace('/[^A-Z0-9_]/i', '', $type));
  36. // Only instantiate the object if it doesn't already exist.
  37. if (!isset(self::$instances[$type]))
  38. {
  39. $class = '\\Joomla\\Registry\\Format\\' . ucfirst($type);
  40. if (!class_exists($class))
  41. {
  42. throw new \InvalidArgumentException('Unable to load format class.', 500);
  43. }
  44. self::$instances[$type] = new $class;
  45. }
  46. return self::$instances[$type];
  47. }
  48. /**
  49. * Converts an object into a formatted string.
  50. *
  51. * @param object $object Data Source Object.
  52. * @param array $options An array of options for the formatter.
  53. *
  54. * @return string Formatted string.
  55. *
  56. * @since 1.0
  57. */
  58. abstract public function objectToString($object, $options = null);
  59. /**
  60. * Converts a formatted string into an object.
  61. *
  62. * @param string $data Formatted string
  63. * @param array $options An array of options for the formatter.
  64. *
  65. * @return object Data Object
  66. *
  67. * @since 1.0
  68. */
  69. abstract public function stringToObject($data, array $options = array());
  70. }