/** * @covers ::formatValueForDatabase */ public function testFormatNullForDatabaseIPAddress() { $property = new EntityProperty(EntityProperty::IPADDRESS); $this->assertNull($property->formatValueForDatabase(null)); }
/** * 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); }
/** * 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; }