/** * Set entity property * * @param string $key Entity property name * @param object|int|double|string|array|boolean|null $value Entity property value * * @throws InvalidEntityPropertyException If entity does not have that property * @throws InvalidValueTypeForEntityPropertyException If value is of the wrong type */ public function set(string $key, $value) { if (isset(self::getColumns()[$key]) === false) { throw new InvalidEntityPropertyException([get_class($this), $key]); } $entityProperty = new EntityProperty(self::getColumns()[$key]); if ($entityProperty->validateValue($value)) { if (!isset($this->properties[$key]) || $this->properties[$key] !== $value) { /** * Set internal property */ $this->properties[$key] = $value; /** * Set altered column */ $this->alteredColumns[$key] = true; } } }
/** * 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); }
/** * @covers ::validateValue */ public function testValidateValueEnumInvalidValue() { $this->expectException(InvalidENUMValueForEntityPropertyException::class); $this->expectExceptionMessage('"bar" is not a valid ENUM value'); $property = new EntityProperty(['type' => EntityProperty::ENUM, 'values' => ['foo']]); $property->validateValue('bar'); }