summaryrefslogtreecommitdiff
path: root/test_tools/simpletest/encoding.php
blob: 62ca2a1b557bf8c79508872b4ea241381e9cd944 (plain)
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
149
150
151
152
153
154
155
156
<?php
    /**
     *	base include file for SimpleTest
     *	@package	SimpleTest
     *	@subpackage	WebTester
     *	@version	$Id: encoding.php,v 1.6 2005/01/02 23:43:23 lastcraft Exp $
     */

    /**
     *    Bundle of GET/POST parameters. Can include
     *    repeated parameters.
	 *    @package SimpleTest
	 *    @subpackage WebTester
     */
    class SimpleFormEncoding {
        protected $_request;
        protected $_x;
        protected $_y;
        
        /**
         *    Starts empty.
         *    @param array $query/SimpleQueryString  Hash of parameters.
         *                                           Multiple values are
         *                                           as lists on a single key.
         *    @access public
         */
        function SimpleFormEncoding($query = false) {
            if (! $query) {
                $query = array();
            }
            $this->_request = array();
            $this->setCoordinates();
            $this->merge($query);
        }
        
        /**
         *    Adds a parameter to the query.
         *    @param string $key            Key to add value to.
         *    @param string/array $value    New data.
         *    @access public
         */
        function add($key, $value) {
            if ($value === false) {
                return;
            }
            if (! isset($this->_request[$key])) {
                $this->_request[$key] = array();
            }
            if (is_array($value)) {
                foreach ($value as $item) {
                    $this->_request[$key][] = $item;
                }
            } else {
                $this->_request[$key][] = $value;
            }
        }
        
        /**
         *    Adds a set of parameters to this query.
         *    @param array/SimpleQueryString $query  Multiple values are
         *                                           as lists on a single key.
         *    @access public
         */
        function merge($query) {
            if (is_object($query)) {
                foreach ($query->getKeys() as $key) {
                    $this->add($key, $query->getValue($key));
                }
                if ($query->getX() !== false) {
                    $this->setCoordinates($query->getX(), $query->getY());
                }
            } elseif (is_array($query)) {
                foreach ($query as $key => $value) {
                    $this->add($key, $value);
                }
            }
        }
        
        /**
         *    Sets image coordinates. Set to false to clear
         *    them.
         *    @param integer $x    Horizontal position.
         *    @param integer $y    Vertical position.
         *    @access public
         */
        function setCoordinates($x = false, $y = false) {
            if (($x === false) || ($y === false)) {
                $this->_x = $this->_y = false;
                return;
            }
            $this->_x = (integer)$x;
            $this->_y = (integer)$y;
        }
        
        /**
         *    Accessor for horizontal image coordinate.
         *    @return integer        X value.
         *    @access public
         */
        function getX() {
            return $this->_x;
        }
         
        /**
         *    Accessor for vertical image coordinate.
         *    @return integer        Y value.
         *    @access public
         */
        function getY() {
            return $this->_y;
        }
        
        /**
         *    Accessor for single value.
         *    @return string/array    False if missing, string
         *                            if present and array if
         *                            multiple entries.
         *    @access public
         */
        function getValue($key) {
            if (! isset($this->_request[$key])) {
                return false;
            } elseif (count($this->_request[$key]) == 1) {
                return $this->_request[$key][0];
            } else {
                return $this->_request[$key];
            }
        }
        
        /**
         *    Accessor for key list.
         *    @return array        List of keys present.
         *    @access public
         */
        function getKeys() {
            return array_keys($this->_request);
        }
        
        /**
         *    Renders the query string as a URL encoded
         *    request part.
         *    @return string        Part of URL.
         *    @access public
         */
        function asString() {
            $statements = array();
            foreach ($this->_request as $key => $values) {
                foreach ($values as $value) {
                    $statements[] = "$key=" . urlencode($value);
                }
            }
            $coords = ($this->_x !== false) ? '?' . $this->_x . ',' . $this->_y : '';
            return implode('&', $statements) . $coords;
        }
    }
?>