1
|
|
|
<?php defined('SYSPATH') OR die('No direct script access.'); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This class is what the upload field accually returns |
5
|
|
|
* and has all the nesessary info and manipulation abilities to save / delete / validate itself |
6
|
|
|
* |
7
|
|
|
* @package Jam |
8
|
|
|
* @author Ivan Kerin |
9
|
|
|
* @copyright (c) 2011-2012 Despark Ltd. |
10
|
|
|
* @license http://creativecommons.org/licenses/by-sa/3.0/legalcode |
11
|
|
|
*/ |
12
|
|
|
class Kohana_Jam_Range implements ArrayAccess, Serializable { |
13
|
|
|
|
14
|
|
|
protected $_min; |
15
|
|
|
|
16
|
|
|
protected $_max; |
17
|
|
|
|
18
|
|
|
protected $_format = ":min - :max"; |
19
|
|
|
|
20
|
58 |
|
public function format($format = NULL) |
21
|
|
|
{ |
22
|
58 |
|
if ($format !== NULL) |
23
|
|
|
{ |
24
|
13 |
|
$this->_format = $format; |
25
|
13 |
|
return $this; |
26
|
|
|
} |
27
|
58 |
|
return $this->_format; |
28
|
|
|
} |
29
|
|
|
|
30
|
1 |
|
public static function sum(array $ranges, $format = NULL) |
31
|
|
|
{ |
32
|
1 |
|
$min = 0; |
33
|
1 |
|
$max = 0; |
34
|
|
|
|
35
|
1 |
|
foreach ($ranges as $range) |
36
|
|
|
{ |
37
|
1 |
|
$min += (int) $range->min(); |
38
|
1 |
|
$max += (int) $range->max(); |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
return new Jam_Range(array($min, $max), $format); |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
public static function merge(array $ranges, $format = NULL) |
45
|
|
|
{ |
46
|
1 |
|
$min = 0; |
47
|
1 |
|
$max = 0; |
48
|
|
|
|
49
|
1 |
|
foreach ($ranges as $range) |
50
|
|
|
{ |
51
|
1 |
|
$min = max($min, $range->min()); |
52
|
1 |
|
$max = max($max, $range->max()); |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
return new Jam_Range(array($min, $max), $format); |
56
|
|
|
} |
57
|
|
|
|
58
|
57 |
|
public function __construct($source = NULL, $format = NULL) |
59
|
|
|
{ |
60
|
57 |
|
if (is_string($source) AND $source !== '') |
61
|
|
|
{ |
62
|
6 |
|
$source = explode('|', $source); |
63
|
|
|
} |
64
|
|
|
|
65
|
57 |
|
if (is_array($source)) |
66
|
|
|
{ |
67
|
46 |
|
$this->min($source[0]); |
68
|
46 |
|
$this->max($source[1]); |
69
|
|
|
} |
70
|
13 |
|
elseif ($source instanceof Jam_Range) |
71
|
|
|
{ |
72
|
3 |
|
$this->min($source->min()); |
73
|
3 |
|
$this->max($source->max()); |
74
|
|
|
} |
75
|
|
|
|
76
|
57 |
|
$this->format($format); |
77
|
57 |
|
} |
78
|
|
|
|
79
|
59 |
|
public function min($min = NULL) |
80
|
|
|
{ |
81
|
59 |
|
if ($min !== NULL) |
82
|
|
|
{ |
83
|
48 |
|
$this->_min = (float) $min; |
84
|
48 |
|
return $this; |
85
|
|
|
} |
86
|
59 |
|
return $this->_min; |
87
|
|
|
} |
88
|
|
|
|
89
|
56 |
|
public function max($max = NULL) |
90
|
|
|
{ |
91
|
56 |
|
if ($max !== NULL) |
92
|
|
|
{ |
93
|
48 |
|
$this->_max =(float) $max; |
94
|
48 |
|
return $this; |
95
|
|
|
} |
96
|
56 |
|
return $this->_max; |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
public function add(Jam_Range $addition) |
100
|
|
|
{ |
101
|
1 |
|
return Jam_Range::sum(array($this, $addition), $this->format()); |
102
|
|
|
} |
103
|
|
|
|
104
|
2 |
|
public function offsetSet($offset, $value) |
105
|
|
|
{ |
106
|
2 |
|
if (is_null($offset)) |
107
|
|
|
throw new Kohana_Exception('Cannot add new values to range'); |
108
|
|
|
|
109
|
2 |
|
if ($offset == 0) |
110
|
|
|
{ |
111
|
1 |
|
$this->min($value); |
112
|
|
|
} |
113
|
2 |
|
elseif ($offset == 1) |
114
|
|
|
{ |
115
|
1 |
|
$this->max($value); |
116
|
|
|
} |
117
|
|
|
else |
118
|
|
|
{ |
119
|
1 |
|
throw new Kohana_Exception('Use offset 0 for min and offset 1 for max, offset :offset not supported', array(':offset' => $offset)); |
120
|
|
|
} |
121
|
1 |
|
} |
122
|
|
|
|
123
|
4 |
|
public function offsetExists($offset) |
124
|
|
|
{ |
125
|
4 |
|
return ($offset == 0 OR $offset == 1); |
126
|
|
|
} |
127
|
|
|
|
128
|
1 |
|
public function offsetUnset($offset) |
129
|
|
|
{ |
130
|
1 |
|
throw new Kohana_Exception('Cannot unset range object'); |
131
|
|
|
} |
132
|
|
|
|
133
|
1 |
|
public function offsetGet($offset) |
134
|
|
|
{ |
135
|
1 |
|
if ( ! $this->offsetExists($offset)) |
136
|
|
|
throw new Kohana_Exception('Use offset 0 for min and offset 1 for max, offset :offset not supported', array(':offset' => $offset)); |
137
|
|
|
|
138
|
1 |
|
return $offset ? $this->max() : $this->min(); |
139
|
|
|
} |
140
|
|
|
|
141
|
4 |
|
public function __toString() |
142
|
|
|
{ |
143
|
4 |
|
return $this->min().'|'.$this->max(); |
144
|
|
|
} |
145
|
|
|
|
146
|
7 |
|
public function humanize() |
147
|
|
|
{ |
148
|
7 |
|
if (is_callable($this->format())) |
149
|
|
|
{ |
150
|
|
|
return call_user_func($this->format(), $this->min(), $this->max()); |
151
|
|
|
} |
152
|
|
|
else |
153
|
|
|
{ |
154
|
7 |
|
return strtr($this->format(), array(':min' => $this->min(), ':max' => $this->max())); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
1 |
|
public function as_array() |
159
|
|
|
{ |
160
|
1 |
|
return array($this->min(), $this->max()); |
161
|
|
|
} |
162
|
|
|
|
163
|
1 |
|
public function serialize() |
164
|
|
|
{ |
165
|
1 |
|
return $this->__toString(); |
166
|
|
|
} |
167
|
|
|
|
168
|
1 |
|
public function unserialize($data) |
169
|
|
|
{ |
170
|
1 |
|
list($min, $max) = explode('|', $data); |
171
|
|
|
|
172
|
1 |
|
$this->min($min); |
173
|
1 |
|
$this->max($max); |
174
|
1 |
|
} |
175
|
|
|
} |
176
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.