Skip to content

Commit

Permalink
add next method to input class
Browse files Browse the repository at this point in the history
  • Loading branch information
Aljar committed Mar 15, 2016
1 parent 0e7084f commit feb29da
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
3 changes: 2 additions & 1 deletion extension.cpp
Expand Up @@ -161,7 +161,8 @@ extern "C" {
Php::ByVal("bytes", Php::Type::Numeric, false)
}).method("name", &Input::name, {
}).method("size", &Input::size)
.method("valid", &Input::valid);
.method("valid", &Input::valid)
.method("next", &Input::next);

// register record methods
record.method("identifier", &Record::identifier, {
Expand Down
35 changes: 35 additions & 0 deletions input.h
Expand Up @@ -42,6 +42,13 @@ class Input : public Php::Base, public Php::Traversable
*/
std::size_t _bytes;

/**
* The input object that is used if the next() method is called
* @var std::shared_ptr<Yothalot::Input>
*/
std::shared_ptr<Yothalot::Input> _input;


public:
/**
* The PHP constructor
Expand Down Expand Up @@ -117,5 +124,33 @@ class Input : public Php::Base, public Php::Traversable
// construct the new iterator
return new InputIterator(this, _name, _seek, _bytes);
}

/**
* Get the next record
* @return Php::Value
*/
Php::Value next()
{
// prevent exceptions
try
{
// do we already have an input object?
if (_input == nullptr) _input = std::make_shared<Yothalot::Input>(_name.data(), _seek, _bytes);

// object must be valid
if (!_input->valid()) return nullptr;

// construct a yothalot record
auto record = std::make_shared<Yothalot::Record>(*_input);

// return object
return Php::Object("Yothalot\\Record", new Record(record));
}
catch (...)
{
// object is in an invalid state
return nullptr;
}
}
};

0 comments on commit feb29da

Please sign in to comment.