Skip to content
This repository has been archived by the owner on May 28, 2021. It is now read-only.

Commit

Permalink
Add record iterator and simple model implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
nielssp committed May 12, 2016
1 parent c298b56 commit a518b0c
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Database/DatabaseSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ public function __construct(Database $database)
public function __get($model)
{
if (!isset($this->models[$model])) {
$this->models[$model] = new SimpleModel($this->database->$model, $this->definition->getDefinition($model));
$this->models[$model] = new SimpleModel(
$model,
$this->database->$model,
$this->definition->getDefinition($model)
);
}
return $this->models[$model];
}
Expand Down
8 changes: 8 additions & 0 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ public function create(array $data = array(), $allowedFields = null);
* @return Record A record.
*/
public function open(array $data, Query\ReadSelection $selection);

/**
* Like {@see readSelection}, but returns a {@see Record} iterator.
*
* @param \Jivoo\Data\Query\ReadSelection $selection Read selection.
* @return \Iterator A {@see Record} iterator.
*/
public function openSelection(Query\ReadSelection $selection);

/**
* Make a selection that selects a single record.
Expand Down
8 changes: 8 additions & 0 deletions src/ModelBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ public function open(array $data, Query\ReadSelection $selection)
return RecordBuilder::open($this, $data, $virtual);
}

/**
* {@inheritDoc}
*/
public function openSelection(Query\ReadSelection $selection)
{
return new RecordIterator($this->readSelection($selection), $this, $selection);
}

/**
* {@inheritDoc}
*/
Expand Down
82 changes: 82 additions & 0 deletions src/RecordIterator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
// Jivoo Data
// Copyright (c) 2016 Niels Sonnich Poulsen (http://nielssp.dk)
// Licensed under the MIT license.
// See the LICENSE file or http://opensource.org/licenses/MIT for more information.
namespace Jivoo\Data;

/**
* An iterator that converts associative arrays to {@see Record}s.
*/
class RecordIterator implements \Iterator
{

/**
* @var \Iterator
*/
private $data;

/**
* @var Model
*/
private $model;

/**
* @var Query\ReadSelection
*/
private $selection;

/**
* Construct iterator.
*
* @param \Iterator $data Data iterator.
* @param \Jivoo\Data\Model $model Model.
* @param \Jivoo\Data\Query\ReadSelection $selection Read selection.
*/
public function __construct(\Iterator $data, Model $model, \Jivoo\Data\Query\ReadSelection $selection)
{
$this->data = $data;
$this->model = $model;
$this->selection = $selection;
}

/**
* {@inheritdoc}
*/
public function current()
{
return $this->model->open($this->data->current(), $this->selection);
}

/**
* {@inheritdoc}
*/
public function key()
{
return $this->data->key();
}

/**
* {@inheritdoc}
*/
public function next()
{
$this->data->next();
}

/**
* {@inheritdoc}
*/
public function rewind()
{
$this->data->rewind();
}

/**
* {@inheritdoc}
*/
public function valid()
{
return $this->data->valid();
}
}
114 changes: 114 additions & 0 deletions src/SimpleModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php
// Jivoo Data
// Copyright (c) 2016 Niels Sonnich Poulsen (http://nielssp.dk)
// Licensed under the MIT license.
// See the LICENSE file or http://opensource.org/licenses/MIT for more information.
namespace Jivoo\Data;

/**
* A simple {@see Model} implementation.
*/
class SimpleModel extends ModelBase
{

/**
* @var string
*/
private $name;

/**
* @var DataSource
*/
private $source;

/**
* @var Definition
*/
private $definition;

/**
* Construct simple model.
*
* @param type $name Model name.
* @param \Jivoo\Data\DataSource $source Data source.
* @param \Jivoo\Data\Jivoo\Data\Definition $definition Data definition.
*/
public function __construct($name, DataSource $source, Jivoo\Data\Definition $definition)
{
$this->name = $name;
$this->source = $source;
$this->definition = $definition;
}

/**
* {@inheritdoc}
*/
public function countSelection(Query\ReadSelection $selection)
{
return $this->source->countSelection($selection);
}

/**
* {@inheritdoc}
*/
public function deleteSelection(Query\Selection $selection)
{
return $this->source->deleteSelection($selection);
}

/**
* {@inheritdoc}
*/
public function getDefinition()
{
return $this->definition;
}

/**
* {@inheritdoc}
*/
public function getName()
{
return $this->name;
}

/**
* {@inheritdoc}
*/
public function insert(array $data, $replace = false)
{
return $this->source->insert($data, $replace);
}

/**
* {@inheritdoc}
*/
public function insertMultiple(array $records, $replace = false)
{
return $this->source->insertMultiple($records, $replace);
}

/**
* {@inheritdoc}
*/
public function joinWith(DataSource $other)
{
return $this->source->joinWith($other);
}

/**
* {@inheritdoc}
*/
public function readSelection(Query\ReadSelection $selection)
{
return $this->source->readSelection($selection);
}

/**
* {@inheritdoc}
*/
public function updateSelection(Query\UpdateSelection $selection)
{
return $this->source->updateSelection($selection);
}
}

0 comments on commit a518b0c

Please sign in to comment.