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

Commit 3549e2c

Browse files
author
Etienne Lamoureux
committed
Added Iterator classes
1 parent a10d1e1 commit 3549e2c

File tree

3 files changed

+163
-0
lines changed

3 files changed

+163
-0
lines changed

src/iterator/JsonFileIterator.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* @author Etienne Lamoureux <etienne.lamoureux@crystalgorithm.com>
5+
* @copyright 2014 Etienne Lamoureux
6+
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
7+
*/
8+
namespace Crystalgorithm\DurmandScriptorium\utils;
9+
10+
class JsonFileIterator extends JsonIterator
11+
{
12+
13+
protected $jsonFileHandle;
14+
15+
public function __construct($jsonFileHandle, $firstTopLevelString = null, $jsonStringHasSquareBrackets = true)
16+
{
17+
$this->jsonFileHandle = $jsonFileHandle;
18+
19+
$jsonString = file_get_contents($jsonFileHandle);
20+
parent::__construct($jsonString, $firstTopLevelString, $jsonStringHasSquareBrackets);
21+
}
22+
23+
public function __destruct()
24+
{
25+
parent::__destruct();
26+
//unlink($this->jsonFileHandle);
27+
}
28+
29+
}

src/iterator/JsonFilesIterator.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* @author Etienne Lamoureux <etienne.lamoureux@crystalgorithm.com>
5+
* @copyright 2014 Etienne Lamoureux
6+
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
7+
*/
8+
namespace Crystalgorithm\DurmandScriptorium\utils;
9+
10+
use AppendIterator;
11+
12+
class JsonFilesIterator extends AppendIterator
13+
{
14+
15+
public function __construct($jsonFileHandles, $firstTopLevelString = null, $jsonStringHasSquareBrackets = true)
16+
{
17+
parent::__construct();
18+
19+
foreach ($jsonFileHandles as $jsonFileHandle)
20+
{
21+
$this->append(new JsonFileIterator($jsonFileHandle, $firstTopLevelString, $jsonStringHasSquareBrackets));
22+
}
23+
}
24+
25+
}

src/iterator/JsonIterator.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
/*
4+
* @author Etienne Lamoureux <etienne.lamoureux@crystalgorithm.com>
5+
* @copyright 2014 Etienne Lamoureux
6+
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
7+
*/
8+
namespace Crystalgorithm\DurmandScriptorium\utils;
9+
10+
use Iterator;
11+
12+
class JsonIterator implements Iterator
13+
{
14+
15+
protected $needle;
16+
protected $jsonString;
17+
protected $cursorPosition;
18+
protected $nextCursorPosition;
19+
20+
public function __construct($jsonString, $firstTopLevelString = null, $jsonStringHasSquareBrackets = false)
21+
{
22+
$this->jsonString = $jsonString;
23+
24+
if ($jsonStringHasSquareBrackets)
25+
{
26+
$this->jsonString = substr($this->jsonString, 1, -1);
27+
}
28+
29+
$this->needleFactory($firstTopLevelString);
30+
}
31+
32+
/**
33+
* {@inheritDoc}
34+
*/
35+
public function current()
36+
{
37+
$this->getNextCursorPosition();
38+
$elementLength = ($this->nextCursorPosition - $this->cursorPosition);
39+
$currentElement = substr($this->jsonString, $this->cursorPosition, $elementLength);
40+
41+
return json_decode($currentElement, true);
42+
}
43+
44+
/**
45+
* {@inheritDoc}
46+
*/
47+
public function key()
48+
{
49+
return null;
50+
}
51+
52+
/**
53+
* {@inheritDoc}
54+
*/
55+
public function next()
56+
{
57+
$this->cursorPosition = ($this->nextCursorPosition + 1);
58+
}
59+
60+
/**
61+
* {@inheritDoc}
62+
*/
63+
public function rewind()
64+
{
65+
$this->cursorPosition = 0;
66+
}
67+
68+
/**
69+
* {@inheritDoc}
70+
*/
71+
public function valid()
72+
{
73+
if ($this->cursorPosition >= strlen($this->jsonString))
74+
{
75+
return false;
76+
}
77+
78+
return true;
79+
}
80+
81+
protected function needleFactory($firstTopLevelString = null)
82+
{
83+
$this->needle = ',{';
84+
85+
if ($firstTopLevelString != null)
86+
{
87+
$this->needle .= '"' . $firstTopLevelString . '"';
88+
}
89+
}
90+
91+
protected function getNextCursorPosition()
92+
{
93+
$this->nextCursorPosition = strpos($this->jsonString, $this->needle, $this->cursorPosition);
94+
95+
if ($this->nextCursorPosition == false)
96+
{
97+
$this->nextCursorPosition = strlen($this->jsonString);
98+
}
99+
100+
return $this->nextCursorPosition;
101+
}
102+
103+
public function __destruct()
104+
{
105+
unset($this->jsonString);
106+
$this->jsonString = '';
107+
}
108+
109+
}

0 commit comments

Comments
 (0)