Skip to content
This repository was archived by the owner on Feb 1, 2023. It is now read-only.

Commit 3bbbaf0

Browse files
EtienneEtienne
authored andcommitted
Send needleFactory to it's own class
1 parent 4befe5a commit 3bbbaf0

File tree

5 files changed

+30
-17
lines changed

5 files changed

+30
-17
lines changed

src/Iterators/JsonFileIterator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ class JsonFileIterator extends JsonIterator
1212

1313
protected $jsonFileHandle;
1414

15-
public function __construct($jsonFileHandle, array $options = null)
15+
public function __construct(NeedleFactory $needleFactory, $jsonFileHandle, array $options = null)
1616
{
1717
$this->jsonFileHandle = $jsonFileHandle;
1818

1919
$jsonString = file_get_contents($jsonFileHandle);
20-
parent::__construct($jsonString, $options);
20+
parent::__construct($needleFactory, $jsonString, $options);
2121
}
2222

2323
public function __destruct()

src/Iterators/JsonFilesIterator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
class JsonFilesIterator extends AppendIterator
1313
{
1414

15-
public function __construct($jsonFileHandles, array $options = null)
15+
public function __construct(NeedleFactory $needleFactory, array $jsonFileHandles, array $options = null)
1616
{
1717
parent::__construct();
1818

1919
foreach ($jsonFileHandles as $jsonFileHandle)
2020
{
21-
$this->append(new JsonFileIterator($jsonFileHandle, $options));
21+
$this->append(new JsonFileIterator($needleFactory, $jsonFileHandle, $options));
2222
}
2323
}
2424

src/Iterators/JsonIterator.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ class JsonIterator implements Iterator
1717
protected $cursorPosition;
1818
protected $nextCursorPosition;
1919

20-
public function __construct($jsonString, array $options = null)
20+
public function __construct(NeedleFactory $needleFactory, $jsonString, array $options = null)
2121
{
2222
$this->jsonString = $jsonString;
2323

2424
$this->setDefaultOptions($options);
25-
$this->needleFactory($options['firstTopLevelString']);
25+
$this->needle = $needleFactory->build();
2626

2727
if ($options['jsonHasSquareBrackets'])
2828
{
@@ -79,16 +79,6 @@ public function valid()
7979
return true;
8080
}
8181

82-
protected function needleFactory($firstTopLevelString = null)
83-
{
84-
$this->needle = ',{';
85-
86-
if ($firstTopLevelString != null)
87-
{
88-
$this->needle .= '"' . $firstTopLevelString . '"';
89-
}
90-
}
91-
9282
protected function getNextCursorPosition()
9383
{
9484
$this->nextCursorPosition = strpos($this->jsonString, $this->needle, $this->cursorPosition);

src/JsonIteratorFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static function buildJsonFileIterator($jsonFileHandle, array $options = n
2424
return new JsonFileIterator($jsonFileHandle, $options);
2525
}
2626

27-
public static function buildJsonFilesIterator($jsonFileHandles, array $options = null)
27+
public static function buildJsonFilesIterator(array $jsonFileHandles, array $options = null)
2828
{
2929
return new JsonFilesIterator($jsonFileHandles, $options);
3030
}

src/NeedleFactory.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Crystalgorithm\PhpJsonIterator;
4+
5+
class NeedleFactory
6+
{
7+
8+
public function __construct()
9+
{
10+
11+
}
12+
13+
function build($firstTopLevelString = null)
14+
{
15+
$this->needle = ',{';
16+
17+
if ($firstTopLevelString != null)
18+
{
19+
$this->needle .= '"' . $firstTopLevelString . '"';
20+
}
21+
}
22+
23+
}

0 commit comments

Comments
 (0)