Skip to content
Permalink
Browse files

#9978 Working on the entity getters and setters

  • Loading branch information...
burzum committed Jan 18, 2017
1 parent 2b943c3 commit 5722d8b29a14b5a6dff8b25b984bd537ec56b528
Showing with 196 additions and 1 deletion.
  1. +196 −1 src/Datasource/EntityTrait.php
@@ -417,6 +417,36 @@ public function hiddenProperties($properties = null)
return $this;
}
/**
* Sets hidden properties.
*
* @param null|array $properties Either an array of properties to treat as virtual or null to get properties
* @param bool $merge Merge the new properties with the existing. By default false.
* @return array|self
*/
public function setHiddenProperties(array $properties, $merge = false)
{
if ($merge === false) {
$this->_hidden = $properties;
return $this;
}
$this->_hidden += $properties;
return $this;
}
/**
* Gets the hidden properties.
*
* @return array
*/
public function getHiddenProperties()
{
return $this->_hidden;
}
/**
* Get/Set the virtual properties on this entity.
*
@@ -436,6 +466,36 @@ public function virtualProperties($properties = null)
return $this;
}
/**
* Sets the virtual properties on this entity.
*
* @param array $properties An array of properties to treat as virtual.
* @param bool $merge Merge the new properties with the existing. By default false.
* @return self
*/
public function setVirtualProperties(array $properties, $merge = false)
{
if ($merge === false) {
$this->_virtual = $properties;
return $this;
}
$this->_virtual += $properties;
return $this;
}
/**
* Gets the virtual properties on this entity.
*
* @return array
*/
public function getVirtualProperties()
{
return $this->_virtual;
}
/**
* Get the list of visible properties.
*
@@ -728,6 +788,88 @@ public function isNew($new = null)
return $this->_new = $new;
}
/**
* Returns all validation errors.
*
* @param string $field
* @return array
*/
public function getErrors()
{
$diff = array_diff_key($this->_properties, $this->_errors);
return $this->_errors + (new Collection($diff))
->filter(function ($value) {
return is_array($value) || $value instanceof EntityInterface;
})
->map(function ($value) {
return $this->_readError($value);
})
->filter()
->toArray();
}
/**
* Returns validation errors of a field
*
* @param string $field Field name to get the errors from
* @return array
*/
public function getError($field)
{
$errors = isset($this->_errors[$field]) ? $this->_errors[$field] : [];
if ($errors) {
return $errors;
}
return $this->_nestedErrors($field);
}
/**
* Sets error messages to the entity
*
* ## Example
*
* ```
* // Sets the error messages for multiple fields at once
* $entity->errors(['salary' => ['message'], 'name' => ['another message']);
* ```
* @param array|null $field The field to get errors for, or the array of errors to set.
* @param bool $overwrite Whether or not to overwrite pre-existing errors for $field
* @return self
*/
public function setErrors(array $fields, $overwrite = false)
{
foreach ($fields as $f => $error) {
$this->_errors += [$f => []];
$this->_errors[$f] = $overwrite ?
(array)$error :
array_merge($this->_errors[$f], (array)$error);
}
return $this;
}
/**
* Sets errors for a single field
*
* ### Example
*
* ```
* // Sets the error messages for a single field
* $entity->errors('salary', ['must be numeric', 'must be a positive number']);
* ```
*
* @param string|array|null $field The field to get errors for, or the array of errors to set.
* @param string|array|null $errors The errors to be set for $field
* @param bool $overwrite Whether or not to overwrite pre-existing errors for $field
* @return self
*/
public function setError($field, array $errors, $overwrite = false)
{
return $this->setErrors([$field => $errors], $overwrite);
}
/**
* Sets the error messages for a field or a list of fields. When called
* without the second argument it returns the validation
@@ -939,6 +1081,44 @@ public function invalid($field = null, $value = null, $overwrite = false)
* @return self|bool
*/
public function accessible($property, $set = null)
{
return $this->setAccess($property, $set);
}
/**
* Stores whether or not a property value can be changed or set in this entity.
* The special property `*` can also be marked as accessible or protected, meaning
* that any other property specified before will take its value. For example
* `$entity->accessible('*', true)` means that any property not specified already
* will be accessible by default.
*
* You can also call this method with an array of properties, in which case they
* will each take the accessibility value specified in the second argument.
*
* ### Example:
*
* ```
* $entity->accessible('id', true); // Mark id as not protected
* $entity->accessible('author_id', false); // Mark author_id as protected
* $entity->accessible(['id', 'user_id'], true); // Mark both properties as accessible
* $entity->accessible('*', false); // Mark all properties as protected
* ```
*
* When called without the second param it will return whether or not the property
* can be set.
*
* ### Example:
*
* ```
* $entity->accessible('id'); // Returns whether it can be set or not
* ```
*
* @param string|array $property single or list of properties to change its accessibility
* @param bool|null $set true marks the property as accessible, false will
* mark it as protected.
* @return self|bool
*/
public function setAccess($property, $set = null)
{
if ($set === null) {
$value = isset($this->_accessible[$property]) ?
@@ -973,7 +1153,7 @@ public function accessible($property, $set = null)
* @param string|null $alias the alias of the repository
* @return string|self
*/
public function source($alias = null)
public function getSource($alias = null)
{
if ($alias === null) {
return $this->_registryAlias;
@@ -983,6 +1163,21 @@ public function source($alias = null)
return $this;
}
/**
* Returns the alias of the repository from which this entity came from.
*
* If called with no arguments, it returns the alias of the repository
* this entity came from if it is known.
*
* @deprecated Use setSource() instead.
* @param string|null $alias the alias of the repository
* @return string|self
*/
public function source($alias = null)
{
return $this->getSource($alias);
}
/**
* Returns a string representation of this object in a human readable format.
*

0 comments on commit 5722d8b

Please sign in to comment.
You can’t perform that action at this time.