Skip to content

Commit

Permalink
Document all methods
Browse files Browse the repository at this point in the history
  • Loading branch information
abellion committed May 25, 2017
1 parent 7ca4282 commit 2b73988
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,19 @@ class Document implements Iterator, ArrayAccess, JsonSerializable, Serializable,
protected $withId = false;
protected $document = [];

/**
* Returns the document's values for debugging
*
* @return array The document's values
*/
public function __debugInfo()
{
return $this->document;
}

/**
* @param array $document The values to put in the document
*/
public function __construct($document = [])
{
if ($this->withId && !isset($document['_id'])) {
Expand All @@ -35,26 +43,59 @@ public function __construct($document = [])
self::fillFromSetter(($document instanceof self) ? $document->document : $document);
}

/**
* Gets the value for the given key
*
* @param string $offset The key
*
* @return mixed The value
*/
public function get(string $offset)
{
return $this->document[$offset];
}

/**
* Gets whether the given key exists or not
*
* @param string $offset The key
*
* @return boolean Whether the given key exists or not
*/
public function has(string $offset)
{
return isset($this->document[$offset]);
}

/**
* Fills the document whith the given values
*
* @param array $document The values
*
* @return self
*/
public function merge(array $document)
{
return self::fillFromSetter($document);
}

/**
* Returns the document as an array
*
* @return array The document as an array
*/
public function toArray()
{
return $this->document;
}

/**
* Gets a value from a getter if it exists, or fallback on the internal array
*
* @param string $offset The key
*
* @return mixed The value
*/
public function getFromGetter(string $offset)
{
$getter = $this->getterIze($offset);
Expand All @@ -66,13 +107,25 @@ public function getFromGetter(string $offset)
return self::get($offset);
}

/**
* Sets the value on the offset
*
* @param string $offset The key to retrieve the value
* @param mixed $value The value
*/
public function set(string $offset, $value)
{
$this->document[$offset] = $value;

return $this;
}

/**
* Sets the value through a setter
*
* @param string $offset The key to retrieve the value
* @param mixed $value The value
*/
public function setFromSetter(string $offset, $value)
{
$setter = $this->setterIze($offset);
Expand All @@ -84,6 +137,13 @@ public function setFromSetter(string $offset, $value)
return self::set($offset, $value);
}

/**
* Fills the document with the given array
*
* @param array $document The array
*
* @return self
*/
public function fill(array $document)
{
foreach ($document as $offset => $value) {
Expand All @@ -93,6 +153,13 @@ public function fill(array $document)
return $this;
}

/**
* Fills the document, though the setters, with the given array
*
* @param array $document The array
*
* @return self
*/
public function fillFromSetter(array $document)
{
foreach ($document as $offset => $value) {
Expand Down
14 changes: 14 additions & 0 deletions src/Document/Accessors/CamelCaseAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,25 @@

trait CamelCaseAccessor
{
/**
* Transforms the given string into CamelCase
*
* @param string $offset The string to transform
*
* @return string The string into CamelCase
*/
public function getterIze($offset)
{
return 'get' . str_replace(' ', '', ucwords(strtr($offset, '_-', ' ')));
}

/**
* Transforms the given string into CamelCase
*
* @param string $offset The string to transform
*
* @return string The string into CamelCase
*/
public function setterIze($offset)
{
return 'set' . str_replace(' ', '', ucwords(strtr($offset, '_-', ' ')));
Expand Down
38 changes: 38 additions & 0 deletions src/Document/ArrayAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,69 @@

trait ArrayAccess
{
/**
* Returns from a getter the value at the given offset
*
* @param string $offset The key
*
* @return mixed The value
*/
public function __get(string $offset)
{
return self::getFromGetter($offset);
}

/**
* Returns the value at the given offset
*
* @param string $offset The key
* @return mixed The value
*/
public function offsetGet($offset)
{
return $this->document[$offset];
}

/**
* Sets from a setter the given value at the given offset
*
* @param string $offset The key
* @param mixed $value The value
*/
public function __set(string $offset, $value)
{
self::setFromSetter($offset, $value);
}

/**
* Sets the given value at the given offset
*
* @param string $offset The key
* @param mixed $value The value
*/
public function offsetSet($offset, $value)
{
$this->document[$offset] = $value;
}

/**
* Unsets the document's value
*
* @param stirng $offset The key
*/
public function offsetUnset($offset)
{
unset($this->document[$offset]);
}

/**
* Determines whether the given key exists or not in the document
*
* @param string $offset The key
*
* @return book Whether the given key exists or not in the document
*/
public function offsetExists($offset)
{
return isset($this->document[$offset]);
Expand Down
23 changes: 23 additions & 0 deletions src/Document/ArrayIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,49 @@

trait ArrayIterator
{
/**
* Rewinds the document
*/
public function rewind()
{
reset($this->document);
}

/**
* Returns the current element
*
* @return mixed The current element
*/
public function current()
{
return current($this->document);
}

/**
* Returns the current key
*
* @return string The current key
*/
public function key()
{
return key($this->document);
}

/**
* Moves the array cursor forward and returns the next element
*
* @return mixed The next element
*/
public function next()
{
return next($this->document);
}

/**
* Determines whether or not the array is ready for an iteration
*
* @return bool Whether or not the array is ready for an iteration
*/
public function valid()
{
$key = key($this->document);
Expand Down
10 changes: 10 additions & 0 deletions src/Document/MongoAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@

trait MongoAccess
{
/**
* Serializes the document to a MongoDB readable value
*
* @return array The document as array
*/
public function bsonSerialize()
{
return $this->document;
}

/**
* Unserializes a document comming from MongoDB
*
* @param array $document The document as array
*/
public function bsonUnserialize(array $document)
{
self::fillFromSetter($document);
Expand Down
Loading

0 comments on commit 2b73988

Please sign in to comment.