Project

General

Profile

1
<?php
2

    
3
/*
4
 * This file is part of the Predis package.
5
 *
6
 * (c) Daniele Alessandri <suppakilla@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

    
12
namespace Predis\Command;
13

    
14
/**
15
 * @link http://redis.io/commands/zrange
16
 * @author Daniele Alessandri <suppakilla@gmail.com>
17
 */
18
class ZSetRange extends PrefixableCommand
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function getId()
24
    {
25
        return 'ZRANGE';
26
    }
27

    
28
    /**
29
     * {@inheritdoc}
30
     */
31
    protected function filterArguments(Array $arguments)
32
    {
33
        if (count($arguments) === 4) {
34
            $lastType = gettype($arguments[3]);
35

    
36
            if ($lastType === 'string' && strtoupper($arguments[3]) === 'WITHSCORES') {
37
                // Used for compatibility with older versions
38
                $arguments[3] = array('WITHSCORES' => true);
39
                $lastType = 'array';
40
            }
41

    
42
            if ($lastType === 'array') {
43
                $options = $this->prepareOptions(array_pop($arguments));
44

    
45
                return array_merge($arguments, $options);
46
            }
47
        }
48

    
49
        return $arguments;
50
    }
51

    
52
    /**
53
     * Returns a list of options and modifiers compatible with Redis.
54
     *
55
     * @param  array $options List of options.
56
     * @return array
57
     */
58
    protected function prepareOptions($options)
59
    {
60
        $opts = array_change_key_case($options, CASE_UPPER);
61
        $finalizedOpts = array();
62

    
63
        if (!empty($opts['WITHSCORES'])) {
64
            $finalizedOpts[] = 'WITHSCORES';
65
        }
66

    
67
        return $finalizedOpts;
68
    }
69

    
70
    /**
71
     * Checks for the presence of the WITHSCORES modifier.
72
     *
73
     * @return bool
74
     */
75
    protected function withScores()
76
    {
77
        $arguments = $this->getArguments();
78

    
79
        if (count($arguments) < 4) {
80
            return false;
81
        }
82

    
83
        return strtoupper($arguments[3]) === 'WITHSCORES';
84
    }
85

    
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function parseResponse($data)
90
    {
91
        if ($this->withScores()) {
92
            $result = array();
93

    
94
            for ($i = 0; $i < count($data); $i++) {
95
                $result[] = array($data[$i], $data[++$i]);
96
            }
97

    
98
            return $result;
99
        }
100

    
101
        return $data;
102
    }
103
}
(137-137/148)