Skip to content

Commit

Permalink
phalcon#14203 - Changed Phalcon\Validation::getValue() behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Aziz Muzafarov committed Nov 7, 2021
1 parent 1710591 commit 4408ca5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 36 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
- `setHtmlQuoteType()` becomes `setFlags()` [#15757](https://github.com/phalcon/cphalcon/issues/15757)
- Changed `Phalcon\Encryption\Security::hash()` to also use `password_hash()` and accept `ARGON2*` algorithms [#15731](https://github.com/phalcon/cphalcon/issues/15731)
- Removed uncamelize of `realClassName` in `Phalcon\Mvc\Router\Route::getRoutePaths()` if definition is string to make processing same as if array definition [#15067](https://github.com/phalcon/cphalcon/issues/15067)
- Changed `Phalcon\Validation::getValue()` behavior to get value from `data` if not found in `entity`. [#14203](https://github.com/phalcon/cphalcon/issues/14203)

## Added
- Added more tests in the suite for additional code coverage [#15691](https://github.com/phalcon/cphalcon/issues/15691)
Expand Down
89 changes: 53 additions & 36 deletions phalcon/Validation.zep
Original file line number Diff line number Diff line change
Expand Up @@ -238,57 +238,74 @@ class Validation extends Injectable implements ValidationInterface
return this->validators;
}

private function getValueByEntity(entity, string field) -> var | null
{
string method;

let method = "get" . camelize(field);

if method_exists(entity, method) {
return entity->{method}();
} elseif method_exists(entity, "readAttribute") {
return entity->readAttribute(field);
} elseif isset entity->{field} {
return entity->{field};
} else {
return null;
}
}

private function getValueByData(data, string field) -> var | null
{
var value, values;

// Check if there is a calculated value
let values = this->values;

if fetch value, values[field] {
return value;
}

if typeof data == "array" {
if isset data[field] {
return data[field];
}
} elseif typeof data == "object" {
if isset data->{field} {
return data->{field};
}
}

return null;
}

/**
* Gets the a value to validate in the array/object data source
*
* @param string $field
*/
public function getValue(string field) -> var | null
{
var entity, method, value, data, values, filters, fieldFilters,
container, filterService, camelizedField;
var entity, method, value, data, filters, fieldFilters,
container, filterService;
bool isRawFetched;

let isRawFetched = false;
let entity = this->entity;
let data = this->data;

// If the entity is an object use it to retrieve the values
if typeof entity == "object" {
let camelizedField = camelize(field);
let method = "get" . camelizedField;

if method_exists(entity, method) {
let value = entity->{method}();
} elseif method_exists(entity, "readAttribute") {
let value = entity->readAttribute(field);
} elseif isset entity->{field} {
let value = entity->{field};
} else {
let value = null;
let value = this->getValueByEntity(entity, field);
if value === null {
let isRawFetched = true;
let value = this->getValueByData(data, field);
}
} else {
let data = this->data;

if unlikely (typeof data != "array" && typeof data != "object") {
throw new Exception("There is no data to validate");
}

// Check if there is a calculated value
let values = this->values;

if fetch value, values[field] {
return value;
}

let value = null;

if typeof data == "array" {
if isset data[field] {
let value = data[field];
}
} elseif typeof data == "object" {
if isset data->{field} {
let value = data->{field};
}
}
let value = this->getValueByData(data, field);
}

if value === null {
Expand Down Expand Up @@ -324,8 +341,8 @@ class Validation extends Injectable implements ValidationInterface
/**
* Set filtered value in entity
*/
if typeof entity == "object" {
let method = "set" . camelizedField;
if typeof entity == "object" && isRawFetched == false {
let method = "set" . camelize(field);

if method_exists(entity, method) {
entity->{method}(value);
Expand Down

0 comments on commit 4408ca5

Please sign in to comment.