/** * Extracts nested validation errors * * @param EntityInterface $entity Entity to extract * * @return array */ protected function _getErrors(EntityInterface $entity) { $errors = $entity->errors(); foreach ($entity->visibleProperties() as $property) { $v = $entity[$property]; if ($v instanceof EntityInterface) { $errors[$property] = $this->_getErrors($v); } elseif (is_array($v)) { foreach ($v as $key => $varValue) { if ($varValue instanceof EntityInterface) { $errors[$property][$key] = $this->_getErrors($varValue); } } } } return Hash::filter($errors); }
/** * Checks if the provided entity has defined certain $property, regardless of * its value. * * @param \Cake\Datasource\EntityInterface $entity The entity to check * @param string $property The property name * @return bool True if exists */ public function propertyExists(EntityInterface $entity, $property) { $visibleProperties = $entity->visibleProperties(); return in_array($property, $visibleProperties); }
/** * Generates a unique hash for the given entity. * * Used by revision system to detect if an entity has changed or not. * * @param \Cake\Datasource\EntityInterface $entity The entity for which calculate its hash * @return string MD5 hash for this particular entity */ protected function _calculateHash($entity) { $hash = []; foreach ($entity->visibleProperties() as $property) { if (strpos($property, 'created') === false && strpos($property, 'created_by') === false && strpos($property, 'modified') === false && strpos($property, 'modified_by') === false) { if ($property == '_fields') { foreach ($entity->get('_fields') as $field) { if ($field instanceof \Field\Model\Entity\Field) { $hash[] = is_object($field->value) || is_array($field->value) ? md5(serialize($field->value)) : md5($field->value); } } } else { $hash[] = $entity->get($property); } } } return md5(serialize($hash)); }
/** * Creates changelog report in string format. * * Example: * * Subject: changed from 'Foo' to 'Bar'. * Content: changed from 'Hello world' to 'Hi there'. * * @param \Cake\Datasource\EntityInterface $entity Entity instance * @return string */ protected function _getChangelog(EntityInterface $entity) { $result = ''; // plain changelog if entity is new if ($entity->isNew()) { return $result; } // get entity's modified fields $fields = $entity->extractOriginalChanged($entity->visibleProperties()); if (empty($fields)) { return $result; } // remove ignored fields foreach ($this->_ignoredFields as $field) { if (!array_key_exists($field, $fields)) { continue; } unset($fields[$field]); } if (empty($fields)) { return $result; } foreach ($fields as $k => $v) { $result .= sprintf(static::CHANGELOG, Inflector::humanize($k), $v, $entity->{$k}); } return $result; }
/** * Prepares entity's cache-columns (those defined using `cache` option). * * @param \Cake\Datasource\EntityInterface $entity The entity to prepare * @return \Cake\Datasource\EntityInterfa Modified entity */ protected function _prepareCachedColumns(EntityInterface $entity) { if ($this->config('cacheMap')) { foreach ((array) $this->config('cacheMap') as $column => $fields) { if (in_array($column, $entity->visibleProperties())) { $string = $entity->get($column); if ($string == serialize(false) || @unserialize($string) !== false) { $entity->set($column, unserialize($string)); } else { $entity->set($column, new CachedColumn()); } } } } return $entity; }