Beispiel #1
0
 /**
  * @throws \Exception
  */
 public function testMergeByConditionComplex1()
 {
     $expected = ['sharedKey1' => 'sharedValue1', 'sharedKey2' => 'sharedValue2', 'MasterKey231' => 'MasterKeyValue231', 'MasterKey232' => 'MasterKeyValue232'];
     $this->assertEquals($expected, Arr::mergeByCondition($this->testData, 'masterKey2'));
     $this->assertEquals($expected, Arr::mergeByCondition($this->testData, 'masterKey3'));
 }
 /**
  * Public validation function to validate some object
  *
  * @param mixed $object Object to validate
  *
  * @return bool
  */
 public function isThisValid($object = null)
 {
     $objectData = $this->getObjectData($object);
     $this->lastState = $this->getState($object);
     $rules = Arr::mergeByCondition($this->rules, $this->lastState);
     $ruleParser = new RuleParser();
     $rules = $ruleParser->parse($rules, $this->templateReplacements);
     $rules = $this->preProcessRules($rules, $objectData);
     //Registering custom validation rules
     foreach ($this->customRules as $ruleName => $customRuleData) {
         ValidationRuleRegistry::registerClosure($customRuleData[0], $customRuleData[1]);
     }
     //Creating validator
     $validatorFactory = new ValidatorFactory($this->validatorFactory);
     $validator = $validatorFactory->create($rules, $objectData);
     $validator->setCustomMessages($this->preProcessValidationErrorMessages($this->customValidationMessages, $rules, $objectData));
     $this->setupValidator($validator);
     $method_name = 'setupValidatorFor' . Str::studly($this->lastState);
     if (method_exists($this, $method_name)) {
         $this->{$method_name}($validator);
     }
     $this->validationPassed = !$validator->fails();
     if ($this->validationPassed) {
         $this->dataStorage = new ArrayDataStorage($this->keyCase);
         $this->dataStorage->setItems(Arr::only($objectData, array_keys($rules)));
         $this->failedMessages = new MessageBag();
         $this->failedRules = [];
     } else {
         $this->failedMessages = $validator->getMessageBag();
         $this->failedRules = $validator->failed();
         $this->dataStorage = null;
     }
     return $this->validationPassed;
 }