Skip to content

Commit

Permalink
pkp#10328 Resolve conflict with HasCamelCasing
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaliy-1 committed Sep 12, 2024
1 parent caa6428 commit a3ed205
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
2 changes: 0 additions & 2 deletions classes/announcement/Announcement.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use APP\core\Application;
use APP\file\PublicFileManager;
use Eloquence\Behaviours\HasCamelCasing;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
Expand All @@ -40,7 +39,6 @@
*/
class Announcement extends Model
{
use HasCamelCasing;
use ModelWithSettings;

// The subdirectory where announcement images are stored
Expand Down
6 changes: 3 additions & 3 deletions classes/core/SettingsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function update(array $values)
{
// Separate Model's primary values from settings
[$settingValues, $primaryValues] = collect($values)->partition(
fn (array|string $value, string $key) => in_array(Str::camel($key), $this->model->getSettings())
fn (mixed $value, string $key) => in_array(Str::camel($key), $this->model->getSettings())
);

// Don't update settings if they aren't set
Expand All @@ -63,7 +63,7 @@ public function update(array $values)

// TODO Eloquent transforms attributes to snake case, find and override instead of transforming here
$settingValues = $settingValues->mapWithKeys(
fn (array|string $value, string $key) => [Str::camel($key) => $value]
fn (mixed $value, string $key) => [Str::camel($key) => $value]
);

$u = $this->model->getTable();
Expand Down Expand Up @@ -334,7 +334,7 @@ protected function buildUpdateSql(Collection $settingValues, string $us, QueryBu
{
$sql = 'CASE ';
$bindings = [];
$settingValues->each(function (array|string $settingValue, string $settingName) use (&$sql, &$bindings, $us) {
$settingValues->each(function (mixed $settingValue, string $settingName) use (&$sql, &$bindings, $us) {
if ($this->isMultilingual($settingName)) {
foreach ($settingValue as $locale => $localizedValue) {
$sql .= 'WHEN ' . $us . '.setting_name=? AND ' . $us . '.locale=? THEN ? ';
Expand Down
23 changes: 18 additions & 5 deletions classes/core/traits/ModelWithSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace PKP\core\traits;

use Eloquence\Behaviours\HasCamelCasing;
use Exception;
use PKP\core\maps\Schema;
use PKP\core\SettingsBuilder;
Expand All @@ -26,6 +27,8 @@

trait ModelWithSettings
{
use HasCamelCasing;

// The list of attributes associated with the model settings
protected array $settings = [];

Expand Down Expand Up @@ -87,8 +90,7 @@ public function getSettings(): array
*/
public function getMultilingualProps(): array
{
$modelProps = parent::getMultilingualProps();
return array_merge($this->multilingualProps, $modelProps);
return $this->multilingualProps;
}

/**
Expand All @@ -110,7 +112,7 @@ public function getLocalizedData(string $data, ?string $locale = null): mixed
throw new Exception('Attribute ' . $data . ' doesn\'t exist in the ' . static::class . ' model');
}

if (!in_array($data, $this->getMultilingualProps())) {
if (!in_array($data, $this->multilingualProps)) {
throw new Exception('Trying to retrieve localized data from a non-multilingual attribute ' . $data);
}

Expand All @@ -126,8 +128,8 @@ protected function setSchemaData(): void
$schemaService = app()->get('schema'); /** @var PKPSchemaService $schemaService */
$schema = $schemaService->get($this->getSchemaName());
$this->convertSchemaToCasts($schema);
$this->settings = array_merge($this->settings, $schemaService->groupPropsByOrigin($this->getSchemaName())[Schema::ATTRIBUTE_ORIGIN_SETTINGS]);
$this->multilingualProps = array_merge($this->multilingualProps, $schemaService->getMultilingualProps($this->getSchemaName()));
$this->settings = array_merge($this->getSettings(), $schemaService->groupPropsByOrigin($this->getSchemaName())[Schema::ATTRIBUTE_ORIGIN_SETTINGS]);
$this->multilingualProps = array_merge($this->getMultilingualProps(), $schemaService->getMultilingualProps($this->getSchemaName()));
}

/**
Expand All @@ -148,4 +150,15 @@ protected function convertSchemaToCasts(stdClass $schema): void
$this->mergeCasts($propCast);
}

/**
* Override method from HasCamelCasing to retrieve values from setting attributes as it leads to the conflict
*/
public function getAttribute($key): mixed
{
if (in_array($key, $this->getSettings())) {
return parent::getAttribute($key);
}

return $this->isRelation($key) ? parent::getAttribute($key) : parent::getAttribute($this->getSnakeKey($key));
}
}

0 comments on commit a3ed205

Please sign in to comment.