tainacan/tests/factories/class-tainacan-entity-facto...

124 lines
3.4 KiB
PHP
Raw Normal View History

<?php
namespace Tainacan\Tests\Factories;
2021-03-11 18:34:10 +00:00
use InvalidArgumentException;
class Entity_Factory {
2017-12-11 14:36:24 +00:00
/**
*
* @var \Tainacan\Entities\Entity
*/
2021-04-06 04:21:22 +00:00
private $entity;
2021-03-11 18:34:10 +00:00
2017-12-11 14:36:24 +00:00
/**
*
* @var \Tainacan\Repositories\Repository
*/
protected $repository;
protected $entity_type;
protected $repository_type;
/**
* Receive a type of the collection as string, an associative array,
* a boolean that say if the values need to be validated and inserted in database, and then
* create the entity type ordered and return it.
*
* @param $type
* @param array $args
* @param bool $is_validated_and_in_db
*
* @param bool $publish
*
2021-03-11 18:34:10 +00:00
* @throws InvalidArgumentException
*
* @return mixed
*/
2017-12-12 13:40:01 +00:00
public function create_entity($type, $args = [], $is_validated_and_in_db = false, $publish = false){
2017-12-05 12:28:38 +00:00
ini_set('display_errors', 1);
2021-03-11 18:34:10 +00:00
try {
$type = trim($type);
if(empty($type)){
2021-03-11 18:34:10 +00:00
throw new InvalidArgumentException('The type can\'t be empty');
} elseif(!strrchr($type, '_')){
$type = ucfirst(strtolower($type));
} else {
$type = ucwords(strtolower($type), '_');
}
$this->entity_type = "\Tainacan\Entities\\$type";
$type_size = strlen($type);
2017-12-04 18:20:49 +00:00
if($type[$type_size-1] == 'y'){
2017-12-04 18:20:49 +00:00
$type[$type_size-1] = 'i';
$this->repository_type = "\Tainacan\Repositories\\$type".'es';
} elseif($type == 'Metadatum'){
$this->repository_type = "\Tainacan\Repositories\Metadata";
} elseif($type == 'Metadata_Section'){
$this->repository_type = "\Tainacan\Repositories\Metadata_Sections";
2022-03-30 13:35:12 +00:00
}
else {
$this->repository_type = "\Tainacan\Repositories\\$type".'s';
}
$this->entity = new $this->entity_type();
$repo = $this->repository_type;
$this->repository = $repo::get_instance();
2017-12-12 13:40:01 +00:00
if($publish) {
2017-12-12 13:40:01 +00:00
$this->entity->set_status('publish');
}
if (!empty($args) && $is_validated_and_in_db) {
foreach ($args as $attribute => $content) {
$set_ = 'set_' . trim($attribute);
$this->entity->$set_( $content );
}
if ($this->entity->validate()) {
$this->entity = $this->repository->insert($this->entity);
} else {
2017-12-12 14:16:17 +00:00
throw new \ErrorException('The entity wasn\'t validated.' . print_r( $this->entity->get_errors(), true));
}
} elseif (!empty($args) && !$is_validated_and_in_db){
foreach ($args as $attribute => $content) {
$set_ = 'set_' . trim($attribute);
$this->entity->$set_( $content );
}
} elseif (empty($args) && !$is_validated_and_in_db) {
try {
$this->entity->set_name( "$type " . rand( 0, 10000 ) . " for test" );
$this->entity->set_description( 'It is only for test' );
} catch (\Exception $exception){
$this->entity->set_title( "$type " . rand( 0, 10000 ) . " for test" );
$this->entity->set_description( 'It is only for test' );
}
} elseif (empty($args) && $is_validated_and_in_db) {
try {
$this->entity->set_name( "$type " . rand( 0, 10000 ) . " for test" );
$this->entity->set_description( 'It is only for test' );
} catch (\Exception $exception){
$this->entity->set_title( "$type " . rand( 0, 10000 ) . " for test" );
$this->entity->set_description( 'It is only for test' );
}
2017-12-12 14:16:17 +00:00
$this->entity->validate();
$this->entity = $this->repository->insert( $this->entity );
} else {
2021-03-11 18:34:10 +00:00
throw new InvalidArgumentException('One or more arguments are invalid.');
}
} catch (\Exception $exception){
2017-12-05 12:28:38 +00:00
echo "\n" . $exception->getMessage() . "\n";
}
2017-12-12 13:40:01 +00:00
return $this->entity;
}
}