Skip to content

Commit fb78102

Browse files
committed
Implement ArrayStorage package
1 parent cf98197 commit fb78102

File tree

7 files changed

+712
-0
lines changed

7 files changed

+712
-0
lines changed

README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,96 @@ using [Composer](http://getcomposer.org).
1515
```bash
1616
composer require phpfluent/arraystorage
1717
```
18+
19+
# Usage
20+
21+
The examples below are using the following use statement at the beginning of the file:
22+
23+
```php
24+
use PHPFluent\ArrayStorage\Storage;
25+
```
26+
27+
## Creating and returning a collection
28+
29+
```php
30+
$storage = new Storage();
31+
$storage->users; // This is a collection
32+
```
33+
34+
## Inserting records to a collection
35+
36+
You can use a single array:
37+
38+
```php
39+
$storage = new Storage();
40+
$storage->users->insert(array('name' => 'Henrique Moody'));
41+
```
42+
43+
But you also can use a Record object:
44+
45+
```php
46+
use PHPFluent\ArrayStorage\Record;
47+
48+
$storage = new Storage();
49+
$record = new Record();
50+
$record->name = 'Henrique Moody';
51+
52+
$storage->users->insert($record);
53+
```
54+
55+
You can create a Record object from Storage object:
56+
57+
```php
58+
$storage = new Storage();
59+
$record = $storage->record();
60+
$record->name = 'Henrique Moody';
61+
62+
$storage->users->insert($record);
63+
```
64+
65+
An important point to note is that, after you insert the record to the collection object
66+
it gives to the record an unique (incremental integer) `id` property.
67+
68+
## Removing records from a collection
69+
70+
```php
71+
$storage = new Storage();
72+
$storage->users->delete(array('name' => 'Henrique Moody'));
73+
```
74+
75+
## Removing all records from a collection
76+
77+
```php
78+
$storage = new Storage();
79+
$storage->users->delete();
80+
```
81+
82+
## Converting collection to an array
83+
84+
```php
85+
$storage = new Storage();
86+
$collectionData = $storage->users->toArray();
87+
```
88+
89+
The example above convert all Record objects that are children of Record objects into an string, but returning its `id` property.
90+
91+
## Converting collection to an expanded array
92+
93+
```php
94+
$storage = new Storage();
95+
$collectionData = $storage->users->toArray();
96+
```
97+
98+
The example above convert all Record objects into an array.
99+
100+
## Finding multiple records into a collection
101+
102+
```php
103+
$storage->users->findAll(array('name' => 'Henrique Moody')); // Return an Collection object with the partial result (if any)
104+
```
105+
106+
## Finding single record into a collection
107+
108+
```php
109+
$storage->users->find(array('name' => 'Henrique Moody')); // Return an Record object with the first matched result (if any) or NULL
110+
```

src/Collection.php

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
3+
namespace PHPFluent\ArrayStorage;
4+
5+
use ArrayIterator;
6+
use IteratorAggregate;
7+
use Countable;
8+
9+
class Collection implements Countable, IteratorAggregate
10+
{
11+
protected $name;
12+
protected $records = array();
13+
protected $lastRecordId = 0;
14+
15+
public function __construct($name, array $records = array())
16+
{
17+
$this->name = $name;
18+
foreach ($records as $record) {
19+
$this->insert($record);
20+
}
21+
}
22+
23+
public function getName()
24+
{
25+
return $this->name;
26+
}
27+
28+
public function record($record)
29+
{
30+
if (! $record instanceof Record) {
31+
$data = (array) $record;
32+
$record = new Record($data);
33+
}
34+
35+
return $record;
36+
}
37+
38+
public function count()
39+
{
40+
return count($this->records);
41+
}
42+
43+
public function insert($data)
44+
{
45+
$record = $this->record($data);
46+
if (0 >= $record->id) {
47+
$record->id = ++$this->lastRecordId;
48+
}
49+
50+
$this->records[] = $record;
51+
52+
return $record->id;
53+
}
54+
55+
public function update(array $update, array $criteria = array())
56+
{
57+
$records = $this->findAll($criteria);
58+
foreach ($records as $record) {
59+
$record->update($update);
60+
}
61+
}
62+
63+
public function delete(array $criteria = array())
64+
{
65+
foreach ($this->records as $key => $record) {
66+
if (! $this->equals($criteria, $record)) {
67+
continue;
68+
}
69+
70+
unset($this->records[$key]);
71+
}
72+
}
73+
74+
public function getIterator()
75+
{
76+
return new ArrayIterator($this->records);
77+
}
78+
79+
protected function equals(array $values, Record $record)
80+
{
81+
foreach ($values as $key => $value) {
82+
if ((string) $record->__get($key) == (string) $value) {
83+
continue;
84+
}
85+
86+
return false;
87+
}
88+
89+
return true;
90+
}
91+
92+
public function findAll(array $criteria = array(), $limit = null)
93+
{
94+
$count = 0;
95+
$results = array();
96+
foreach ($this->records as $record) {
97+
if (null !== $limit && $count == $limit) {
98+
continue;
99+
}
100+
101+
if (! $this->equals($criteria, $record)) {
102+
continue;
103+
}
104+
105+
$count++;
106+
$results[] = $record;
107+
}
108+
109+
$name = $this->name . '-' . json_encode($criteria);
110+
111+
return new static($name, $results);
112+
}
113+
114+
public function find(array $criteria = array())
115+
{
116+
$records = $this->findAll($criteria, 1);
117+
118+
return $records->getIterator()->current();
119+
}
120+
121+
public function toArray($expandChildren = false)
122+
{
123+
$results = array();
124+
foreach ($this->records as $record) {
125+
$results[] = $record->toArray($expandChildren);
126+
}
127+
128+
return $results;
129+
}
130+
}

src/Record.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace PHPFluent\ArrayStorage;
4+
5+
class Record
6+
{
7+
protected $data = array('id' => null);
8+
9+
public function __construct(array $data = array())
10+
{
11+
foreach ($data as $name => $value) {
12+
$this->__set($name, $value);
13+
}
14+
}
15+
16+
public function __set($name, $value)
17+
{
18+
$this->data[$name] = $value;
19+
}
20+
21+
public function __get($name)
22+
{
23+
if (! isset($this->data[$name])) {
24+
$this->data[$name] = null;
25+
}
26+
27+
return $this->data[$name];
28+
}
29+
30+
public function __toString()
31+
{
32+
return (string) $this->__get('id');
33+
}
34+
35+
public function update(array $update)
36+
{
37+
foreach ($update as $key => $value) {
38+
$this->__set($key, $value);
39+
}
40+
}
41+
42+
public function toArray($expandChildren = false)
43+
{
44+
$result = array();
45+
foreach ($this->data as $key => $value) {
46+
if ($value instanceof static && true === $expandChildren) {
47+
$value = $value->toArray($expandChildren);
48+
} elseif ($value instanceof static && true !== $expandChildren) {
49+
$value = (int) $value->__toString() ?: null;
50+
}
51+
52+
$result[$key] = $value;
53+
}
54+
55+
return $result;
56+
}
57+
}

src/Storage.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace PHPFluent\ArrayStorage;
4+
5+
use ArrayIterator;
6+
use Countable;
7+
use IteratorAggregate;
8+
9+
class Storage implements IteratorAggregate, Countable
10+
{
11+
protected $collections = array();
12+
13+
public function __get($name)
14+
{
15+
if (! isset($this->collections[$name])) {
16+
$this->collections[$name] = new Collection($name);
17+
}
18+
19+
return $this->collections[$name];
20+
}
21+
22+
public function count()
23+
{
24+
return count($this->collections);
25+
}
26+
27+
public function getIterator()
28+
{
29+
return new ArrayIterator($this->collections);
30+
}
31+
}

0 commit comments

Comments
 (0)