Exemple #1
0
 /**
  * Solver constructor.
  * @param Relation $relation
  * @param EntityStructure $structure
  * @param Query $query
  */
 public function __construct(&$relation, &$structure, &$query)
 {
     $this->relation =& $relation;
     $this->structure =& $structure;
     $this->query =& $query;
     // Determinate target structure
     $this->targetStructure = EntityManager::getInstance()->getEntityStructure($relation->targetEntity);
 }
Exemple #2
0
 /**
  * Fetch target entity or entities.
  *
  * @param Entity $entity
  * @return mixed
  */
 public function solveFetch(Entity &$entity)
 {
     /** @var JoinTable $joinTable */
     $joinTable = $this->relation->join;
     // Get target entity structure
     $targetStructure = EntityManager::getInstance()->getEntityStructure($joinTable->targetEntityName);
     $query = "SELECT * FROM {$targetStructure->tableName} as target, {$joinTable->name} as couple WHERE target.{$joinTable->targetColumn->entityColumn} = couple.{$joinTable->targetColumn->name} AND couple.{$joinTable->column->name} = ?;";
     $bind = array($entity->{$joinTable->column->entityColumn});
     $results = EntityManager::query($this->relation->targetEntity)->custom($query, $bind);
     return $results;
 }
Exemple #3
0
 /**
  * @covers \SweetORM\EntityManager
  * @covers \SweetORM\EntityManager::getInstance
  * @covers \SweetORM\EntityManager::isRegistered
  * @covers \SweetORM\EntityManager::registerEntity
  * @covers \SweetORM\EntityManager::getEntityStructure
  * @covers \SweetORM\Structure\EntityStructure
  * @covers \SweetORM\Structure\Indexer\TableIndexer
  * @covers \SweetORM\Structure\Indexer\EntityIndexer
  * @covers \SweetORM\Structure\Indexer\ColumnIndexer
  * @covers \SweetORM\Structure\Indexer\RelationIndexer
  */
 public function testRegisterEntity()
 {
     $manager = EntityManager::getInstance();
     $manager->clearRegisteredEntities();
     $registered = $manager->isRegistered(Post::class);
     $this->assertFalse($registered);
     $manager->registerEntity(Category::class);
     $registered = $manager->isRegistered(Category::class);
     $this->assertTrue($registered);
     $structure = $manager->getEntityStructure(Category::class);
     $this->assertInstanceOf("\\SweetORM\\Structure\\EntityStructure", $structure);
     $this->assertEquals(Category::class, $structure->name);
     $this->assertEquals("category", $structure->tableName);
 }
Exemple #4
0
 /**
  * Get validator and filler class for provided data.
  *
  * @param mixed $data
  * @return Structure\Validator\Validator|false
  */
 public static function validator($data)
 {
     return EntityManager::getInstance()->validator(static::class, $data);
 }
Exemple #5
0
 /**
  * @param EntityStructure $structure
  * @param \ReflectionProperty $property
  * @param ManyToMany|Relation $relation
  *
  * @throws RelationException Class not correct, no target property found or not extending Entity.
  * @throws \ReflectionException Class not found
  */
 private function manyToMany(&$structure, $property, $relation)
 {
     $from = $structure->name;
     $to = $relation->targetEntity;
     $reflection = null;
     try {
         $reflection = new \ReflectionClass($to);
     } catch (\Exception $e) {
         // @codeCoverageIgnore
         // Ignore, we will throw error on the next if. // @codeCoverageIgnore
     }
     if ($reflection === null || !$reflection->isSubclassOf(Entity::class)) {
         throw new RelationException("The target entity of your relation on the entity '" . $from . "' and property '" . $property->getName() . "' has an unknown target Entity!");
         // @codeCoverageIgnore
     }
     /** @var JoinTable $join */
     $join = $this->getJoin($property, JoinTable::class);
     $join->sourceEntityName = $from;
     $join->targetEntityName = $to;
     $relation->join = $join;
     // Register the join table
     EntityManager::getInstance()->registerJoinTable($join);
     // Add declaration to the structure
     $structure->relationProperties[] = $property->getName();
     $structure->foreignColumnNames[] = $join->column;
     $structure->relations[$property->getName()] = $relation;
 }
Exemple #6
0
 /**
  * @covers \SweetORM\EntityManager
  * @covers \SweetORM\EntityManager::getInstance
  * @covers \SweetORM\EntityManager::getEntityStructure
  * @covers \SweetORM\EntityManager::validator
  * @covers \SweetORM\Entity::validator
  * @covers \SweetORM\Structure\EntityStructure
  * @covers \SweetORM\Structure\ValidationManager
  * @covers \SweetORM\Structure\ValidationManager::validator
  * @covers \SweetORM\Structure\Validator\Validator
  * @covers \SweetORM\Structure\Validator\ArrayValidator
  * @covers \SweetORM\Structure\Annotation\Constraint
  * @covers \SweetORM\Structure\Indexer\ColumnIndexer
  * @covers \SweetORM\Database\Query
  */
 public function testConstraints()
 {
     $manager = EntityManager::getInstance();
     $manager->clearRegisteredEntities();
     $array = array('startsWith' => 'www.google.com');
     $result = ConstraintTest::validator($array)->test();
     $this->assertFalse($result->isSuccess());
     $array = array('startsWith' => 'www.google.com/testi');
     $result = ConstraintTest::validator($array)->test();
     $this->assertTrue($result->isSuccess());
     $array = array('startsWith' => 'www.google.com/testi');
     $result = ConstraintTest::validator($array)->test();
     $this->assertTrue($result->isSuccess());
     $array = array('startsWith' => 'www.google.com/testin');
     $result = ConstraintTest::validator($array)->test();
     $this->assertTrue($result->isSuccess());
     $array = array('startsWith' => 'ww.google.com/testin');
     $result = ConstraintTest::validator($array)->test();
     $this->assertFalse($result->isSuccess());
     $result = ConstraintTest::validator(array('question' => 'noo'))->test();
     $this->assertFalse($result->isSuccess());
     $result = ConstraintTest::validator(array('question' => 'YES'))->test();
     $this->assertFalse($result->isSuccess());
     $result = ConstraintTest::validator(array('question' => 'no'))->test();
     $this->assertTrue($result->isSuccess());
     $result = ConstraintTest::validator(array('question' => 'yes'))->test();
     $this->assertTrue($result->isSuccess());
     $result = ConstraintTest::validator(array('between' => 44.49))->test();
     $this->assertFalse($result->isSuccess());
     $result = ConstraintTest::validator(array('between' => 44.5))->test();
     $this->assertTrue($result->isSuccess());
     $result = ConstraintTest::validator(array('between' => 55.5))->test();
     $this->assertTrue($result->isSuccess());
     $result = ConstraintTest::validator(array('between' => 55.51))->test();
     $this->assertFalse($result->isSuccess());
     $result = ConstraintTest::validator(array('endsWith' => 'www.test.com'))->test();
     $this->assertTrue($result->isSuccess());
     $result = ConstraintTest::validator(array('endsWith' => 'www.whereisgoogle.com'))->test();
     // == 21 chars
     $this->assertFalse($result->isSuccess());
     $result = ConstraintTest::validator(array('endsWith' => 'www.hereisgoogle.com'))->test();
     // == 20 chars
     $this->assertTrue($result->isSuccess());
     $result = ConstraintTest::validator(array('endsWith' => 'www.test.nl'))->test();
     // != .com
     $this->assertFalse($result->isSuccess());
     $result = ConstraintTest::validator(array('youtube' => 'www.test.nl'))->test();
     $this->assertFalse($result->isSuccess());
     $result = ConstraintTest::validator(array('youtube' => 'https://youtu.be'))->test();
     $this->assertFalse($result->isSuccess());
     $result = ConstraintTest::validator(array('youtube' => 'https://youtu.be/dQw4w9WgXcQ'))->test();
     $this->assertTrue($result->isSuccess());
     $result = ConstraintTest::validator(array('url' => 'https://youtu.be/dQw4w9WgXcQ'))->test();
     $this->assertTrue($result->isSuccess());
     $result = ConstraintTest::validator(array('url' => 'dQw4w9WgXcQ'))->test();
     $this->assertFalse($result->isSuccess());
 }
Exemple #7
0
 /**
  * Set a new relationship value.
  *
  * @param string $virtualProperty
  * @param Entity|null $relationEntity
  *
  * @throws RelationException
  * @throws \Exception
  */
 public function set($virtualProperty, $relationEntity)
 {
     // Check for existing of the relation property
     if (!in_array($virtualProperty, $this->structure->relationProperties) || !isset($this->structure->relations[$virtualProperty])) {
         throw new RelationException("Relation not defined!");
         // @codeCoverageIgnore
     }
     // Make cache array if needed, for lazy loading
     if (!isset(self::$lazy[get_class($this->entity)][$virtualProperty])) {
         self::$lazy[get_class($this->entity)][$virtualProperty] = array();
     }
     /** @var Relation $relation */
     $relation = $this->structure->relations[$virtualProperty];
     if (!$relation instanceof Relation) {
         throw new RelationException("Relation indexing failed, something is really wrong, please report! Fetch proprty no instance of relation!");
         // @codeCoverageIgnore
     }
     // Can only set OneToOne and ManyToOne
     if (!$relation instanceof OneToOne && !$relation instanceof ManyToOne) {
         throw new RelationException("Only relations OneToOne and ManyToOne could be set!");
         // @codeCoverageIgnore
     }
     // If NULL then set null into the entity id column (fk)
     if ($relationEntity === null) {
         // Set null
         $this->entity->{$relation->join->column} = null;
         return;
     }
     // Get target structure
     $targetStructure = EntityManager::getInstance()->getEntityStructure($relationEntity);
     // Check if relationEntity is saved, if not throw exception!
     if (!$relationEntity->_saved) {
         throw new RelationException("Save the relationship entity first!");
     }
     // Set the id in the from entity
     $id = $relationEntity->{$targetStructure->primaryColumn->propertyName};
     $this->entity->{$relation->join->column} = $id;
     // Set the cache
     self::$lazy[get_class($this->entity)][$virtualProperty][$id] = $relationEntity;
 }