Skip to content

Commit 955f59d

Browse files
committed
Allow create to be statically called
The docs display models being able to be created by a static create method. This should have been made available at the get go.
1 parent 76a830d commit 955f59d

File tree

1 file changed

+57
-31
lines changed

1 file changed

+57
-31
lines changed

src/Models/Model.php

Lines changed: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -880,9 +880,11 @@ public function inside($dn)
880880
*/
881881
public function save(array $attributes = [])
882882
{
883+
$this->fill($attributes);
884+
883885
$this->fireModelEvent(new Events\Saving($this));
884886

885-
$saved = $this->exists ? $this->update($attributes) : $this->create($attributes);
887+
$saved = $this->exists ? $this->performUpdate() : $this->performInsert();
886888

887889
if ($saved) {
888890
$this->fireModelEvent(new Events\Saved($this));
@@ -892,18 +894,14 @@ public function save(array $attributes = [])
892894
}
893895

894896
/**
895-
* Create the model in the directory.
896-
*
897-
* @param array $attributes The attributes for the new entry.
897+
* Inserts the model into the directory.
898898
*
899899
* @throws \Exception
900900
*
901901
* @return bool
902902
*/
903-
public function create(array $attributes = [])
903+
protected function performInsert()
904904
{
905-
$this->fill($attributes);
906-
907905
// Here we will populate the models object class if it
908906
// does not already have one. An LDAP record
909907
// cannot be created without it.
@@ -916,16 +914,16 @@ public function create(array $attributes = [])
916914
// If the model doesn't currently have a distinguished
917915
// name set, we'll create one automatically using
918916
// the current query builders base DN.
919-
if (empty($this->dn)) {
920-
$this->dn = $this->getCreatableDn();
917+
if (empty($this->getDn())) {
918+
$this->setDn($this->getCreatableDn());
921919
}
922920

923921
$this->fireModelEvent(new Events\Creating($this));
924922

925923
// Before performing the insert, we will filter the attributes of the model
926924
// to ensure no empty values are sent. If empty values are sent, then
927925
// the LDAP server will return an error message indicating such.
928-
if ($query->insert($this->dn, array_filter($this->getAttributes()))) {
926+
if ($query->insert($this->getDn(), array_filter($this->getAttributes()))) {
929927
$this->fireModelEvent(new Events\Created($this));
930928

931929
$this->exists = true;
@@ -936,6 +934,54 @@ public function create(array $attributes = [])
936934
return false;
937935
}
938936

937+
/**
938+
* Updates the model in the directory.
939+
*
940+
* @throws \Exception
941+
*
942+
* @return bool
943+
*/
944+
protected function performUpdate()
945+
{
946+
$modifications = $this->getModifications();
947+
948+
if (count($modifications) > 0) {
949+
$this->fireModelEvent(new Events\Updating($this));
950+
951+
if ($this->newQuery()->update($this->dn, $modifications)) {
952+
$this->fireModelEvent(new Events\Updated($this));
953+
954+
// Re-set the models modifications.
955+
$this->modifications = [];
956+
957+
// Re-sync the models attributes.
958+
return $this->synchronize();
959+
}
960+
961+
return false;
962+
}
963+
964+
return true;
965+
}
966+
967+
/**
968+
* Create the model in the directory.
969+
*
970+
* @param array $attributes The attributes for the new entry.
971+
*
972+
* @throws \Exception
973+
*
974+
* @return Model
975+
*/
976+
public static function create(array $attributes = [])
977+
{
978+
$instance = new static($attributes);
979+
980+
$instance->save();
981+
982+
return $instance;
983+
}
984+
939985
/**
940986
* Create an attribute on the model.
941987
*
@@ -971,27 +1017,7 @@ public function update(array $attributes = [])
9711017
{
9721018
$this->validateExistence();
9731019

974-
$this->fill($attributes);
975-
976-
$modifications = $this->getModifications();
977-
978-
if (count($modifications) > 0) {
979-
$this->fireModelEvent(new Events\Updating($this));
980-
981-
if ($this->newQuery()->update($this->dn, $modifications)) {
982-
$this->fireModelEvent(new Events\Updated($this));
983-
984-
// Re-set the models modifications.
985-
$this->modifications = [];
986-
987-
// Re-sync the models attributes.
988-
return $this->synchronize();
989-
}
990-
991-
return false;
992-
}
993-
994-
return true;
1020+
return $this->save($attributes);
9951021
}
9961022

9971023
/**

0 commit comments

Comments
 (0)