コード例 #1
0
ファイル: EntityPropertyTest.php プロジェクト: zortje/mvc
 /**
  * @covers ::formatValueForDatabase
  */
 public function testFormatNullForDatabaseIPAddress()
 {
     $property = new EntityProperty(EntityProperty::IPADDRESS);
     $this->assertNull($property->formatValueForDatabase(null));
 }
コード例 #2
0
ファイル: Table.php プロジェクト: zortje/mvc
 /**
  * Find all entities for given conditions
  *
  * ```
  * [
  *     '{entity_property}' => '{property_value}'
  * ]
  * ```
  *
  * @param array $conditions
  *
  * @throws InvalidEntityPropertyException If entity does not have that property
  *
  * @return Entity[] Entities
  */
 public function findBy(array $conditions) : array
 {
     /**
      * Check if entity have the properties in conditions
      *
      * @var Entity $entity
      */
     $reflector = new \ReflectionClass($this->entityClass);
     $entity = $reflector->newInstanceWithoutConstructor();
     foreach (array_keys($conditions) as $key) {
         if (!isset($entity::getColumns()[$key])) {
             throw new InvalidEntityPropertyException([$this->entityClass, $key]);
         }
     }
     /**
      * Validate values in conditions
      */
     foreach ($conditions as $key => $value) {
         $entityProperty = new EntityProperty($entity::getColumns()[$key]);
         $entityProperty->validateValue($value);
     }
     /**
      * Execute with key-value condition
      */
     $parameters = [];
     foreach ($conditions as $key => $value) {
         $parameters[":{$key}"] = $value;
     }
     $stmt = $this->pdo->prepare($this->sqlCommand->selectFromWhere(array_keys($conditions)));
     $stmt->execute($parameters);
     return $this->createEntitiesFromStatement($stmt);
 }
コード例 #3
0
ファイル: Entity.php プロジェクト: zortje/mvc
 /**
  * Return columns in structure for saving
  *
  * @param array $columns Columns to include
  *
  * @return array
  */
 protected function toArrayFromColumns(array $columns) : array
 {
     $array = [];
     foreach ($columns as $column => $type) {
         $property = new EntityProperty($type);
         $value = $this->get($column);
         $value = $property->formatValueForDatabase($value);
         $array[":{$column}"] = $value;
     }
     return $array;
 }