Skip to content

Commit

Permalink
pslam general object return
Browse files Browse the repository at this point in the history
Signed-off-by: Abdul Malik Ikhsan <[email protected]>
  • Loading branch information
samsonasik committed Nov 15, 2022
1 parent 75bc902 commit c5f3ff9
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions src/Storage/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
use function class_exists;
use function class_implements;
use function class_parents;
use function get_class;
use function get_debug_type;
use function gettype;
use function in_array;
use function is_array;
use function is_object;
use function is_string;
use function sprintf;

Expand Down Expand Up @@ -62,16 +65,21 @@ public static function factory($type, $options = [])
));
}

return match (true) {
in_array(AbstractSessionArrayStorage::class, class_parents($type)) => static::createSessionArrayStorage($type, $options),
$type === ArrayStorage::class, in_array(ArrayStorage::class, class_parents($type)) => static::createArrayStorage($type, $options),
in_array(StorageInterface::class, class_implements($type)) => new $type($options),
default => throw new Exception\InvalidArgumentException(sprintf(
'Unrecognized type "%s" provided; expects a class implementing %s\StorageInterface',
$type,
__NAMESPACE__
)),
};
switch (true) {
case in_array(AbstractSessionArrayStorage::class, class_parents($type)):
return static::createSessionArrayStorage($type, $options);
case $type === ArrayStorage::class:
case in_array(ArrayStorage::class, class_parents($type)):
return static::createArrayStorage($type, $options);
case in_array(StorageInterface::class, 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__
));
}
}

/**
Expand All @@ -92,7 +100,7 @@ protected static function createArrayStorage($type, $options)
throw new Exception\InvalidArgumentException(sprintf(
'%s expects the "input" option to be an array; received "%s"',
$type,
get_debug_type($options['input'])
is_object($options['input']) ? get_class($options['input']) : gettype($options['input'])
));
}
$input = $options['input'];
Expand All @@ -107,7 +115,9 @@ protected static function createArrayStorage($type, $options)
throw new Exception\InvalidArgumentException(sprintf(
'%s expects the "iterator_class" option to be a valid class; received "%s"',
$type,
get_debug_type($options['iterator_class'])
is_object($options['iterator_class'])
? get_class($options['iterator_class'])
: gettype($options['iterator_class'])
));
}
$iteratorClass = $options['iterator_class'];
Expand All @@ -120,6 +130,7 @@ protected static function createArrayStorage($type, $options)
* 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.
*/
Expand All @@ -135,7 +146,7 @@ protected static function createSessionArrayStorage($type, array $options)
throw new Exception\InvalidArgumentException(sprintf(
'%s expects the "input" option to be null, an array, or to implement ArrayAccess; received "%s"',
$type,
get_debug_type($input)
is_object($input) ? $input::class : gettype($input)
));
}
}
Expand Down

0 comments on commit c5f3ff9

Please sign in to comment.