This commit is contained in:
Philipp Holzer 2021-10-10 22:45:25 +02:00
parent 7061e16b27
commit f273c27e3b
Signed by: nupplaPhil
GPG key ID: 24A7501396EB5432
7 changed files with 99 additions and 10 deletions

View file

@ -87,7 +87,7 @@ class BaseCollection extends \ArrayIterator
*/ */
public function column($column, $index_key = null) public function column($column, $index_key = null)
{ {
return array_column($this->getArrayCopy(), $column, $index_key); return array_column($this->getArrayCopy(true), $column, $index_key);
} }
/** /**
@ -124,4 +124,21 @@ class BaseCollection extends \ArrayIterator
{ {
return new static(array_reverse($this->getArrayCopy()), $this->getTotalCount()); return new static(array_reverse($this->getArrayCopy()), $this->getTotalCount());
} }
/**
* @inheritDoc
*
* includes recursion for entity::toArray() function
* @see BaseEntity::toArray()
*/
public function getArrayCopy(bool $recursive = false): array
{
if (!$recursive) {
return parent::getArrayCopy();
}
return array_map(function ($item) {
return is_object($item) ? $item->toArray() : $item;
}, parent::getArrayCopy());
}
} }

View file

@ -287,6 +287,9 @@ class Index extends BaseSettings
$profileFieldInputs['new']['value'], $profileFieldInputs['new']['value'],
$permissionSet $permissionSet
)); ));
unset($profileFieldInputs['new']);
unset($profileFieldOrder['new']);
} }
foreach ($profileFieldInputs as $id => $profileFieldInput) { foreach ($profileFieldInputs as $id => $profileFieldInput) {

View file

@ -30,6 +30,7 @@ use Friendica\Profile\ProfileField\Factory;
use Friendica\Profile\ProfileField\Entity; use Friendica\Profile\ProfileField\Entity;
use Friendica\Profile\ProfileField\Collection; use Friendica\Profile\ProfileField\Collection;
use Friendica\Security\PermissionSet\Depository\PermissionSet as PermissionSetDepository; use Friendica\Security\PermissionSet\Depository\PermissionSet as PermissionSetDepository;
use Friendica\Util\DateTimeFormat;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
class ProfileField extends BaseDepository class ProfileField extends BaseDepository
@ -46,7 +47,7 @@ class ProfileField extends BaseDepository
{ {
parent::__construct($database, $logger, $factory); parent::__construct($database, $logger, $factory);
$this->permissionSetDepository = permissionSetDepository; $this->permissionSetDepository = $permissionSetDepository;
} }
/** /**
@ -91,11 +92,13 @@ class ProfileField extends BaseDepository
protected function convertToTableRow(Entity\ProfileField $profileField): array protected function convertToTableRow(Entity\ProfileField $profileField): array
{ {
return [ return [
'uid' => $profileField->uid,
'label' => $profileField->label, 'label' => $profileField->label,
'value' => $profileField->value, 'value' => $profileField->value,
'order' => $profileField->order, 'order' => $profileField->order,
'created' => $profileField->created, 'created' => $profileField->created->format(DateTimeFormat::MYSQL),
'edited' => $profileField->edited, 'edited' => $profileField->edited->format(DateTimeFormat::MYSQL),
'psid' => $profileField->permissionSetId
]; ];
} }
@ -205,7 +208,7 @@ class ProfileField extends BaseDepository
try { try {
if ($profileField->id) { if ($profileField->id) {
$this->db->update(self::$table_name, $fields, ['id' => $profileField]); $this->db->update(self::$table_name, $fields, ['id' => $profileField->id]);
} else { } else {
$this->db->insert(self::$table_name, $fields); $this->db->insert(self::$table_name, $fields);
@ -233,7 +236,7 @@ class ProfileField extends BaseDepository
// Update the order based on the new Profile Field Collection // Update the order based on the new Profile Field Collection
$order = 0; $order = 0;
$labelProfileFieldsOld = $profileFieldsOld->column('id', 'label'); $labelProfileFieldsOld = array_flip($profileFieldsOld->column('label'));
foreach ($profileFields as $profileField) { foreach ($profileFields as $profileField) {
// Update existing field (preserve // Update existing field (preserve

View file

@ -58,7 +58,7 @@ class ProfileField extends BaseEntity
/** @var int */ /** @var int */
protected $order; protected $order;
/** @var int */ /** @var int */
protected $psid; protected $permissionSetId;
/** @var string */ /** @var string */
protected $label; protected $label;
/** @var string */ /** @var string */
@ -68,6 +68,9 @@ class ProfileField extends BaseEntity
/** @var \DateTime */ /** @var \DateTime */
protected $edited; protected $edited;
/**
* @throws UnexpectedPermissionSetException In case no Permission Set can be retrieved
*/
public function __construct(PermissionSetDepository $permissionSetDepository, int $uid, int $order, int $permissionSetId, string $label, string $value, \DateTime $created, \DateTime $edited, int $id = null, PermissionSet $permissionSet = null) public function __construct(PermissionSetDepository $permissionSetDepository, int $uid, int $order, int $permissionSetId, string $label, string $value, \DateTime $created, \DateTime $edited, int $id = null, PermissionSet $permissionSet = null)
{ {
$this->permissionSetDepository = $permissionSetDepository; $this->permissionSetDepository = $permissionSetDepository;
@ -75,12 +78,16 @@ class ProfileField extends BaseEntity
$this->uid = $uid; $this->uid = $uid;
$this->order = $order; $this->order = $order;
$this->psid = $permissionSetId; $this->permissionSetId = $permissionSetId ?? ($permissionSet ? $permissionSet->id : null);
$this->label = $label; $this->label = $label;
$this->value = $value; $this->value = $value;
$this->created = $created; $this->created = $created;
$this->edited = $edited; $this->edited = $edited;
$this->id = $id; $this->id = $id;
if (is_null($this->permissionSetId)) {
throw new UnexpectedPermissionSetException('Either set the permission set ID or the permission set itself');
}
} }
/** /**
@ -93,7 +100,7 @@ class ProfileField extends BaseEntity
case 'permissionSet': case 'permissionSet':
if (empty($this->permissionSet)) { if (empty($this->permissionSet)) {
try { try {
$permissionSet = $this->permissionSetDepository->selectOneById($this->psid, $this->uid); $permissionSet = $this->permissionSetDepository->selectOneById($this->permissionSetId, $this->uid);
if ($permissionSet->uid !== $this->uid) { if ($permissionSet->uid !== $this->uid) {
throw new UnexpectedPermissionSetException(sprintf('PermissionSet %d (user-id: %d) for ProfileField %d (user-id: %d) is invalid.', $permissionSet->id, $permissionSet->uid, $this->id, $this->uid)); throw new UnexpectedPermissionSetException(sprintf('PermissionSet %d (user-id: %d) for ProfileField %d (user-id: %d) is invalid.', $permissionSet->id, $permissionSet->uid, $this->id, $this->uid));
} }
@ -130,7 +137,7 @@ class ProfileField extends BaseEntity
$this->value = $value; $this->value = $value;
$this->order = $order; $this->order = $order;
$this->permissionSet = $permissionSet; $this->permissionSet = $permissionSet;
$this->psid = $permissionSet->id; $this->permissionSetId = $permissionSet->id;
$this->edited = new \DateTime('now', new \DateTimeZone('UTC')); $this->edited = new \DateTime('now', new \DateTimeZone('UTC'));
} }

View file

@ -0,0 +1,9 @@
<?php
namespace Friendica\Test\Util;
use Friendica\BaseCollection;
class CollectionDouble extends BaseCollection
{
}

View file

@ -0,0 +1,26 @@
<?php
namespace Friendica\Test\Util;
use Friendica\BaseEntity;
/**
* @property-read string $protString
* @property-read int $protInt
* @property-read \DateTime $protDateTime
*/
class EntityDouble extends BaseEntity
{
protected $protString;
protected $protInt;
protected $protDateTime;
private $privString;
public function __construct(string $protString, int $protInt, \DateTime $protDateTime, string $privString)
{
$this->protString = $protString;
$this->protInt = $protInt;
$this->protDateTime = $protDateTime;
$this->privString = $privString;
}
}

View file

@ -0,0 +1,24 @@
<?php
namespace Friendica\Test\src;
use Friendica\Test\MockedTest;
use Friendica\Test\Util\CollectionDouble;
use Friendica\Test\Util\EntityDouble;
class CollectionTest extends MockedTest
{
/**
* Test if the BaseCollection::column() works as expected
*/
public function testGetArrayCopy()
{
$collection = new CollectionDouble();
$collection->append(new EntityDouble('test', 23, new \DateTime('now', new \DateTimeZone('UTC')), 'privTest'));
$collection->append(new EntityDouble('test2', 25, new \DateTime('now', new \DateTimeZone('UTC')), 'privTest23'));
self::assertEquals(['test', 'test2'], $collection->column('protString'));
self::assertEmpty($collection->column('privString'));
self::assertEquals([23,25], $collection->column('protInt'));
}
}