Skip to content

Commit a938cba

Browse files
committed
rewrote resultIterator constructor
1 parent 555d6a9 commit a938cba

File tree

5 files changed

+84
-55
lines changed

5 files changed

+84
-55
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
},
8282
"extra": {
8383
"branch-alias": {
84-
"dev-master": "5.1.x-dev"
84+
"dev-master": "5.2.x-dev"
8585
}
8686
},
8787
"minimum-stability": "dev",

src/InnerResultIterator.php

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,27 +68,37 @@ class InnerResultIterator implements \Iterator, \Countable, \ArrayAccess
6868

6969
protected $count = null;
7070

71+
private function __construct()
72+
{
73+
}
74+
7175
/**
7276
* @param mixed[] $parameters
7377
* @param array[] $columnDescriptors
7478
*/
75-
public function __construct(?string $magicSql, ?array $parameters, ?int $limit, ?int $offset, ?array $columnDescriptors, ?ObjectStorageInterface $objectStorage, ?string $className, ?TDBMService $tdbmService, ?MagicQuery $magicQuery, ?LoggerInterface $logger)
79+
public static function createInnerResultIterator(string $magicSql, array $parameters, ?int $limit, ?int $offset, array $columnDescriptors, ObjectStorageInterface $objectStorage, ?string $className, TDBMService $tdbmService, MagicQuery $magicQuery, LoggerInterface $logger): self
7680
{
77-
if (!$magicSql) {
78-
$this->count = 0;
79-
} else {
80-
$this->magicSql = $magicSql;
81-
$this->objectStorage = $objectStorage;
82-
$this->className = $className;
83-
$this->tdbmService = $tdbmService;
84-
$this->parameters = $parameters;
85-
$this->limit = $limit;
86-
$this->offset = $offset;
87-
$this->columnDescriptors = $columnDescriptors;
88-
$this->magicQuery = $magicQuery;
89-
$this->databasePlatform = $this->tdbmService ? $this->tdbmService->getConnection()->getDatabasePlatform() : null;
90-
}
91-
$this->logger = $logger ?? new NullLogger();
81+
$iterator = new static();
82+
$iterator->magicSql = $magicSql;
83+
$iterator->objectStorage = $objectStorage;
84+
$iterator->className = $className;
85+
$iterator->tdbmService = $tdbmService;
86+
$iterator->parameters = $parameters;
87+
$iterator->limit = $limit;
88+
$iterator->offset = $offset;
89+
$iterator->columnDescriptors = $columnDescriptors;
90+
$iterator->magicQuery = $magicQuery;
91+
$iterator->databasePlatform = $iterator->tdbmService->getConnection()->getDatabasePlatform();
92+
$iterator->logger = $logger;
93+
return $iterator;
94+
}
95+
96+
public static function createEmpyIterator(): self
97+
{
98+
$iterator = new static();
99+
$iterator->count = 0;
100+
$iterator->logger = new NullLogger();
101+
return $iterator;
92102
}
93103

94104
private function getQuery(): string

src/PageIterator.php

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,24 +68,38 @@ class PageIterator implements Page, \ArrayAccess, \JsonSerializable
6868
*/
6969
private $logger;
7070

71+
private function __construct()
72+
{
73+
}
74+
7175
/**
7276
* @param mixed[] $parameters
7377
* @param array[] $columnDescriptors
7478
*/
75-
public function __construct(ResultIterator $parentResult, ?string $magicSql, array $parameters, ?int $limit, ?int $offset, ?array $columnDescriptors, ?ObjectStorageInterface $objectStorage, ?string $className, ?TDBMService $tdbmService, ?MagicQuery $magicQuery, ?int $mode, ?LoggerInterface $logger)
79+
public static function createResultIterator(ResultIterator $parentResult, string $magicSql, array $parameters, int $limit, int $offset, array $columnDescriptors, ObjectStorageInterface $objectStorage, ?string $className, TDBMService $tdbmService, MagicQuery $magicQuery, int $mode, LoggerInterface $logger): self
80+
{
81+
$iterator = new self();
82+
$iterator->parentResult = $parentResult;
83+
$iterator->magicSql = $magicSql;
84+
$iterator->objectStorage = $objectStorage;
85+
$iterator->className = $className;
86+
$iterator->tdbmService = $tdbmService;
87+
$iterator->parameters = $parameters;
88+
$iterator->limit = $limit;
89+
$iterator->offset = $offset;
90+
$iterator->columnDescriptors = $columnDescriptors;
91+
$iterator->magicQuery = $magicQuery;
92+
$iterator->mode = $mode;
93+
$iterator->logger = $logger;
94+
return $iterator;
95+
}
96+
97+
public static function createEmpyIterator(ResultIterator $parentResult): self
7698
{
77-
$this->parentResult = $parentResult;
78-
$this->magicSql = $magicSql;
79-
$this->objectStorage = $objectStorage;
80-
$this->className = $className;
81-
$this->tdbmService = $tdbmService;
82-
$this->parameters = $parameters;
83-
$this->limit = $limit;
84-
$this->offset = $offset;
85-
$this->columnDescriptors = $columnDescriptors;
86-
$this->magicQuery = $magicQuery;
87-
$this->mode = $mode;
88-
$this->logger = $logger ?? new NullLogger();
99+
$iterator = new self();
100+
$iterator->parentResult = $parentResult;
101+
$iterator->logger = new NullLogger();
102+
return $iterator;
89103
}
90104

91105
/**
@@ -102,11 +116,11 @@ public function getIterator()
102116
{
103117
if ($this->innerResultIterator === null) {
104118
if ($this->parentResult->count() === 0) {
105-
$this->innerResultIterator = new InnerResultIterator(null, null, null, null, null, null, null, null, null, null);
119+
$this->innerResultIterator = InnerResultIterator::createEmpyIterator();
106120
} elseif ($this->mode === TDBMService::MODE_CURSOR) {
107-
$this->innerResultIterator = new InnerResultIterator($this->magicSql, $this->parameters, $this->limit, $this->offset, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger);
121+
$this->innerResultIterator = InnerResultIterator::createInnerResultIterator($this->magicSql, $this->parameters, $this->limit, $this->offset, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger);
108122
} else {
109-
$this->innerResultIterator = new InnerResultArray($this->magicSql, $this->parameters, $this->limit, $this->offset, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger);
123+
$this->innerResultIterator = InnerResultArray::createInnerResultIterator($this->magicSql, $this->parameters, $this->limit, $this->offset, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger);
110124
}
111125
}
112126

src/ResultIterator.php

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -67,32 +67,37 @@ class ResultIterator implements Result, \ArrayAccess, \JsonSerializable
6767

6868
private $logger;
6969

70+
private function __construct()
71+
{
72+
}
73+
7074
/**
7175
* @param mixed[] $parameters
7276
*/
73-
public function __construct(?QueryFactory $queryFactory, ?array $parameters, ?ObjectStorageInterface $objectStorage, ?string $className, ?TDBMService $tdbmService, ?MagicQuery $magicQuery, int $mode, ?LoggerInterface $logger)
77+
public static function createResultIterator(QueryFactory $queryFactory, array $parameters, ObjectStorageInterface $objectStorage, ?string $className, TDBMService $tdbmService, MagicQuery $magicQuery, int $mode, LoggerInterface $logger): self
7478
{
79+
$iterator = new self();
7580
if ($mode !== TDBMService::MODE_CURSOR && $mode !== TDBMService::MODE_ARRAY) {
7681
throw new TDBMException("Unknown fetch mode: '".$mode."'");
7782
}
7883

79-
if (!$queryFactory) {
80-
$this->totalCount = 0;
81-
} else {
82-
$this->queryFactory = $queryFactory;
83-
$this->objectStorage = $objectStorage;
84-
$this->className = $className;
85-
$this->tdbmService = $tdbmService;
86-
$this->parameters = $parameters;
87-
$this->magicQuery = $magicQuery;
88-
$this->mode = $mode;
89-
}
90-
$this->logger = $logger ?? new NullLogger();
84+
$iterator->queryFactory = $queryFactory;
85+
$iterator->objectStorage = $objectStorage;
86+
$iterator->className = $className;
87+
$iterator->tdbmService = $tdbmService;
88+
$iterator->parameters = $parameters;
89+
$iterator->magicQuery = $magicQuery;
90+
$iterator->mode = $mode;
91+
$iterator->logger = $logger;
92+
return $iterator;
9193
}
9294

9395
public static function createEmpyIterator(): self
9496
{
95-
return new self(null, null, null, null, null, null, TDBMService::MODE_ARRAY, null);
97+
$iterator = new self();
98+
$iterator->totalCount = 0;
99+
$iterator->logger = new NullLogger();
100+
return $iterator;
96101
}
97102

98103
protected function executeCountQuery(): void
@@ -158,11 +163,11 @@ public function getIterator()
158163
{
159164
if ($this->innerResultIterator === null) {
160165
if ($this->totalCount === 0) {
161-
$this->innerResultIterator = new InnerResultArray(null, null, null, null, null, null, null, null, null, null);
166+
$this->innerResultIterator = InnerResultArray::createEmpyIterator();
162167
} elseif ($this->mode === TDBMService::MODE_CURSOR) {
163-
$this->innerResultIterator = new InnerResultIterator($this->queryFactory->getMagicSql(), $this->parameters, null, null, $this->queryFactory->getColumnDescriptors(), $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger);
168+
$this->innerResultIterator = InnerResultIterator::createInnerResultIterator($this->queryFactory->getMagicSql(), $this->parameters, null, null, $this->queryFactory->getColumnDescriptors(), $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger);
164169
} else {
165-
$this->innerResultIterator = new InnerResultArray($this->queryFactory->getMagicSql(), $this->parameters, null, null, $this->queryFactory->getColumnDescriptors(), $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger);
170+
$this->innerResultIterator = InnerResultArray::createInnerResultIterator($this->queryFactory->getMagicSql(), $this->parameters, null, null, $this->queryFactory->getColumnDescriptors(), $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger);
166171
}
167172
}
168173

@@ -178,9 +183,9 @@ public function getIterator()
178183
public function take($offset, $limit)
179184
{
180185
if ($this->totalCount === 0) {
181-
return new PageIterator($this, null, [], null, null, null, null, null, null, null, null, null);
186+
return PageIterator::createEmpyIterator($this);
182187
}
183-
return new PageIterator($this, $this->queryFactory->getMagicSql(), $this->parameters, $limit, $offset, $this->queryFactory->getColumnDescriptors(), $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->mode, $this->logger);
188+
return PageIterator::createResultIterator($this, $this->queryFactory->getMagicSql(), $this->parameters, $limit, $offset, $this->queryFactory->getColumnDescriptors(), $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->mode, $this->logger);
184189
}
185190

186191
/**

src/TDBMService.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,7 @@ public function findObjects(string $mainTable, $filter = null, array $parameters
11371137

11381138
$queryFactory = new FindObjectsQueryFactory($mainTable, $additionalTablesFetch, $filterString, $orderString, $this, $this->tdbmSchemaAnalyzer->getSchema(), $this->orderByAnalyzer, $this->cache);
11391139

1140-
return new ResultIterator($queryFactory, $parameters, $this->objectStorage, $className, $this, $this->magicQuery, $mode, $this->logger);
1140+
return ResultIterator::createResultIterator($queryFactory, $parameters, $this->objectStorage, $className, $this, $this->magicQuery, $mode, $this->logger);
11411141
}
11421142

11431143
/**
@@ -1170,7 +1170,7 @@ public function findObjectsFromSql(string $mainTable, string $from, $filter = nu
11701170

11711171
$queryFactory = new FindObjectsFromSqlQueryFactory($mainTable, $from, $filterString, $orderString, $this, $this->tdbmSchemaAnalyzer->getSchema(), $this->orderByAnalyzer, $this->schemaAnalyzer, $this->cache, $this->cachePrefix);
11721172

1173-
return new ResultIterator($queryFactory, $parameters, $this->objectStorage, $className, $this, $this->magicQuery, $mode, $this->logger);
1173+
return ResultIterator::createResultIterator($queryFactory, $parameters, $this->objectStorage, $className, $this, $this->magicQuery, $mode, $this->logger);
11741174
}
11751175

11761176
/**
@@ -1334,7 +1334,7 @@ public function findObjectsFromRawSql(string $mainTable, string $sql, array $par
13341334

13351335
$queryFactory = new FindObjectsFromRawSqlQueryFactory($this, $this->tdbmSchemaAnalyzer->getSchema(), $mainTable, $sql, $sqlCount);
13361336

1337-
return new ResultIterator($queryFactory, $parameters, $this->objectStorage, $className, $this, $this->magicQuery, $mode, $this->logger);
1337+
return ResultIterator::createResultIterator($queryFactory, $parameters, $this->objectStorage, $className, $this, $this->magicQuery, $mode, $this->logger);
13381338
}
13391339

13401340
/**

0 commit comments

Comments
 (0)