Now if you like to have a more convenient way for creating objects have a look on the factory pattern factory pattern for example, but don't pollute your entities with endless boilerplate.
Now if you like to have a more convenient way for creating objects have a look on the factory pattern for example, but don't pollute your entities with endless boilerplate.
Now if you like to have a more convenient way for creating objects have a look on the factory pattern for example, but don't pollute your entities with endless boilerplate.
I think you're mixing too many ideas here and as a consequence your class ends up having too many responsibilities, which than need extensive documentation because their behaviour may not be obvious and self explanatory.
Try to keep things just as simple as possible. In your case a simple plain old PHP object would potentially do the job.
Take for example the following entity class, which is basically the core of your code, all the rest of your functions are just creational util function.
class Taxonomy
{
private $name;
private $isHierarchical;
public function __construct($name, $isHierarchical = false)
{
$this->name = $name;
$this->isHierarchical = $isHierarchical;
}
public function getName()
{
return $this->name;
}
public function isHierarchical()
{
return $this->isHierarchical;
}
}
Because the code is that simple and self explanatory I don't even need to add any comment. Generally it's preferable to have clear and readable code over extensive documentation ...
That little extra bit of logic to have unique taxonomy names within an array of taxonomies could be archived by using for example a TaxonomyCollection object which takes care of that.
E.g.
class TaxonomyCollection
{
private $taxonomies = [];
public function __construct(array $taxonomies)
{
array_walk($taxonomies, function($taxonomy) {
$this->addTaxonomy($taxonomy);
});
}
public function addTaxonomy(Taxonomy $taxonomy)
{
if ($this->getTaxonomyByName($taxonomy->getName())) {
throw new \RuntimeException(
sprintf(
'Taxonomy name "%s" not unique within the collection.',
$taxonomy->getName()
)
);
}
array_push($this->taxonomies, $taxonomy);
}
public function getTaxonomies()
{
return $this->taxonomies;
}
public function getTaxonomyByName($taxonomyName)
{
foreach($this->taxonomies as $taxonomy) {
if ($taxonomyName === $taxonomy->getName()) {
return $taxonomy;
}
}
return null;
}
}
Example usage:
new TaxonomyCollection([
new Taxonomy('foo', true),
new Taxonomy('bar'),
]);
Now if you like to have a more convenient way for creating objects have a look on the factory pattern for example, but don't pollute your entities with endless boilerplate.