1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oro\Bundle\EntityExtendBundle\Entity\Repository; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityRepository; |
6
|
|
|
use Doctrine\ORM\QueryBuilder; |
7
|
|
|
|
8
|
|
|
use Oro\Bundle\EntityExtendBundle\Entity\AbstractEnumValue; |
9
|
|
|
use Oro\Bundle\EntityExtendBundle\Tools\ExtendHelper; |
10
|
|
|
|
11
|
|
|
class EnumValueRepository extends EntityRepository |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Creates an entity represents an enum value |
15
|
|
|
* |
16
|
|
|
* @param string $name The enum value name |
17
|
|
|
* @param int $priority An number used to sort enum values on UI. |
18
|
|
|
* Values with less priority is rendered at the top |
19
|
|
|
* @param boolean $default Determines if this value is selected by default for new records |
20
|
|
|
* @param string|null $id The enum value identifier. If not specified it is generated |
21
|
|
|
* automatically based on the given name. Usually it is the same as name, |
22
|
|
|
* but spaces are replaced with underscore and result is converted to lower case. |
23
|
|
|
* As the id length is limited to 32 characters, in case if the name is longer then |
24
|
|
|
* some hashing function is used to generate the id. |
25
|
|
|
* |
26
|
|
|
* @return AbstractEnumValue |
27
|
|
|
* |
28
|
|
|
* @throws \InvalidArgumentException |
29
|
|
|
*/ |
30
|
|
|
public function createEnumValue($name, $priority, $default, $id = null) |
31
|
|
|
{ |
32
|
|
|
if (strlen($name) === 0) { |
33
|
|
|
throw new \InvalidArgumentException('$name must not be empty.'); |
34
|
|
|
} |
35
|
|
|
if (!isset($id) || $id === '') { |
36
|
|
|
$id = ExtendHelper::buildEnumValueId($name); |
37
|
|
|
} elseif (strlen($id) > ExtendHelper::MAX_ENUM_VALUE_ID_LENGTH) { |
38
|
|
|
throw new \InvalidArgumentException( |
39
|
|
|
sprintf( |
40
|
|
|
'$id length must be less or equal %d characters. id: %s.', |
41
|
|
|
ExtendHelper::MAX_ENUM_VALUE_ID_LENGTH, |
42
|
|
|
$id |
43
|
|
|
) |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$enumValueClassName = $this->getClassName(); |
48
|
|
|
|
49
|
|
|
return new $enumValueClassName($id, $name, $priority, $default); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return QueryBuilder |
54
|
|
|
*/ |
55
|
|
|
public function getValuesQueryBuilder() |
56
|
|
|
{ |
57
|
|
|
$qb = $this->createQueryBuilder('e'); |
58
|
|
|
$qb->orderBy($qb->expr()->asc('e.priority')); |
59
|
|
|
|
60
|
|
|
return $qb; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return AbstractEnumValue[] |
65
|
|
|
*/ |
66
|
|
|
public function getValues() |
67
|
|
|
{ |
68
|
|
|
return $this->getValuesQueryBuilder()->getQuery()->getResult(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return QueryBuilder |
73
|
|
|
*/ |
74
|
|
|
public function getDefaultValuesQueryBuilder() |
75
|
|
|
{ |
76
|
|
|
$qb = $this->getValuesQueryBuilder(); |
77
|
|
|
$qb->andWhere($qb->expr()->eq('e.default', ':default')) |
78
|
|
|
->setParameter('default', true); |
79
|
|
|
|
80
|
|
|
return $qb; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return AbstractEnumValue[] |
85
|
|
|
*/ |
86
|
|
|
public function getDefaultValues() |
87
|
|
|
{ |
88
|
|
|
return $this->getDefaultValuesQueryBuilder()->getQuery()->getResult(); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|