1: <?php
2:
3: 4: 5: 6: 7: 8: 9:
10:
11: require_once 'exceptions.php';
12:
13: 14: 15:
16:
17: class Size {
18:
19: 20: 21: 22: 23:
24: private $width;
25:
26: 27: 28: 29: 30:
31:
32: private $height;
33:
34: 35: 36: 37: 38:
39:
40: public function __construct($width, $height) {
41: $this->setWidth($width);
42: $this->setHeight($height);
43: }
44:
45: 46: 47: 48: 49: 50: 51:
52:
53: public function greatThen(Size $s) {
54: return $this->getWidth() > $s->getWidth() || $this->getHeight() > $s->getHeight();
55: }
56:
57: 58: 59: 60: 61: 62: 63:
64:
65: public function lessThen(Size $s) {
66: return !$this->greatThen($s) && !$this->equals($s);
67: }
68:
69: 70: 71: 72: 73: 74: 75:
76:
77: public function isInner(Size $s) {
78: return $this->getWidth() <= $s->getWidth() && $this->getHeight() <= $s->getHeight();
79: }
80:
81: 82: 83: 84: 85: 86:
87:
88: public function equals(Size $s) {
89: return $this->getWidth() == $s->getWidth() && $this->getHeight() == $s->getHeight();
90: }
91:
92: 93: 94: 95:
96:
97: public function flip() {
98: $t = $this->getWidth();
99: $this->setWidth($this->getHeight());
100: $this->setHeight($t);
101: return $this;
102: }
103:
104: 105: 106: 107: 108: 109: 110: 111:
112:
113: public function getByWidth($width) {
114: if (!is_integer($width)) {
115: throw new IllegalArgumentExceptions();
116: }
117:
118: if ($width >= $this->getWidth()) {
119: return $this;
120: }
121:
122: $height = (int) round($this->getHeight() * $width / $this->getWidth());
123: return new Size($width, $height);
124: }
125:
126: 127: 128: 129: 130: 131: 132:
133:
134: public function getByHeight($height) {
135: if (!is_integer($height)) {
136: throw new IllegalArgumentException();
137: }
138:
139: if ($height >= $this->getHeight()) {
140: return $this;
141: }
142:
143: $width = (int) round($this->getWidth() * $height / $this->getHeight());
144: return new Size($width, $height);
145: }
146:
147: 148: 149: 150: 151: 152: 153:
154:
155: public function getByFrame() {
156: $args = func_get_args();
157: if (count($args) == 2) {
158: $this->getByFrame(new Size($args[0], $args[1]));
159: } else if (count($args) == 1 && $args[0] instanceof Size) {
160: $frame = $args[0];
161: } else {
162: throw new IllegalArgumentException();
163: }
164:
165: if ($frame->getWidth() <= 0 || $frame->getHeight() <= 0)
166: throw new IllegalArgumentException();
167:
168: if ($this->isInner($frame))
169: return $this;
170:
171: $height = $frame->getHeight();
172: $width = $frame->getWidth();
173:
174: if ($this->getWidth() / $width > $this->getHeight() / $height)
175: return $this->getByWidth($width);
176:
177: return $this->getByHeight($height);
178: }
179:
180: 181: 182: 183: 184: 185: 186: 187: 188:
189:
190: public static function add(Size $s, $obj) {
191: if($obj instanceof Size) {
192: return new Size($s->getWidth() + $obj->getWidth(), $s->getHeight() + $obj->getHeight());
193: } else if ($obj instanceof Point) {
194: return new Size($s->getWidth() + $obj->getX(), $s->getHeigth() + $obj->getY());
195: }
196: throw new IllegalArgumentException();
197: }
198:
199: 200: 201: 202: 203: 204: 205: 206: 207: 208:
209:
210: public static function subtract(Size $s, $obj) {
211: if($obj instanceof Size) {
212: return new Size($s->getWidth() - $obj->getWidth(), $s->getHeight() - $obj->getHeight());
213: } else if ($obj instanceof Point) {
214: return new Size($s->getWidth() - $obj->getX(), $s->getHeigth() - $obj->getY());
215: }
216: throw new IllegalArgumentException();
217: }
218:
219: public function getWidth() {
220: return $this->width;
221: }
222:
223: public function getHeight() {
224: return $this->height;
225: }
226:
227: 228: 229: 230:
231:
232: public function setWidth($width) {
233: if (is_integer($width)) {
234: $this->width = $width;
235: } else {
236: throw new IllegalArgumentException();
237: }
238: }
239:
240: 241: 242: 243:
244:
245: public function setHeight($height) {
246: if (is_integer($height)) {
247: $this->height = $height;
248: } else {
249: throw new IllegalArgumentException();
250: }
251: }
252:
253: public function __toString() {
254: return "{width: {$this->width}, height: {$this->height}}";
255: }
256: }
257: ?>