Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aggregate multiple columns behavior & parameter list support #1702

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
requested changes
  • Loading branch information
mringler committed Mar 4, 2021
commit c1a0d89902631f57da36675449a3a78875089261
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Propel\Generator\Behavior\AggregateColumn\AggregateColumnRelationBehavior;
use Propel\Generator\Builder\Om\ObjectBuilder;
use Propel\Generator\Model\Behavior;
use Propel\Generator\Model\Table;
use Propel\Generator\Model\ForeignKey;

/**
* Keeps an aggregate column updated with related table
Expand Down Expand Up @@ -56,15 +58,15 @@ class AggregateMultipleColumnsBehavior extends Behavior
*
* @return bool
*/
public function allowMultiple()
public function allowMultiple(): bool
{
return true;
}

/**
* @return string
*/
public function getAggregationName()
public function getAggregationName(): string
{
if ($this->aggregationName === null) {
$this->aggregationName = $this->buildAggregationName();
Expand All @@ -76,26 +78,26 @@ public function getAggregationName()
/**
* @return string
*/
private function buildAggregationName()
private function buildAggregationName(): string
{
$foreignTableName = $this->getForeignTable()->getPhpName();
$baseAggregationName = 'AggregatedColumnsFrom' . $foreignTableName;
$tableName = $this->getTable()->getPhpName();
if (!array_key_exists($tableName, self::$insertedAggregationNames)) {
self::$insertedAggregationNames[$tableName] = [];
if (!array_key_exists($tableName, static::$insertedAggregationNames)) {
static::$insertedAggregationNames[$tableName] = [];
}

$existingNames = &self::$insertedAggregationNames[$tableName];
$existingNames = &static::$insertedAggregationNames[$tableName];
if (!in_array($baseAggregationName, $existingNames)) {
$existingNames[] = $baseAggregationName;

return $baseAggregationName;
}

$ix = 1;
$duplicateAvoidanceSuffix = 1;
do {
$aggregationName = $baseAggregationName . $ix;
$ix++;
$aggregationName = $baseAggregationName . $duplicateAvoidanceSuffix;
$duplicateAvoidanceSuffix++;
} while (in_array($aggregationName, $existingNames));

$existingNames[] = $aggregationName;
Expand All @@ -108,7 +110,7 @@ private function buildAggregationName()
*
* @return void
*/
public function modifyTable()
public function modifyTable(): void
{
$this->validateColumnParameter();
$this->addMissingColumnsToTable();
Expand All @@ -118,18 +120,18 @@ public function modifyTable()
/**
* @return void
*/
private function validateColumnParameter()
private function validateColumnParameter(): void
{
$columnParameters = $this->getParameter(self::PARAMETER_KEY_COLUMNS);
$columnParameters = $this->getParameter(static::PARAMETER_KEY_COLUMNS);
if (empty($columnParameters)) {
$this->throwInvalidArgumentExceptionWithLocation('At least one column is required');
}
foreach ($columnParameters as $columnDefinition) {
if (empty($columnDefinition[self::PARAMETER_KEY_COLUMN_NAME])) {
if (empty($columnDefinition[static::PARAMETER_KEY_COLUMN_NAME])) {
$this->throwInvalidArgumentExceptionWithLocation('Parameter \'name\' is missing on a column');
}
if (empty($columnDefinition[self::PARAMETER_KEY_COLUMN_EXPRESSION])) {
$colName = $columnDefinition[self::PARAMETER_KEY_COLUMN_NAME];
if (empty($columnDefinition[static::PARAMETER_KEY_COLUMN_EXPRESSION])) {
$colName = $columnDefinition[static::PARAMETER_KEY_COLUMN_NAME];
$this->throwInvalidArgumentExceptionWithLocation('Parameter \'expression\' is missing on column ' . $colName);
}
}
Expand All @@ -140,12 +142,12 @@ private function validateColumnParameter()
*
* @return void
*/
private function addMissingColumnsToTable()
private function addMissingColumnsToTable(): void
{
$table = $this->getTable();
$columnParameters = $this->getParameter(self::PARAMETER_KEY_COLUMNS);
$columnParameters = $this->getParameter(static::PARAMETER_KEY_COLUMNS);
foreach ($columnParameters as $columnDefinition) {
$columnName = $columnDefinition[self::PARAMETER_KEY_COLUMN_NAME];
$columnName = $columnDefinition[static::PARAMETER_KEY_COLUMN_NAME];
if ($table->hasColumn($columnName)) {
continue;
}
Expand All @@ -159,9 +161,9 @@ private function addMissingColumnsToTable()
*
* @return void
*/
private function addAutoupdateBehaviorToForeignTable()
private function addAutoupdateBehaviorToForeignTable(): void
{
if (!$this->getParameter(self::PARAMETER_KEY_FOREIGN_TABLE)) {
if (!$this->getParameter(static::PARAMETER_KEY_FOREIGN_TABLE)) {
$this->throwInvalidArgumentExceptionWithLocation('You must define a \'foreign_table\' parameter');
}
$foreignTable = $this->getForeignTable();
Expand All @@ -183,7 +185,7 @@ private function addAutoupdateBehaviorToForeignTable()
*
* @return string
*/
public function objectMethods(ObjectBuilder $builder)
public function objectMethods(ObjectBuilder $builder): string
{
$script = '';
$script .= $this->addObjectCompute($builder);
Expand All @@ -199,15 +201,15 @@ public function objectMethods(ObjectBuilder $builder)
*
* @return string
*/
protected function addObjectCompute(ObjectBuilder $builder)
protected function addObjectCompute(ObjectBuilder $builder): string
{
if ($this->getForeignKey()->isPolymorphic()) {
throw new InvalidArgumentException('AggregateColumnBehavior does not work with polymorphic relations.');
}

$conditions = [];
if ($this->getParameter(self::PARAMETER_KEY_CONDITION)) {
$conditions[] = $this->getParameter(self::PARAMETER_KEY_CONDITION);
if ($this->getParameter(static::PARAMETER_KEY_CONDITION)) {
$conditions[] = $this->getParameter(static::PARAMETER_KEY_CONDITION);
}

$bindings = [];
Expand Down Expand Up @@ -236,14 +238,14 @@ protected function addObjectCompute(ObjectBuilder $builder)
/**
* @return string
*/
private function buildSelectionStatement()
private function buildSelectionStatement(): string
{
$columnDefinitions = $this->getParameter(self::PARAMETER_KEY_COLUMNS);
$columnDefinitions = $this->getParameter(static::PARAMETER_KEY_COLUMNS);
$selects = [];
$table = $this->getTable();
foreach ($columnDefinitions as $columnDefinition) {
$expression = $columnDefinition[self::PARAMETER_KEY_COLUMN_EXPRESSION];
$columName = $columnDefinition[self::PARAMETER_KEY_COLUMN_NAME];
$expression = $columnDefinition[static::PARAMETER_KEY_COLUMN_EXPRESSION];
$columName = $columnDefinition[static::PARAMETER_KEY_COLUMN_NAME];
$columnPhpName = $table->getColumn($columName)->getPhpName();
$selects[] = "$expression AS $columnPhpName";
}
Expand All @@ -254,15 +256,15 @@ private function buildSelectionStatement()
/**
* @return string
*/
protected function addObjectUpdate()
protected function addObjectUpdate(): string
{
$table = $this->getTable();
$columnPhpNames = array_map(function (array $colDef) use ($table) {
$columName = $colDef[AggregateMultipleColumnsBehavior::PARAMETER_KEY_COLUMN_NAME];
$columnPhpNames = array_map(function (array $columnParameters) use ($table) {
$columName = $columnParameters[AggregateMultipleColumnsBehavior::PARAMETER_KEY_COLUMN_NAME];

return $table->getColumn($columName)->getPhpName();
},
$this->getParameter(self::PARAMETER_KEY_COLUMNS));
$this->getParameter(static::PARAMETER_KEY_COLUMNS));

return $this->renderTemplate('objectUpdate', [
'aggregationName' => $this->getAggregationName(),
Expand All @@ -273,12 +275,12 @@ protected function addObjectUpdate()
/**
* @return string
*/
private function getForeignTableNameFullyQualified()
private function getForeignTableNameFullyQualified(): string
{
$database = $this->getTable()->getDatabase();
$foreignTableName = $database->getTablePrefix() . $this->getParameter(self::PARAMETER_KEY_FOREIGN_TABLE);
$foreignTableName = $database->getTablePrefix() . $this->getParameter(static::PARAMETER_KEY_FOREIGN_TABLE);
$platform = $database->getPlatform();
$foreignSchema = $this->getParameter(self::PARAMETER_KEY_FOREIGN_SCHEMA);
$foreignSchema = $this->getParameter(static::PARAMETER_KEY_FOREIGN_SCHEMA);
if ($platform->supportsSchemas() && $foreignSchema) {
$foreignTableName = $foreignSchema . $platform->getSchemaDelimiter() . $foreignTableName;
}
Expand All @@ -289,7 +291,7 @@ private function getForeignTableNameFullyQualified()
/**
* @return \Propel\Generator\Model\Table|null
*/
protected function getForeignTable()
protected function getForeignTable(): Table
{
$database = $this->getTable()->getDatabase();
$foreignTableName = $this->getForeignTableNameFullyQualified();
Expand All @@ -300,7 +302,7 @@ protected function getForeignTable()
/**
* @return \Propel\Generator\Model\ForeignKey
*/
protected function getForeignKey()
protected function getForeignKey(): ForeignKey
{
$foreignTable = $this->getForeignTable();
// let's infer the relation from the foreign table
Expand All @@ -322,7 +324,7 @@ protected function getForeignKey()
*
* @return void
*/
private function throwInvalidArgumentExceptionWithLocation($format, ...$args)
private function throwInvalidArgumentExceptionWithLocation($format, ...$args): void
{
$format .= ' in the \'aggregate_multiple_columns\' behavior definition in the \'%s\' table definition';
$args[] = $this->getTable()->getName();
Expand Down
10 changes: 5 additions & 5 deletions src/Propel/Generator/Builder/Util/SchemaReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ protected function isAlreadyParsed($filePath)
*
* @return void
*/
private function initParameterListCollector($attributes)
private function initParameterListCollector(array $attributes): void
{
$parameterName = $this->getExpectedValue($attributes, 'name');

Expand All @@ -568,7 +568,7 @@ private function initParameterListCollector($attributes)
*
* @return void
*/
private function addItemToParameterListCollector()
private function addItemToParameterListCollector(): void
{
$this->currParameterListCollector['value'][] = [];
}
Expand All @@ -580,7 +580,7 @@ private function addItemToParameterListCollector()
*
* @return void
*/
private function addAttributeToParameterListItem($attributes)
private function addAttributeToParameterListItem(array $attributes)
{
$name = $this->getExpectedValue($attributes, 'name');
$value = $this->getExpectedValue($attributes, 'value');
Expand All @@ -595,7 +595,7 @@ private function addAttributeToParameterListItem($attributes)
*
* @return void
*/
private function finalizeParameterList()
private function finalizeParameterList(): void
{
$parentTag = $this->peekCurrentSchemaTag();
if ($parentTag === 'behavior') {
Expand All @@ -615,7 +615,7 @@ private function finalizeParameterList()
*
* @return string the non-empty value
*/
private function getExpectedValue($attributes, $key)
private function getExpectedValue(array $attributes, string $key): string
{
if (empty($attributes[$key])) {
$this->throwSchemaExceptionWithLocation('Parameter misses expected attribute "%s"', $key);
Expand Down