Skip to content

Commit 52299b4

Browse files
author
Alexander Obuhovich
committed
Merge pull request php-annotations#116 from lsces/Issue112
Adds missing "RangeAnnotation" to demo script
2 parents 4fc3cc2 + 4f5bb42 commit 52299b4

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

demo/annotations/Package.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ public static function register(AnnotationManager $annotationManager)
2323
$annotationManager->registry['length'] = 'mindplay\demo\annotations\LengthAnnotation';
2424
$annotationManager->registry['required'] = 'mindplay\demo\annotations\RequiredAnnotation';
2525
$annotationManager->registry['text'] = 'mindplay\demo\annotations\TextAnnotation';
26-
}
26+
$annotationManager->registry['range'] = 'mindplay\demo\annotations\RangeAnnotation';
27+
}
2728
}

demo/annotations/RangeAnnotation.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the php-annotation framework.
5+
*
6+
* (c) Rasmus Schultz <rasmus@mindplay.dk>
7+
*
8+
* This software is licensed under the GNU LGPL license
9+
* for more information, please see:
10+
*
11+
* <https://github.com/mindplay-dk/php-annotations>
12+
*/
13+
14+
namespace mindplay\demo\annotations;
15+
16+
use mindplay\annotations\AnnotationException;
17+
18+
/**
19+
* Specifies validation against a minimum and/or maximum numeric value.
20+
*
21+
* @usage('property'=>true, 'inherited'=>true)
22+
*/
23+
class RangeAnnotation extends ValidationAnnotationBase
24+
{
25+
/**
26+
* @var mixed $min Minimum numeric value (integer or floating point)
27+
*/
28+
public $min = null;
29+
30+
/**
31+
* @var mixed $max Maximum numeric value (integer or floating point)
32+
*/
33+
public $max = null;
34+
35+
/**
36+
* Initialize the annotation.
37+
*/
38+
public function initAnnotation(array $properties)
39+
{
40+
if (isset($properties[0])) {
41+
if (isset($properties[1])) {
42+
$this->min = $properties[0];
43+
$this->max = $properties[1];
44+
unset($properties[1]);
45+
} else {
46+
$this->max = $properties[0];
47+
}
48+
49+
unset($properties[0]);
50+
}
51+
52+
parent::initAnnotation($properties);
53+
54+
if ($this->min !== null && !is_int($this->min) && !is_float($this->min)) {
55+
throw new AnnotationException('RangeAnnotation requires a numeric (float or int) min property');
56+
}
57+
58+
if ($this->max !== null && !is_int($this->max) && !is_float($this->max)) {
59+
throw new AnnotationException('RangeAnnotation requires a numeric (float or int) max property');
60+
}
61+
62+
if ($this->min === null && $this->max === null) {
63+
throw new AnnotationException('RangeAnnotation requires a min and/or max property');
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)