1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the IrishDan\ResponsiveImageBundle package. |
4
|
|
|
* |
5
|
|
|
* (c) Daniel Byrne <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE file that was distributed with this source |
8
|
|
|
* code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace IrishDan\ResponsiveImageBundle\Validator\Constraints; |
12
|
|
|
|
13
|
|
|
use IrishDan\ResponsiveImageBundle\ImageProcessing\CoordinateGeometry; |
14
|
|
|
use Symfony\Component\Validator\Constraint; |
15
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class CropFocusCoordinatesValidator |
19
|
|
|
* |
20
|
|
|
* @package IrishDan\ResponsiveImageBundle\Validator\Constraints |
21
|
|
|
*/ |
22
|
|
|
class CropFocusCoordinatesValidator extends ConstraintValidator |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Validate a crop focus coordinate string to ensure the syntax is correct |
26
|
|
|
* and to ensure that the focus rectangle inside the crop rectangle |
27
|
|
|
* |
28
|
|
|
* @param mixed $value |
29
|
|
|
* @param Constraint $constraint |
30
|
|
|
*/ |
31
|
|
|
public function validate($value, Constraint $constraint) |
32
|
|
|
{ |
33
|
|
|
$valid = true; |
34
|
|
|
|
35
|
|
|
// Check it is in the format: 0,0,0,0:0,0,0,0 |
|
|
|
|
36
|
|
|
$testValue = str_replace(' ', '', $value); |
37
|
|
|
if (!preg_match('/^(\d+),(\d+),(\d+),(\d+):(\d+),(\d+),(\d+),(\d+)$/', $testValue, $matches)) { |
38
|
|
|
$valid = false; |
39
|
|
|
} |
40
|
|
|
else { |
41
|
|
|
// Check the focus rectangle is inside the crop rectangle. |
42
|
|
|
$cropFocusCoordinates = explode(':', $testValue); |
43
|
|
|
$crop = explode(',', $cropFocusCoordinates[0]); |
44
|
|
|
$focus = explode(',', $cropFocusCoordinates[1]); |
45
|
|
|
|
46
|
|
|
// Add constraint generated entity. |
47
|
|
|
$geometry = new CoordinateGeometry($crop[0], $crop[1], $crop[2], $crop[3]); |
48
|
|
|
|
49
|
|
|
if (!$geometry->isInside($focus[0], $focus[1], $focus[2], $focus[3])) { |
50
|
|
|
$valid = false; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (!$valid) { |
55
|
|
|
$this->context->buildViolation($constraint->message) |
56
|
|
|
->setParameter('{{ string }}', $value) |
57
|
|
|
->addViolation() |
58
|
|
|
; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.