fix calendar css, take 2. (#213)
[openemr.git] / interface / modules / zend_modules / library / Zend / Session / Storage / Factory.php
blob0d7bf36ed35c3a1fb63cbc17148ba749bdde9509
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Session\Storage;
12 use ArrayAccess;
13 use Traversable;
14 use Zend\Session\Exception;
15 use Zend\Stdlib\ArrayObject;
16 use Zend\Stdlib\ArrayUtils;
18 abstract class Factory
20 /**
21 * Create and return a StorageInterface instance
23 * @param string $type
24 * @param array|Traversable $options
25 * @return StorageInterface
26 * @throws Exception\InvalidArgumentException for unrecognized $type or individual options
28 public static function factory($type, $options = array())
30 if (!is_string($type)) {
31 throw new Exception\InvalidArgumentException(sprintf(
32 '%s expects the $type argument to be a string class name; received "%s"',
33 __METHOD__,
34 (is_object($type) ? get_class($type) : gettype($type))
35 ));
37 if (!class_exists($type)) {
38 $class = __NAMESPACE__ . '\\' . $type;
39 if (!class_exists($class)) {
40 throw new Exception\InvalidArgumentException(sprintf(
41 '%s expects the $type argument to be a valid class name; received "%s"',
42 __METHOD__,
43 $type
44 ));
46 $type = $class;
49 if ($options instanceof Traversable) {
50 $options = ArrayUtils::iteratorToArray($options);
52 if (!is_array($options)) {
53 throw new Exception\InvalidArgumentException(sprintf(
54 '%s expects the $options argument to be an array or Traversable; received "%s"',
55 __METHOD__,
56 (is_object($options) ? get_class($options) : gettype($options))
57 ));
60 switch (true) {
61 case (in_array('Zend\Session\Storage\AbstractSessionArrayStorage', class_parents($type))):
62 return static::createSessionArrayStorage($type, $options);
63 case ($type === 'Zend\Session\Storage\ArrayStorage'):
64 case (in_array('Zend\Session\Storage\ArrayStorage', class_parents($type))):
65 return static::createArrayStorage($type, $options);
66 case (in_array('Zend\Session\Storage\StorageInterface', class_implements($type))):
67 return new $type($options);
68 default:
69 throw new Exception\InvalidArgumentException(sprintf(
70 'Unrecognized type "%s" provided; expects a class implementing %s\StorageInterface',
71 $type,
72 __NAMESPACE__
73 ));
77 /**
78 * Create a storage object from an ArrayStorage class (or a descendent)
80 * @param string $type
81 * @param array $options
82 * @return ArrayStorage
84 protected static function createArrayStorage($type, $options)
86 $input = array();
87 $flags = ArrayObject::ARRAY_AS_PROPS;
88 $iteratorClass = 'ArrayIterator';
90 if (isset($options['input']) && null !== $options['input']) {
91 if (!is_array($options['input'])) {
92 throw new Exception\InvalidArgumentException(sprintf(
93 '%s expects the "input" option to be an array; received "%s"',
94 $type,
95 (is_object($options['input']) ? get_class($options['input']) : gettype($options['input']))
96 ));
98 $input = $options['input'];
101 if (isset($options['flags'])) {
102 $flags = $options['flags'];
105 if (isset($options['iterator_class'])) {
106 if (!class_exists($options['iterator_class'])) {
107 throw new Exception\InvalidArgumentException(sprintf(
108 '%s expects the "iterator_class" option to be a valid class; received "%s"',
109 $type,
110 (is_object($options['iterator_class']) ? get_class($options['iterator_class']) : gettype($options['iterator_class']))
113 $iteratorClass = $options['iterator_class'];
116 return new $type($input, $flags, $iteratorClass);
120 * Create a storage object from a class extending AbstractSessionArrayStorage
122 * @param string $type
123 * @param array $options
124 * @return AbstractSessionArrayStorage
125 * @throws Exception\InvalidArgumentException if the input option is invalid
127 protected static function createSessionArrayStorage($type, array $options)
129 $input = null;
130 if (isset($options['input'])) {
131 if (null !== $options['input']
132 && !is_array($options['input'])
133 && !$input instanceof ArrayAccess
135 throw new Exception\InvalidArgumentException(sprintf(
136 '%s expects the "input" option to be null, an array, or to implement ArrayAccess; received "%s"',
137 $type,
138 (is_object($options['input']) ? get_class($options['input']) : gettype($options['input']))
141 $input = $options['input'];
144 return new $type($input);