|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace JhFlexiTime\Validator; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\Mapping\MappingException; |
|
6
|
|
|
use DoctrineModule\Validator\UniqueObject as DoctrineUniqueObject; |
|
7
|
|
|
use ZfcUser\Entity\UserInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class UniqueObject |
|
11
|
|
|
* @package JhFlexiTime\Validator |
|
12
|
|
|
* @author Aydin Hassan <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class UniqueObject extends DoctrineUniqueObject |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Returns false if there is another object with the same field values but other identifiers. |
|
19
|
|
|
* |
|
20
|
|
|
* @param mixed $value |
|
21
|
|
|
* @param array $context |
|
22
|
|
|
* @return boolean |
|
23
|
|
|
*/ |
|
24
|
|
|
public function isValid($value, $context = null) |
|
25
|
|
|
{ |
|
26
|
|
|
if (!$this->useContext) { |
|
27
|
|
|
$context = (array) $value; |
|
28
|
|
|
} else { |
|
29
|
|
|
$value = (array) $context; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
$value = $this->cleanSearchValue($value); |
|
33
|
|
|
$match = $this->objectRepository->findOneBy($value); |
|
34
|
|
|
|
|
35
|
|
|
if (!is_object($match)) { |
|
36
|
|
|
return true; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$expectedIdentifiers = $this->getExpectedIdentifiers($context); |
|
40
|
|
|
$foundIdentifiers = $this->getFoundIdentifiers($match); |
|
41
|
|
|
|
|
42
|
|
|
foreach ($foundIdentifiers as $key => $idField) { |
|
43
|
|
|
if (is_object($idField)) { |
|
44
|
|
|
try { |
|
45
|
|
|
$classMeta = $this->objectManager->getClassMetadata(get_class($idField)); |
|
46
|
|
|
$idValue = $classMeta->getIdentifierValues($idField); |
|
47
|
|
|
$idValue = array_pop($idValue); |
|
48
|
|
|
$foundIdentifiers[$key] = $idValue; |
|
49
|
|
|
} catch (MappingException $e) { |
|
50
|
|
|
//not an object managed by doctrine |
|
51
|
|
|
continue; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if (count(array_diff_assoc($expectedIdentifiers, $foundIdentifiers)) == 0) { |
|
57
|
|
|
return true; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$this->error(self::ERROR_OBJECT_NOT_UNIQUE, $value); |
|
|
|
|
|
|
61
|
|
|
return false; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: