diff --git a/src/Facade/FactoryMuffin.php b/src/Facade/FactoryMuffin.php index 63b7e7a..91e49a0 100644 --- a/src/Facade/FactoryMuffin.php +++ b/src/Facade/FactoryMuffin.php @@ -10,6 +10,18 @@ * * @see League\FactoryMuffin\FactoryMuffin * + * @method static void setSaveMethod(string $method) Set the method we use when saving objects. + * @method static void setDeleteMethod(string $method) Set the method we use when deleting objects. + * @method static object create(string $model, array $attr) Creates and saves in db an instance of the model. + * @method static mixed save(object $object) Save our object to the db, and keep track of it. + * @method static object[] saved() Return an array of saved objects. + * @method static void deleteSaved() Call the delete method on any saved objects. + * @method static object instance(string $model, array $attr, bool $save) Return an instance of the model. + * @method static array attributesFor(string $model, array $attr, bool $save) Returns the mock attributes for the model. + * @method static void define(string $model, array $definition) Define a new model factory. + * @method static string|object generateAttr(string $kind, string $model, bool $save) Generate the attributes. + * @method static void loadFactories(string|string[] $paths) Load the specified factories. + * * @package League\FactoryMuffin\Facades * @author Zizaco * @author Scott Robertson diff --git a/src/FactoryMuffin.php b/src/FactoryMuffin.php index 4e2f4e2..fad81ab 100644 --- a/src/FactoryMuffin.php +++ b/src/FactoryMuffin.php @@ -38,6 +38,13 @@ class FactoryMuffin */ private $saved = array(); + /** + * This is the method used when saving objects. + * + * @type string + */ + private $saveMethod = 'save'; + /** * This is the method used when deleting objects. * @@ -46,14 +53,33 @@ class FactoryMuffin private $deleteMethod = 'delete'; /** - * This is the method used when saving objects. + * Set the method we use when saving objects. * - * @type string + * @param string $method + * + * @return void */ - private $saveMethod = 'save'; + public function setSaveMethod($method) + { + $this->saveMethod = $method; + } /** - * Creates and saves in db an instance of Model with mock attributes. + * Set the method we use when deleting objects. + * + * @param string $method + * + * @return void + */ + public function setDeleteMethod($method) + { + $this->deleteMethod = $method; + } + + /** + * Creates and saves in db an instance of the model. + * + * This object will be generated with mock attributes. * * @param string $model Model class name. * @param array $attr Model attributes. @@ -78,7 +104,7 @@ public function create($model, $attr = array()) } /** - * Save our object to the DB, and keep track of it. + * Save our object to the db, and keep track of it. * * @param object $object * @@ -101,11 +127,55 @@ public function save($object) } /** - * Return an instance of the model, which is not saved in the database. + * Return an array of saved objects. + * + * @return object[] + */ + public function saved() + { + return $this->saved; + } + + /** + * Call the delete method on any saved objects. + * + * @throws \League\FactoryMuffin\Exception\DeletingFailed + * @throws \League\FactoryMuffin\Exception\DeleteMethodNotFound + * + * @return void + */ + public function deleteSaved() + { + $exceptions = array(); + $method = $this->deleteMethod; + foreach ($this->saved() as $saved) { + try { + if (!method_exists($saved, $method)) { + throw new DeleteMethodNotFound($saved, $method); + } + + $saved->$method(); + } catch (Exception $e) { + $exceptions[] = $e; + } + } + + $this->saved = array(); + + if ($exceptions) { + throw new DeletingFailed($exceptions); + } + } + + /** + * Return an instance of the model. + * + * This does not save it in the database. Use the create method to create + * and save to the db, or pass the object from this method into the save method. * * @param string $model Model class name. - * @param bool $save Are we saving an object, or just creating an instance? * @param array $attr Model attributes. + * @param bool $save Are we saving an object, or just creating an instance? * * @return object */ @@ -125,7 +195,7 @@ public function instance($model, $attr = array(), $save = false) } /** - * Returns an array of mock attributes for the specified model. + * Returns the mock attributes for the model. * * @param string $model Model class name. * @param array $attr Model attributes. @@ -147,19 +217,6 @@ public function attributesFor($model, $attr = array(), $save = false) return $attr; } - /** - * Define a new model factory. - * - * @param string $model Model class name. - * @param array $definition Array with definition of attributes. - * - * @return void - */ - public function define($model, array $definition = array()) - { - $this->factories[$model] = $definition; - } - /** * Get factory attributes. * @@ -167,7 +224,7 @@ public function define($model, array $definition = array()) * * @throws \League\FactoryMuffin\Exception\NoDefinedFactory * - * @return mixed + * @return array */ private function getFactoryAttrs($model) { @@ -179,7 +236,22 @@ private function getFactoryAttrs($model) } /** - * Generate the attributes and return a string, or an instance of the model. + * Define a new model factory. + * + * @param string $model Model class name. + * @param array $definition Array with definition of attributes. + * + * @return void + */ + public function define($model, array $definition = array()) + { + $this->factories[$model] = $definition; + } + + /** + * Generate the attributes. + * + * This method will return a string, or an instance of the model. * * @param string $kind The kind of attribute that will be generate. * @param string $model The name of the model class. @@ -197,6 +269,10 @@ public function generateAttr($kind, $model = null, $save = false) /** * Load the specified factories. * + * This method expects either a single path to a directory containing php + * files, or an array of directory paths, and will include_once every file. + * These files should contain factory definitions for your models. + * * @param string|string[] $paths * * @throws \League\FactoryMuffin\Exception\DirectoryNotFound @@ -214,71 +290,6 @@ public function loadFactories($paths) } } - /** - * Return an array of saved objects. - * - * @return object[] - */ - public function saved() - { - return $this->saved; - } - - /** - * Call the delete method on any saved objects. - * - * @throws \League\FactoryMuffin\Exception\DeletingFailed - * @throws \League\FactoryMuffin\Exception\DeleteMethodNotFound - * - * @return void - */ - public function deleteSaved() - { - $exceptions = array(); - $method = $this->deleteMethod; - foreach ($this->saved() as $saved) { - try { - if (!method_exists($saved, $method)) { - throw new DeleteMethodNotFound($saved, $method); - } - - $saved->$method(); - } catch (Exception $e) { - $exceptions[] = $e; - } - } - - $this->saved = array(); - - if ($exceptions) { - throw new DeletingFailed($exceptions); - } - } - - /** - * Set the method we use when deleting objects. - * - * @param string $method - * - * @return void - */ - public function setDeleteMethod($method) - { - $this->deleteMethod = $method; - } - - /** - * Set the method we use when saving objects. - * - * @param string $method - * - * @return void - */ - public function setSaveMethod($method) - { - $this->saveMethod = $method; - } - /** * Load all the files in a directory. *