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
<?php
/**
* @see https://github.com/zendframework/zend-cache for the canonical source repository
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-cache/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Cache\Psr;
use Zend\Cache\Storage\StorageInterface;
/**
* Provides common functionality surrounding value de/serialization as required
* by both PSR-6 and PSR-16
*/
trait SerializationTrait
{
/**
* Determine if the given storage adapter requires serialization.
*
* @param StorageInterface $storage
* @return bool
*/
private function isSerializationRequired(StorageInterface $storage)
{
$capabilities = $storage->getCapabilities();
$requiredTypes = ['string', 'integer', 'double', 'boolean', 'NULL', 'array', 'object'];
$types = $capabilities->getSupportedDatatypes();
foreach ($requiredTypes as $type) {
// 'object' => 'object' is OK
// 'integer' => 'string' is not (redis)
// 'integer' => 'integer' is not (memcache)
if (! (isset($types[$type]) && in_array($types[$type], [true, 'array', 'object'], true))) {
return true;
}
}
return false;
}
}