Skip to content

Instantly share code, notes, and snippets.

@kamaulynder
Last active August 29, 2015 14:03
Show Gist options
  • Save kamaulynder/fc8f6ca5cc200be38fc3 to your computer and use it in GitHub Desktop.
Save kamaulynder/fc8f6ca5cc200be38fc3 to your computer and use it in GitHub Desktop.
namespace Ushahidi\Usecase\Tag;
use Ushahidi\Entity\Tag;
use Ushahidi\Tool\Validator;
use Ushahidi\Exception\ValidatorException;
class Update
{
private $repo;
private $valid;
private $updated = [];
public function __construct(UpdateTagRepository $repo, Validator $valid)
{
$this->repo = $repo;
$this->valid = $valid;
}
public function interact(Tag $tag, TagData $input)
{
if($input->role)
{
$role = $input->role;
$input->role = json_encode($role);
}
// We only want to work with values that have been changed
$update = $input->getDifferent($tag->asArray());
if (!$this->valid->check($update))
throw new ValidatorException("Failed to validate tag", $this->valid->errors());
// Determine what changes to make in the tag
$this->updated = $update->asArray();
$this->repo->updateTag($tag->id, $this->updated);
// Reflect the changes in the tag
$tag->setData($this->updated);
return $tag;
}
public function getUpdated()
{
return $this->updated;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment