1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* part-db version 0.1 |
5
|
|
|
* Copyright (C) 2005 Christoph Lechner |
6
|
|
|
* http://www.cl-projects.de/ |
7
|
|
|
* |
8
|
|
|
* part-db version 0.2+ |
9
|
|
|
* Copyright (C) 2009 K. Jacobs and others (see authors.php) |
10
|
|
|
* http://code.google.com/p/part-db/ |
11
|
|
|
* |
12
|
|
|
* Part-DB Version 0.4+ |
13
|
|
|
* Copyright (C) 2016 - 2019 Jan Böhmer |
14
|
|
|
* https://github.com/jbtronics |
15
|
|
|
* |
16
|
|
|
* This program is free software; you can redistribute it and/or |
17
|
|
|
* modify it under the terms of the GNU General Public License |
18
|
|
|
* as published by the Free Software Foundation; either version 2 |
19
|
|
|
* of the License, or (at your option) any later version. |
20
|
|
|
* |
21
|
|
|
* This program is distributed in the hope that it will be useful, |
22
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
23
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
24
|
|
|
* GNU General Public License for more details. |
25
|
|
|
* |
26
|
|
|
* You should have received a copy of the GNU General Public License |
27
|
|
|
* along with this program; if not, write to the Free Software |
28
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
29
|
|
|
* |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
namespace App\Validator\Constraints; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
use App\Entity\Parts\PartLot; |
36
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
37
|
|
|
use Symfony\Component\DependencyInjection\Compiler\ValidateEnvPlaceholdersPass; |
38
|
|
|
use Symfony\Component\Form\Exception\UnexpectedTypeException; |
39
|
|
|
use Symfony\Component\Validator\Constraint; |
40
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
41
|
|
|
|
42
|
|
|
class ValidPartLotValidator extends ConstraintValidator |
43
|
|
|
{ |
44
|
|
|
protected $em; |
45
|
|
|
|
46
|
|
|
public function __construct(EntityManagerInterface $em) |
47
|
|
|
{ |
48
|
|
|
$this->em = $em; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Checks if the passed value is valid. |
53
|
|
|
* |
54
|
|
|
* @param mixed $value The value that should be validated |
55
|
|
|
* @param Constraint $constraint The constraint for the validation |
56
|
|
|
*/ |
57
|
|
|
public function validate($value, Constraint $constraint) |
58
|
|
|
{ |
59
|
|
|
if (!$constraint instanceof ValidPartLot) { |
60
|
|
|
throw new UnexpectedTypeException($constraint, ValidPartLot::class); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (!$value instanceof PartLot) { |
64
|
|
|
throw new UnexpectedTypeException($value, PartLot::class); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
//We can only validate the values if we know the storelocation |
68
|
|
|
if ($value->getStorageLocation()) { |
69
|
|
|
$parts = $value->getStorageLocation()->getParts(); |
70
|
|
|
|
71
|
|
|
//Check for isFull() attribute |
72
|
|
|
if ($value->getStorageLocation()->isFull()) { |
73
|
|
|
//Compare with saved amount value |
74
|
|
|
$db_lot = $this->em->getUnitOfWork()->getOriginalEntityData($value); |
75
|
|
|
|
76
|
|
|
//Amount increasment is not allowed |
77
|
|
|
if ($db_lot && $value->getAmount() > $db_lot['amount']) { |
|
|
|
|
78
|
|
|
$this->context->buildViolation('validator.part_lot.location_full.no_increasment') |
79
|
|
|
->setParameter('{{ old_amount }}', $db_lot['amount']) |
80
|
|
|
->atPath('amount')->addViolation(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (!$parts->contains($value->getPart())) { |
84
|
|
|
$this->context->buildViolation('validator.part_lot.location_full') |
85
|
|
|
->atPath('storage_location')->addViolation(); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
//Check for onlyExisting |
91
|
|
|
if ($value->getStorageLocation()->isLimitToExistingParts()) { |
92
|
|
|
if (!$parts->contains($value->getPart())) { |
93
|
|
|
$this->context->buildViolation('validator.part_lot.only_existing') |
94
|
|
|
->atPath('storage_location')->addViolation(); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
//Check for only single part |
99
|
|
|
if ($value->getStorageLocation()->isLimitToExistingParts()) { |
100
|
|
|
if (($parts->count() > 0) && !$parts->contains($value->getPart())) { |
101
|
|
|
$this->context->buildViolation('validator.part_lot.single_part') |
102
|
|
|
->atPath('storage_location')->addViolation(); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.