Skip to content

Commit 63a6fa4

Browse files
committed
UseQuery doesn't handle properties anymore, only relations
1 parent 8ffe2e4 commit 63a6fa4

File tree

3 files changed

+81
-71
lines changed

3 files changed

+81
-71
lines changed

src/Service/QueryModifier/Easy/Modifier.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,13 @@ private function after()
112112
*/
113113
private function before(string $dotProperty)
114114
{
115-
$this->dotUseQuery = new UseQueryFromDotNotation($this->query, $dotProperty);
116-
$property = $this->dotUseQuery->getProperty();
117-
$this->query = $this->dotUseQuery->useQuery();
115+
116+
$dotProperty = trim($dotProperty, UseQueryFromDotNotation::RELATION_SEP);
117+
$parts = explode(UseQueryFromDotNotation::RELATION_SEP, $dotProperty);
118+
$property = ucfirst(array_pop($parts));
119+
120+
$this->dotUseQuery = new UseQueryFromDotNotation($this->query);
121+
$this->query = $this->dotUseQuery->fromArray($parts)->useQuery();
118122

119123
return $property;
120124
}

src/Service/QueryModifier/UseQuery/UseQueryFromDotNotation.php

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ class UseQueryFromDotNotation
1919
* @var int
2020
*/
2121
protected $depth = 0;
22-
/**
23-
* @var string
24-
*/
25-
protected $dotProperty;
2622
/**
2723
* @var bool
2824
*/
@@ -31,31 +27,54 @@ class UseQueryFromDotNotation
3127
* @var array
3228
*/
3329
protected $map = [];
34-
/**
35-
* @var string
36-
*/
37-
protected $property;
3830
/**
3931
* @var ModelCriteria
4032
*/
4133
protected $query;
4234

43-
public function __construct(ModelCriteria $query, string $dotProperty)
35+
/**
36+
* UseQueryFromDotNotation constructor.
37+
* @param ModelCriteria $query
38+
*/
39+
public function __construct(ModelCriteria $query)
4440
{
45-
$cleanDotProperty = trim($dotProperty);
46-
# Remove first dot, in this case this is NO relation, only property
47-
$cleanDotProperty = ltrim($cleanDotProperty, ".");
41+
$this->query = $query;
42+
}
43+
44+
/**
45+
* @param array $relations
46+
* @return UseQueryFromDotNotation
47+
*/
48+
public function fromArray(array $relations): self
49+
{
50+
if (empty($relations)) {
51+
return $this;
52+
}
4853

49-
$this->map = explode(self::RELATION_SEP, $cleanDotProperty);
54+
$this->map = $relations;
55+
$this->depth = count($this->map);
5056

51-
$this->query = $query;
52-
$this->dotProperty = $cleanDotProperty;
53-
$this->property = array_pop($this->map);
54-
$this->depth = count($this->map);
57+
return $this;
58+
}
5559

56-
if (empty($this->property)) {
57-
throw new \InvalidArgumentException("Property is empty in \"$dotProperty\"");
60+
/**
61+
* @param string $relations
62+
* @return UseQueryFromDotNotation
63+
*/
64+
public function fromString(string $relations): self
65+
{
66+
$relations = trim($relations);
67+
# Remove first dot, in this case this is NO relation, only property
68+
$relations = ltrim($relations, ".");
69+
70+
if (empty($relations)) {
71+
return $this;
5872
}
73+
74+
$this->map = explode(self::RELATION_SEP, $relations);
75+
$this->depth = count($this->map);
76+
77+
return $this;
5978
}
6079

6180
/**
@@ -86,14 +105,6 @@ public function getDepth(): int
86105
return $this->depth;
87106
}
88107

89-
/**
90-
* @return string ucfirst string
91-
*/
92-
public function getProperty(): string
93-
{
94-
return ucfirst($this->property);
95-
}
96-
97108
/**
98109
* @return bool
99110
*/
@@ -103,10 +114,11 @@ public function isInUse(): bool
103114
}
104115

105116
/**
117+
* @param null $alias
106118
* @return ModelCriteria
107119
* @throws UseQueryFromDotNotationException
108120
*/
109-
public function useQuery(): ModelCriteria
121+
public function useQuery($alias = null): ModelCriteria
110122
{
111123
if ($this->inUse) {
112124
throw new UseQueryFromDotNotationException("A query is already in use and have not be terminated with UseQueryFromDotNotation::endUse()");
@@ -116,9 +128,10 @@ public function useQuery(): ModelCriteria
116128
foreach ($this->map as $relation) {
117129
$method = sprintf('use%sQuery', ucfirst($relation));
118130
if (!method_exists($this->query, $method)) {
119-
throw new RelationNotFoundException("Relation \"$relation\" Not Found in \"$this->dotProperty\"");
131+
$path = implode(self::RELATION_SEP, $this->map);
132+
throw new RelationNotFoundException("Relation \"$relation\" Not Found in \"$path\"");
120133
}
121-
$this->query = call_user_func([$this->query, $method]);
134+
$this->query = call_user_func([$this->query, $method], $alias);
122135
}
123136
}
124137
$this->inUse = true;

tests/Service/QueryModifier/UseQuery/UseQueryFromDotNotationTest.php

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,19 @@ public function testEndUse()
3333
{
3434

3535
$query = $this->mockQueryInstance();
36-
$use = new UseQueryFromDotNotation($query, "aName");
37-
$use->useQuery();
38-
$this->assertSame(0, $use->getDepth());
39-
$usedQuery = $use->endUse();
40-
$this->assertInstanceOf(\AQuery::class, $usedQuery);
41-
$this->assertSame("AName", $use->getProperty());
42-
$this->assertFalse($use->isInUse());
4336

44-
$use = new UseQueryFromDotNotation($query, "b.bName");
45-
$use->useQuery();
37+
$use = new UseQueryFromDotNotation($query);
38+
$use->fromString("b")->useQuery();
4639
$this->assertSame(1, $use->getDepth());
4740
$usedQuery = $use->endUse();
4841
$this->assertInstanceOf(\AQuery::class, $usedQuery);
49-
$this->assertSame("BName", $use->getProperty());
5042
$this->assertFalse($use->isInUse());
5143

52-
$use = new UseQueryFromDotNotation($query, "b.c.cName");
53-
$use->useQuery();
44+
$use = new UseQueryFromDotNotation($query);
45+
$use->fromString("b.c")->useQuery();
5446
$this->assertSame(2, $use->getDepth());
5547
$usedQuery = $use->endUse();
5648
$this->assertInstanceOf(\AQuery::class, $usedQuery);
57-
$this->assertSame("CName", $use->getProperty());
5849
$this->assertFalse($use->isInUse());
5950
}
6051

@@ -64,7 +55,7 @@ public function testEndUse()
6455
public function testEndUseBeforeUse()
6556
{
6657
$query = $this->mockQueryInstance();
67-
$use = new UseQueryFromDotNotation($query, "zzz.foo");
58+
$use = new UseQueryFromDotNotation($query);
6859
$this->expectException(UseQueryFromDotNotationException::class);
6960
$use->endUse();
7061
}
@@ -82,21 +73,27 @@ public function testSeparatorValue()
8273
*/
8374
public function testUseQuery()
8475
{
85-
$query = $this->mockQueryInstance();
86-
$use = new UseQueryFromDotNotation($query, "foo");
87-
$usedQuery = $use->useQuery();
88-
$this->assertInstanceOf(\AQuery::class, $usedQuery);
89-
$this->assertTrue(method_exists($usedQuery, "filterByAName"));
76+
// $query = $this->mockQueryInstance();
77+
// $use = new UseQueryFromDotNotation($query, "foo");
78+
// $usedQuery = $use->useQuery();
79+
// $this->assertInstanceOf(\AQuery::class, $usedQuery);
80+
// $this->assertTrue(method_exists($usedQuery, "filterByAName"));
9081

9182
$query = $this->mockQueryInstance();
92-
$use = new UseQueryFromDotNotation($query, "b.foo");
93-
$usedQuery = $use->useQuery();
83+
$use = new UseQueryFromDotNotation($query);
84+
$usedQuery = $use->fromString("b")->useQuery();
85+
$this->assertInstanceOf(\BQuery::class, $usedQuery);
86+
$this->assertTrue(method_exists($usedQuery, "filterByBName"));
87+
88+
$query = $this->mockQueryInstance();
89+
$use = new UseQueryFromDotNotation($query);
90+
$usedQuery = $use->fromArray(["b"])->useQuery();
9491
$this->assertInstanceOf(\BQuery::class, $usedQuery);
9592
$this->assertTrue(method_exists($usedQuery, "filterByBName"));
9693

9794
$query = $this->mockQueryInstance();
98-
$use = new UseQueryFromDotNotation($query, "b.c.foo");
99-
$usedQuery = $use->useQuery();
95+
$use = new UseQueryFromDotNotation($query);
96+
$usedQuery = $use->fromString("b.c")->useQuery();
10097
$this->assertInstanceOf(\CQuery::class, $usedQuery);
10198
$this->assertTrue(method_exists($usedQuery, "filterByCName"));
10299
}
@@ -107,8 +104,8 @@ public function testUseQuery()
107104
public function testUseQueryAlreadyInUse()
108105
{
109106
$query = $this->mockQueryInstance();
110-
$use = new UseQueryFromDotNotation($query, "b.foo");
111-
$use->useQuery();
107+
$use = new UseQueryFromDotNotation($query);
108+
$use->fromString("b")->useQuery();
112109
$this->expectException(UseQueryFromDotNotationException::class);
113110
$use->useQuery();
114111
}
@@ -119,30 +116,26 @@ public function testUseQueryAlreadyInUse()
119116
public function testUseQueryRelationNotFound()
120117
{
121118
$query = $this->mockQueryInstance();
122-
$use = new UseQueryFromDotNotation($query, "zzz.foo");
119+
$use = new UseQueryFromDotNotation($query);
123120
$this->expectException(RelationNotFoundException::class);
124-
$use->useQuery();
125-
}
126-
127-
public function testUseQueryWithBadProperty()
128-
{
129-
$query = $this->mockQueryInstance();
130-
$this->expectException(\InvalidArgumentException::class);
131-
new UseQueryFromDotNotation($query, "b.");
121+
$use->fromString("zzz")->useQuery();
132122
}
133123

134124
public function testUseQueryWithDotBeforeProperty()
135125
{
136126
$query = $this->mockQueryInstance();
137-
$use = new UseQueryFromDotNotation($query, ".b");
138-
$this->assertSame(0, $use->getDepth());
127+
$use = new UseQueryFromDotNotation($query);
128+
$use->fromString(".b");
129+
$this->assertSame(1, $use->getDepth());
139130
}
140131

141132
public function testUseQueryWithEmptyProperty()
142133
{
143134
$query = $this->mockQueryInstance();
144-
$this->expectException(\InvalidArgumentException::class);
145-
new UseQueryFromDotNotation($query, "");
135+
$use = new UseQueryFromDotNotation($query);
136+
$use->fromString("");
137+
$this->assertSame(0, $use->getDepth());
138+
146139
}
147140

148141
/**

0 commit comments

Comments
 (0)