From 3cc54359e8078f737af19f2c3dfecbf78fc19523 Mon Sep 17 00:00:00 2001 From: Chris Smoak Date: Tue, 23 Jan 2018 09:50:06 -0700 Subject: [PATCH] Allow turning humanized keys on/off. --- src/Attribute.php | 8 ++++++++ src/Validation.php | 4 +++- src/Validator.php | 12 ++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Attribute.php b/src/Attribute.php index 5416df4..2d28ef4 100644 --- a/src/Attribute.php +++ b/src/Attribute.php @@ -9,6 +9,8 @@ class Attribute protected $key; + protected $humanizedKey; + protected $alias; protected $validation; @@ -24,6 +26,7 @@ public function __construct(Validation $validation, $key, $alias = null, array $ $this->validation = $validation; $this->alias = $alias; $this->key = $key; + $this->humanizedKey = ucfirst(str_replace('_', ' ', $key)); foreach($rules as $rule) { $this->addRule($rule); } @@ -94,6 +97,11 @@ public function getKey() return $this->key; } + public function getHumanizedKey() + { + return $this->humanizedKey; + } + public function setAlias($alias) { $this->alias = $alias; diff --git a/src/Validation.php b/src/Validation.php index c98640b..1e34cf0 100644 --- a/src/Validation.php +++ b/src/Validation.php @@ -245,8 +245,10 @@ protected function resolveAttributeName(Attribute $attribute) return $this->aliases[$attribute->getKey()]; } elseif($primaryAttribute AND isset($this->aliases[$primaryAttribute->getKey()])) { return $this->aliases[$primaryAttribute->getKey()]; + } elseif ($this->validator->getUseHumanizedKeys()) { + return $attribute->getHumanizedKey(); } else { - return ucfirst(str_replace('_', ' ', $attribute->getKey())); + return $attribute->getKey(); } } diff --git a/src/Validator.php b/src/Validator.php index a06a95c..1ef39ab 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -11,6 +11,8 @@ class Validator protected $allowRuleOverride = false; + protected $useHumanizedKeys = true; + public function __construct(array $messages = []) { $this->messages = $messages; @@ -124,4 +126,14 @@ public function allowRuleOverride($status = false) { $this->allowRuleOverride = $status; } + + public function setUseHumanizedKeys($useHumanizedKeys = true) + { + $this->useHumanizedKeys = $useHumanizedKeys; + } + + public function getUseHumanizedKeys() + { + return $this->useHumanizedKeys; + } }