Skip to content

Commit d3f03c8

Browse files
authored
Collection supports filter, not matching (#46)
* Collection supports filter, not matching * Support php 7.2 and higher
1 parent d01377b commit d3f03c8

File tree

6 files changed

+138
-229
lines changed

6 files changed

+138
-229
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ language: php
22

33
matrix:
44
include:
5-
- php: 7.1
65
- php: 7.2
6+
- php: 7.3
77
- php: nightly
88
allow_failures:
99
- php: nightly

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"license": "MIT",
55
"type": "composer-plugin",
66
"require": {
7-
"php": ">=7.1",
7+
"php": ">=7.2",
88
"ext-bcmath": "*",
99
"ext-json": "*",
1010
"composer-plugin-api": "^1.0",

src/Enum/EnumeratorCompatibleEntityInterface.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,18 @@ interface EnumeratorCompatibleEntityInterface
2424
*/
2525
public function __construct($owning_entity, string $name, ?string $value);
2626

27+
/**
28+
* @return string
29+
*/
30+
public function getName(): string;
31+
2732
/**
2833
* @return string
2934
*/
3035
public function getValue(): ?string;
3136

3237
/**
33-
* @param string $value
38+
* @param string|null $value
3439
*/
3540
public function setValue($value);
3641
}

src/Resources/templates/enum_class.php.twig

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace {{ namespace }};
55
66
use Doctrine\Common\Collections\Collection;
77
use Doctrine\Common\Collections\Criteria;
8+
use Hostnet\Component\AccessorGenerator\Enum\EnumeratorCompatibleEntityInterface;
89
910
/**
1011
* Generated accessor for enum class {{ enum_class }}.
@@ -17,10 +18,10 @@ class {{ class_name }}
1718
1819
/**
1920
* @param Collection $collection
20-
* @param Entity|* $owning_entity
21+
* @param object $owning_entity
2122
* @param string $parameter_entity_class
2223
*/
23-
public function __construct(Collection $collection, $owning_entity, string $parameter_entity_class)
24+
public function __construct(Collection $collection, object $owning_entity, string $parameter_entity_class)
2425
{
2526
$this->collection = $collection;
2627
$this->owning_entity = $owning_entity;
@@ -67,12 +68,9 @@ class {{ class_name }}
6768
{
6869
$items = $this
6970
->collection
70-
->matching((new Criteria())
71-
->where(Criteria::expr()->eq(
72-
'name',
73-
\{{ info.enumClass }}::{{ info.constName }}
74-
))
75-
);
71+
->filter(function(EnumeratorCompatibleEntityInterface $object) {
72+
return $object->getName() === \{{ info.enumClass }}::{{ info.constName }};
73+
});
7674
7775
if ($items->isEmpty()) {
7876
$item = new $this->parameter_entity_class(
@@ -105,12 +103,9 @@ class {{ class_name }}
105103
{
106104
$items = $this
107105
->collection
108-
->matching((new Criteria())
109-
->where(Criteria::expr()->eq(
110-
'name',
111-
\{{ info.enumClass }}::{{ info.constName }}
112-
))
113-
);
106+
->filter(function(EnumeratorCompatibleEntityInterface $object) {
107+
return $object->getName() === \{{ info.enumClass }}::{{ info.constName }};
108+
});
114109
115110
return $items->isEmpty()
116111
? false
@@ -166,18 +161,15 @@ class {{ class_name }}
166161
/**
167162
* Returns the parameter element for easy access.
168163
*
169-
* @return object
164+
* @return EnumeratorCompatibleEntityInterface
170165
*/
171-
private function get{{ info.methodName }}EntityInstance()
166+
private function get{{ info.methodName }}EntityInstance(): EnumeratorCompatibleEntityInterface
172167
{
173168
$items = $this
174169
->collection
175-
->matching((new Criteria())
176-
->where(Criteria::expr()->eq(
177-
'name',
178-
\{{ info.enumClass }}::{{ info.constName }}
179-
))
180-
);
170+
->filter(function(EnumeratorCompatibleEntityInterface $object) {
171+
return $object->getName() === \{{ info.enumClass }}::{{ info.constName }};
172+
});
181173
182174
return $items->first();
183175
}

0 commit comments

Comments
 (0)