Example #1
0
 /**
  * Test hydrateObject (with being passed failure list).
  *
  * @test
  * @uses   \Bairwell\Hydrator
  * @uses   \Bairwell\Hydrator\CachedClass
  * @uses   \Bairwell\Hydrator\CachedProperty
  * @uses   \Bairwell\Hydrator\Failure
  * @uses   \Bairwell\Hydrator\FailureList
  * @uses   \Bairwell\Hydrator\Annotations\AsBase
  * @uses   \Bairwell\Hydrator\Annotations\AsInt
  * @uses   \Bairwell\Hydrator\Annotations\AsString
  * @uses   \Bairwell\Hydrator\Annotations\From
  * @covers \Bairwell\Hydrator::hydrateObject
  */
 public function testHydrateObject()
 {
     $sut = new Hydrator();
     $sources = new Hydrator\Sources();
     $sources->add('dummySource', function ($fieldName) {
         switch ($fieldName) {
             case 'numbered':
                 return "1245";
             case 'stringed':
                 return 45;
             default:
                 throw new \Exception('Unexpected source field - part 1 ' . $fieldName);
         }
     });
     $sources->add('other', function ($fieldName) {
         switch ($fieldName) {
             case 'testOther':
                 return 'correct';
             default:
                 throw new \Exception('Unexpected source field for other part 1: ' . $fieldName);
         }
     });
     $conditions = new Hydrator\Conditionals();
     $conditions->add('sunrisen', function () {
         return true;
     });
     $conditions->add('moonrisen', function () {
         return false;
     });
     $mockedObject = new Hydrator\MockedObject();
     $failures = new Hydrator\FailureList();
     $dummyFailure = new Hydrator\Failure();
     $dummyFailure->setInputField('unittest');
     $dummyFailure->setMessage('unittest made message');
     $dummyFailure->setSource('jeff');
     $failures->add($dummyFailure);
     $failures = $sut->hydrateObject($mockedObject, $sources, $conditions, $failures);
     // check the failures
     $this->assertInstanceOf('\\Bairwell\\Hydrator\\FailureList', $failures);
     $this->assertCount(1, $failures);
     $this->assertSame($dummyFailure, $failures[0]);
     // now check the contents
     $this->assertInstanceOf('\\Bairwell\\Hydrator\\MockedObject', $mockedObject);
     $this->assertInternalType('integer', $mockedObject->testIntCast);
     $this->assertEquals(1245, $mockedObject->testIntCast);
     $this->assertNull($mockedObject->testStringCast);
     $this->assertInternalType('string', $mockedObject->testOther);
     $this->assertEquals('correct', $mockedObject->testOther);
     // okay, now let's repeat that with the same object but different sources and conditions.
     $sources = new Hydrator\Sources();
     $sources->add(['dummySource', 'other'], function ($fieldName) {
         switch ($fieldName) {
             case 'numbered':
                 return null;
             case 'testOther':
                 return null;
             case 'stringed':
                 return 45;
             default:
                 throw new \Exception('Unexpected source field for second part:' . $fieldName);
         }
     });
     $conditions->unset('moonrisen');
     $conditions->add('moonrisen', function () {
         return true;
     });
     $failures = $sut->hydrateObject($mockedObject, $sources, $conditions, $failures);
     // check the failures
     $this->assertInstanceOf('\\Bairwell\\Hydrator\\FailureList', $failures);
     $this->assertCount(1, $failures);
     $this->assertSame($dummyFailure, $failures[0]);
     // now check the contents
     $this->assertInstanceOf('\\Bairwell\\Hydrator\\MockedObject', $mockedObject);
     $this->assertInternalType('integer', $mockedObject->testIntCast);
     $this->assertEquals(1245, $mockedObject->testIntCast);
     $this->assertInternalType('string', $mockedObject->testStringCast);
     $this->assertEquals('45', $mockedObject->testStringCast);
     $this->assertInternalType('string', $mockedObject->testOther);
     $this->assertEquals('correct', $mockedObject->testOther);
 }
Example #2
0
 /**
  * Hydrate a single property via single source.
  *
  * @param mixed          $currentValue Current value of the property.
  * @param array|callable $source       The source we are using.
  * @param string         $sourceName   Name of the source.
  * @param string         $fromField    Which field should we be reading from.
  * @param CachedProperty $property     The property we are working on.
  * @param FailureList    $failureList  Referenced list of failures.
  *
  * @return mixed The new value.
  * @throws \TypeError If source is not callable or array.
  */
 private function hydrateSinglePropertyViaSource($currentValue, $source, string $sourceName, string $fromField, CachedProperty $property, FailureList &$failureList)
 {
     if (true === is_array($source)) {
         $data = null;
         if (true === array_key_exists($fromField, $source)) {
             $data = $source[$fromField];
         }
     } elseif (true === is_callable($source)) {
         $data = call_user_func($source, $fromField);
     } else {
         throw new \TypeError('Source must be an array or callable: got ' . gettype($source));
     }
     $isValid = true;
     if (null === $data || true === is_array($data) && 0 === count($data)) {
         $isValid = false;
     }
     if (true === $isValid) {
         $arrayStyles = $property->getFrom()->arrayStyles;
         if (false === empty($arrayStyles)) {
             $data = $this->extractFromArray($data, $arrayStyles);
         }
         if (false === $property->hasCastAs()) {
             $currentValue = $data;
             if (null !== $this->logger) {
                 $this->logger->debug('Hydrator: No cast setting for field {fromField}: {currentValue}', ['fromField' => $fromField, 'currentValue' => $currentValue]);
             }
         } else {
             $castAs = $property->getCastAs();
             $newValue = $castAs->cast($data);
             if (true === $castAs->hasErrored()) {
                 $failure = new Failure();
                 $failure->setInputField($fromField)->setInputValue($data)->setMessage($castAs->getErrorMessage())->setTokens($castAs->getErrorTokens())->setSource($sourceName);
                 $failureList->add($failure);
                 if (null !== $this->logger) {
                     $this->logger->debug('Hydrator: Cast failed for field {fromField}: {currentValue}: {castErrorMessage}', ['fromField' => $fromField, 'currentValue' => $currentValue, 'castErrorMessage' => $castAs->getErrorMessage()]);
                 }
             } else {
                 $currentValue = $newValue;
             }
         }
         //end if
     }
     //end if
     return $currentValue;
 }