Skip to content

Commit

Permalink
Add with and without methods
Browse files Browse the repository at this point in the history
  • Loading branch information
abellion committed May 25, 2017
1 parent f7fd408 commit 9c6b677
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,47 @@ public function has(string $offset)
return isset($this->document[$offset]);
}

/**
* Returns a new document with the specified fields
*
* @param array $fields The keys to keep
*
* @return Xenus\Document A new document conatining only the specified fields
*/
public function with(array $fields)
{
$document = [];

foreach ($fields as $field) {
if (array_key_exists($field, $this->document)) {
$document[$field] = $this->document[$field];
}
}

return new self($document);
}

/**
* Returns a new document without the specified fields
*
* @param array $fields The keys to drop
*
* @return Xenus\Document A new document without the specified fields
*/
public function without(array $fields)
{
$document = [];

foreach ($this->document as $key => $value) {
if (!in_array($key, $fields)) {
$document[$key] = $this->document[$key];
}
}

return new self($document);
}


/**
* Fills the document whith the given values
*
Expand Down

0 comments on commit 9c6b677

Please sign in to comment.