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

6.x: rename _accessible to patchable #18039

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 6 additions & 6 deletions src/Datasource/EntityInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,26 +150,26 @@ public function setError(string $field, array|string $errors, bool $overwrite =
* Stores whether a field value can be changed or set in this entity.
*
* @param array<string>|string $field single or list of fields to change its accessibility
* @param bool $set true marks the field as accessible, false will
* @param bool $set true marks the field as patchable, false will
* mark it as protected.
* @return $this
*/
public function setAccess(array|string $field, bool $set);
public function setPatchable(array|string $field, bool $set);

/**
* Accessible configuration for this entity.
* Patchable configuration for this entity.
*
* @return array<bool>
*/
public function getAccessible(): array;
public function getPatchable(): array;

/**
* Checks if a field is accessible
* Checks if a field can be patched
*
* @param string $field Field name to check
* @return bool
*/
public function isAccessible(string $field): bool;
public function isPatchable(string $field): bool;

/**
* Sets the source alias
Expand Down
54 changes: 27 additions & 27 deletions src/Datasource/EntityTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,18 @@ trait EntityTrait
protected array $_invalid = [];

/**
* Map of fields in this entity that can be safely mass assigned, each
* field name points to a boolean indicating its status. An empty array
* means no fields are accessible for mass assigment.
* Map of fields in this entity that can be safely mass assigned.
* Each field name points to a boolean indicating its status.
* An empty array means no fields can be patched into the entity.
*
* The special field '\*' can also be mapped, meaning that any other field
* not defined in the map will take its value. For example, `'*' => true`
* means that any field not defined in the map will be accessible for mass
* means that any field not defined in the map will be patchable for mass
* assignment by default.
*
* @var array<string, bool>
*/
protected array $_accessible = ['*' => true];
protected array $patchable = ['*' => true];

/**
* The alias of the repository this entity came from
Expand Down Expand Up @@ -277,7 +277,7 @@ public function set(array|string $field, mixed $value = null, array $options = [
foreach ($field as $name => $value) {
/** @psalm-suppress RedundantCastGivenDocblockType */
$name = (string)$name;
if ($options['guard'] === true && !$this->isAccessible($name)) {
if ($options['guard'] === true && !$this->isPatchable($name)) {
continue;
}

Expand Down Expand Up @@ -1259,72 +1259,72 @@ public function setInvalidField(string $field, mixed $value)

/**
* Stores whether a field value can be changed or set in this entity.
* The special field `*` can also be marked as accessible or protected, meaning
* The special field `*` can also be marked as patchable or protected, meaning
* that any other field specified before will take its value. For example
* `$entity->setAccess('*', true)` means that any field not specified already
* will be accessible by default.
* `$entity->setPatchable('*', true)` means that any field not specified already
* will be patchable by default.
*
* You can also call this method with an array of fields, in which case they
* will each take the accessibility value specified in the second argument.
*
* ### Example:
*
* ```
* $entity->setAccess('id', true); // Mark id as not protected
* $entity->setAccess('author_id', false); // Mark author_id as protected
* $entity->setAccess(['id', 'user_id'], true); // Mark both fields as accessible
* $entity->setAccess('*', false); // Mark all fields as protected
* $entity->setPatchable('id', true); // Mark id as not protected
* $entity->setPatchable('author_id', false); // Mark author_id as protected
* $entity->setPatchable(['id', 'user_id'], true); // Mark both fields as patchable
* $entity->setPatchable('*', false); // Mark all fields as protected
* ```
*
* @param array<string>|string $field Single or list of fields to change its accessibility
* @param bool $set True marks the field as accessible, false will
* @param bool $set True marks the field as patchable, false will
* mark it as protected.
* @return $this
*/
public function setAccess(array|string $field, bool $set)
public function setPatchable(array|string $field, bool $set)
{
if ($field === '*') {
$this->_accessible = array_map(fn ($p) => $set, $this->_accessible);
$this->_accessible['*'] = $set;
$this->patchable = array_map(fn ($p) => $set, $this->patchable);
$this->patchable['*'] = $set;

return $this;
}

foreach ((array)$field as $prop) {
$this->_accessible[$prop] = $set;
$this->patchable[$prop] = $set;
}

return $this;
}

/**
* Returns the raw accessible configuration for this entity.
* Returns the raw patchable configuration for this entity.
* The `*` wildcard refers to all fields.
*
* @return array<bool>
*/
public function getAccessible(): array
public function getPatchable(): array
{
return $this->_accessible;
return $this->patchable;
}

/**
* Checks if a field is accessible
* Checks if a field can be patched
*
* ### Example:
*
* ```
* $entity->isAccessible('id'); // Returns whether it can be set or not
* $entity->isPatchable('id'); // Returns whether it can be set or not
* ```
*
* @param string $field Field name to check
* @return bool
*/
public function isAccessible(string $field): bool
public function isPatchable(string $field): bool
{
$value = $this->_accessible[$field] ?? null;
$value = $this->patchable[$field] ?? null;

return ($value === null && !empty($this->_accessible['*'])) || $value;
return ($value === null && !empty($this->patchable['*'])) || $value;
}

/**
Expand Down Expand Up @@ -1375,7 +1375,7 @@ public function __debugInfo(): array

return $fields + [
'[new]' => $this->isNew(),
'[accessible]' => $this->_accessible,
'[patchable]' => $this->patchable,
'[dirty]' => $this->_dirty,
'[original]' => $this->_original,
'[originalFields]' => $this->_originalFields,
Expand Down
4 changes: 2 additions & 2 deletions src/Datasource/RepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public function newEntity(array $data, array $options = []): EntityInterface;
public function newEntities(array $data, array $options = []): array;

/**
* Merges the passed `$data` into `$entity` respecting the accessible
* Merges the passed `$data` into `$entity` respecting the patchable
* fields configured on the entity. Returns the same entity after being
* altered.
*
Expand All @@ -255,7 +255,7 @@ public function patchEntity(EntityInterface $entity, array $data, array $options

/**
* Merges each of the elements passed in `$data` into the entities
* found in `$entities` respecting the accessible fields configured on the entities.
* found in `$entities` respecting the patchable fields configured on the entities.
* Merging is done by matching the primary key in each of the elements in `$data`
* and `$entities`.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Session/DatabaseSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public function write(string $id, string $data): bool
$pkField => $id,
'data' => $data,
'expires' => time() + $this->_timeout,
], ['accessibleFields' => [$pkField => true]]);
], ['patchableFields' => [$pkField => true]]);

return (bool)$this->_table->save($session);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ORM/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Entity implements EntityInterface, InvalidPropertyInterface
* - useSetters: whether use internal setters for properties or not
* - markClean: whether to mark all properties as clean after setting them
* - markNew: whether this instance has not yet been persisted
* - guard: whether to prevent inaccessible properties from being set (default: false)
* - guard: whether to prevent unpatchable properties from being set (default: false)
* - source: A string representing the alias of the repository this entity came from
*
* ### Example:
Expand Down
44 changes: 22 additions & 22 deletions src/ORM/Marshaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ protected function _buildPropertyMap(array $data, array $options): array
* Defaults to true/default.
* - associated: Associations listed here will be marshalled as well. Defaults to null.
* - fields: An allowed list of fields to be assigned to the entity. If not present,
* the accessible fields list in the entity will be used. Defaults to null.
* - accessibleFields: A list of fields to allow or deny in entity accessible fields. Defaults to null
* the patchable fields list in the entity will be used. Defaults to null.
* - patchableFields: A list of fields to allow or deny in entity patchable fields. Defaults to null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if we could shorten this to just patchable. The Fields suffix seems unnecessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

patchable does sound a bit like a boolean flag, though.
But I can understand that the length might be a bit cumbersome.

* - forceNew: When enabled, belongsToMany associations will have 'new' entities created
* when primary key values are set, and a record does not already exist. Normally primary key
* on missing entities would be ignored. Defaults to false.
Expand All @@ -166,7 +166,7 @@ protected function _buildPropertyMap(array $data, array $options): array
* ```
* $result = $marshaller->one($data, [
* 'associated' => [
* 'Tags' => ['accessibleFields' => ['*' => true]]
* 'Tags' => ['patchableFields' => ['*' => true]]
* ]
* ]);
* ```
Expand All @@ -185,7 +185,7 @@ protected function _buildPropertyMap(array $data, array $options): array
* @param array<string, mixed> $options List of options
* @return \Cake\Datasource\EntityInterface
* @see \Cake\ORM\Table::newEntity()
* @see \Cake\ORM\Entity::$_accessible
* @see \Cake\ORM\Entity::$patchable
*/
public function one(array $data, array $options = []): EntityInterface
{
Expand All @@ -194,9 +194,9 @@ public function one(array $data, array $options = []): EntityInterface
$primaryKey = (array)$this->_table->getPrimaryKey();
$entity = $this->_table->newEmptyEntity();

if (isset($options['accessibleFields'])) {
foreach ((array)$options['accessibleFields'] as $key => $value) {
$entity->setAccess($key, $value);
if (isset($options['patchableFields'])) {
foreach ((array)$options['patchableFields'] as $key => $value) {
$entity->setPatchable($key, $value);
}
}
$errors = $this->_validate($data, $options['validate'], true);
Expand Down Expand Up @@ -345,8 +345,8 @@ protected function _marshalAssociation(Association $assoc, mixed $value, array $
* Defaults to true/default.
* - associated: Associations listed here will be marshalled as well. Defaults to null.
* - fields: An allowed list of fields to be assigned to the entity. If not present,
* the accessible fields list in the entity will be used. Defaults to null.
* - accessibleFields: A list of fields to allow or deny in entity accessible fields. Defaults to null
* the patchable fields list in the entity will be used. Defaults to null.
* - patchableFields: A list of fields to allow or deny in entity patchable fields. Defaults to null
* - forceNew: When enabled, belongsToMany associations will have 'new' entities created
* when primary key values are set, and a record does not already exist. Normally primary key
* on missing entities would be ignored. Defaults to false.
Expand All @@ -355,7 +355,7 @@ protected function _marshalAssociation(Association $assoc, mixed $value, array $
* @param array<string, mixed> $options List of options
* @return array<\Cake\Datasource\EntityInterface> An array of hydrated records.
* @see \Cake\ORM\Table::newEntities()
* @see \Cake\ORM\Entity::$_accessible
* @see \Cake\ORM\Entity::$patchable
*/
public function many(array $data, array $options = []): array
{
Expand Down Expand Up @@ -521,8 +521,8 @@ protected function _loadAssociatedByIds(Association $assoc, array $ids): array
* - validate: Whether to validate data before hydrating the entities. Can
* also be set to a string to use a specific validator. Defaults to true/default.
* - fields: An allowed list of fields to be assigned to the entity. If not present
* the accessible fields list in the entity will be used.
* - accessibleFields: A list of fields to allow or deny in entity accessible fields.
* the patchable fields list in the entity will be used.
* - patchableFields: A list of fields to allow or deny in entity patchable fields.
*
* The above options can be used in each nested `associated` array. In addition to the above
* options you can also use the `onlyIds` option for HasMany and BelongsToMany associations.
Expand All @@ -549,7 +549,7 @@ protected function _loadAssociatedByIds(Association $assoc, array $ids): array
* @param array $data key value list of fields to be merged into the entity
* @param array<string, mixed> $options List of options.
* @return \Cake\Datasource\EntityInterface
* @see \Cake\ORM\Entity::$_accessible
* @see \Cake\ORM\Entity::$patchable
*/
public function merge(EntityInterface $entity, array $data, array $options = []): EntityInterface
{
Expand All @@ -562,9 +562,9 @@ public function merge(EntityInterface $entity, array $data, array $options = [])
$keys = $entity->extract((array)$this->_table->getPrimaryKey());
}

if (isset($options['accessibleFields'])) {
foreach ((array)$options['accessibleFields'] as $key => $value) {
$entity->setAccess($key, $value);
if (isset($options['patchableFields'])) {
foreach ((array)$options['patchableFields'] as $key => $value) {
$entity->setPatchable($key, $value);
}
}

Expand Down Expand Up @@ -662,15 +662,15 @@ public function merge(EntityInterface $entity, array $data, array $options = [])
* also be set to a string to use a specific validator. Defaults to true/default.
* - associated: Associations listed here will be marshalled as well.
* - fields: An allowed list of fields to be assigned to the entity. If not present,
* the accessible fields list in the entity will be used.
* - accessibleFields: A list of fields to allow or deny in entity accessible fields.
* the patchable fields list in the entity will be used.
* - patchableFields: A list of fields to allow or deny in entity patchable fields.
*
* @param iterable<\Cake\Datasource\EntityInterface> $entities the entities that will get the
* data merged in
* @param array $data list of arrays to be merged into the entities
* @param array<string, mixed> $options List of options.
* @return array<\Cake\Datasource\EntityInterface>
* @see \Cake\ORM\Entity::$_accessible
* @see \Cake\ORM\Entity::$patchable
*/
public function mergeMany(iterable $entities, array $data, array $options = []): array
{
Expand Down Expand Up @@ -841,8 +841,8 @@ protected function _mergeJoinData(array $original, BelongsToMany $assoc, array $
$associated = $options['associated'] ?? [];
$extra = [];
foreach ($original as $entity) {
// Mark joinData as accessible so we can marshal it properly.
$entity->setAccess('_joinData', true);
// Mark joinData as patchable so we can marshal it properly.
$entity->setPatchable('_joinData', true);

$joinData = $entity->get('_joinData');
if ($joinData instanceof EntityInterface) {
Expand All @@ -858,7 +858,7 @@ protected function _mergeJoinData(array $original, BelongsToMany $assoc, array $
$nested = (array)$associated['_joinData'];
}

$options['accessibleFields'] = ['_joinData' => true];
$options['patchableFields'] = ['_joinData' => true];

$records = $this->mergeMany($original, $value, $options);
foreach ($records as $record) {
Expand Down
12 changes: 6 additions & 6 deletions src/ORM/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -1682,8 +1682,8 @@ protected function _processFindOrCreate(

$entity = $this->newEmptyEntity();
if ($options['defaults'] && is_array($search)) {
$accessibleFields = array_combine(array_keys($search), array_fill(0, count($search), true));
$entity = $this->patchEntity($entity, $search, ['accessibleFields' => $accessibleFields]);
$patchableFields = array_combine(array_keys($search), array_fill(0, count($search), true));
$entity = $this->patchEntity($entity, $search, ['patchableFields' => $patchableFields]);
}
if ($callback !== null) {
$entity = $callback($entity) ?: $entity;
Expand Down Expand Up @@ -2853,13 +2853,13 @@ public function newEmptyEntity(): EntityInterface
* ```
*
* The `fields` option lets remove or restrict input data from ending up in
* the entity. If you'd like to relax the entity's default accessible fields,
* you can use the `accessibleFields` option:
* the entity. If you'd like to relax the entity's default patchable fields,
* you can use the `patchableFields` option:
*
* ```
* $article = $this->Articles->newEntity(
* $this->request->getData(),
* ['accessibleFields' => ['protected_field' => true]]
* ['patchableFields' => ['protected_field' => true]]
* );
* ```
*
Expand Down Expand Up @@ -2953,7 +2953,7 @@ public function newEntities(array $data, array $options = []): array
* ```
* $article = $this->Articles->patchEntity($article, $this->request->getData(), [
* 'associated' => [
* 'Tags' => ['accessibleFields' => ['*' => true]]
* 'Tags' => ['patchableFields' => ['*' => true]]
* ]
* ]);
* ```
Expand Down
Loading