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