Skip to content

Commit

Permalink
Added Renaming and Renamed events
Browse files Browse the repository at this point in the history
Closes #78
  • Loading branch information
stevebauman committed Feb 14, 2020
1 parent 73b9219 commit 95c3bc7
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Models/Events/Renamed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace LdapRecord\Models\Events;

class Renamed extends Event
{
//
}
57 changes: 57 additions & 0 deletions src/Models/Events/Renaming.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace LdapRecord\Models\Events;

use LdapRecord\Models\Model;

class Renaming extends Event
{
/**
* The models RDN.
*
* @var string
*/
protected $rdn;

/**
* The models new parent DN.
*
* @var string
*/
protected $newParentDn;

/**
* Constructor.
*
* @param Model $model
* @param string $rdn
* @param string $newParentDn
*/
public function __construct(Model $model, $rdn, $newParentDn)
{
parent::__construct($model);

$this->rdn = $rdn;
$this->newParentDn = $newParentDn;
}

/**
* Get the models RDN.
*
* @return string
*/
public function getRdn()
{
return $this->rdn;
}

/**
* Get the models parent DN.
*
* @return string
*/
public function getNewParentDn()
{
return $this->newParentDn;
}
}
6 changes: 6 additions & 0 deletions src/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use ArrayAccess;
use JsonSerializable;
use LdapRecord\Container;
use LdapRecord\Models\Events\Renamed;
use LdapRecord\Models\Events\Renaming;
use LdapRecord\Utilities;
use LdapRecord\Connection;
use InvalidArgumentException;
Expand Down Expand Up @@ -1200,12 +1202,16 @@ public function rename($rdn, $newParentDn = null, $deleteOldRdn = true)
$newParentDn = $this->getParentDn($this->dn);
}

$this->fireModelEvent(new Renaming($this, $rdn, $newParentDn));

if ($this->newQuery()->rename($this->dn, $rdn, $newParentDn, $deleteOldRdn)) {
// If the model was successfully renamed, we will set
// its new DN so any further updates to the model
// can be performed without any issues.
$this->dn = implode(',', [$rdn, $newParentDn]);

$this->fireModelEvent(new Renamed($this));

return true;
}

Expand Down

0 comments on commit 95c3bc7

Please sign in to comment.