Skip to content

Commit

Permalink
We want one single source of truth when we want to know if an entity …
Browse files Browse the repository at this point in the history
…has errors. Nested entities should also be taken in consideration.
  • Loading branch information
JoshuaLuckers committed Jul 5, 2018
1 parent f597821 commit 7ddc4a1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Datasource/EntityInterface.php
Expand Up @@ -29,7 +29,7 @@
* @method array getVirtual()
* @method $this setDirty($property, $isDirty)
* @method bool isDirty($property = null)
* @method bool canBePersisted()
* @method bool hasErrors($includeHasErrorsFromNestedEntities = true)
* @method array getErrors()
* @method array getError($field)
* @method array setErrors(array $fields, $overwrite = false)
Expand Down
54 changes: 41 additions & 13 deletions src/Datasource/EntityTrait.php
Expand Up @@ -90,13 +90,6 @@ trait EntityTrait
*/
protected $_new = true;

/**
* Returns whether this entity can be persisted.
*
* @var bool
*/
protected $_canBePersisted = true;

/**
* List of errors per field as stored in this object
*
Expand Down Expand Up @@ -865,7 +858,6 @@ public function clean()
{
$this->_dirty = [];
$this->_errors = [];
$this->_canBePersisted = true;
$this->_invalid = [];
$this->_original = [];
}
Expand Down Expand Up @@ -900,13 +892,28 @@ public function isNew($new = null)
}

/**
* Returns whether this entity can be persisted.
* Returns whether this entity has errors.
*
* @param bool $includeHasErrorsFromNestedEntities
* @return bool
*/
public function canBePersisted()
public function hasErrors($includeHasErrorsFromNestedEntities = true)
{
return $this->_canBePersisted;
if (empty($this->_errors) === false) {
return true;
}

if ($includeHasErrorsFromNestedEntities === false) {
return false;
}

foreach ($this->_properties as $property) {
if ($this->_readHasErrors($property) === true) {
return true;
}
}

return false;
}

/**
Expand Down Expand Up @@ -961,7 +968,6 @@ public function getError($field)
*/
public function setErrors(array $fields, $overwrite = false)
{
$this->_canBePersisted = false;
if ($overwrite) {
foreach ($fields as $f => $error) {
$this->_errors[$f] = (array)$error;
Expand Down Expand Up @@ -1109,6 +1115,29 @@ protected function _nestedErrors($field)
return [];
}

/**
* Reads if there are errors for one or many objects.
*
* @param array|\Cake\Datasource\EntityTrait $object The object to read errors from.
* @return bool
*/
protected function _readHasErrors($object)
{
if ($object instanceof EntityInterface && $object->hasErrors() === true) {
return true;
}

if (is_array($object) === true) {
foreach ($object as $value) {
if ($this->_readHasErrors($value) === true) {
return true;
}
}
}

return false;
}

/**
* Read the error(s) from one or many objects.
*
Expand Down Expand Up @@ -1426,7 +1455,6 @@ public function __debugInfo()
return $properties + [
'[new]' => $this->isNew(),
'[accessible]' => $this->_accessible,
'[persistable]' => $this->_canBePersisted,
'[dirty]' => $this->_dirty,
'[original]' => $this->_original,
'[virtual]' => $this->_virtual,
Expand Down
2 changes: 1 addition & 1 deletion src/ORM/Table.php
Expand Up @@ -1902,7 +1902,7 @@ public function save(EntityInterface $entity, $options = [])
'_primary' => true
]);

if ($entity->canBePersisted() === false) {
if ($entity->hasErrors($options['associated']) === true) {
return false;
}

Expand Down

0 comments on commit 7ddc4a1

Please sign in to comment.