コード例 #1
0
 public function testSimpleMapMultiplePropsWithListIdNeedleObjectDataButWithMax()
 {
     //Arrange
     $data = new \stdClass();
     $data->Name = "foobar";
     $data->Idiot = "true";
     $data->Animals = array("5", "1");
     $elephant = new \stdClass();
     $elephant->id = 5;
     $elephant->Name = "Elephant";
     $cat = new \stdClass();
     $cat->id = 1;
     $cat->Name = "Cat";
     $animalList = array($elephant, $cat);
     $vars = get_defined_vars();
     //Act
     $got = \BTRSchemeMap::Map($data, __DIR__ . "/sample2.json", $vars);
     //Assert
     $this->assertTrue($got->Name === "foobar");
     $this->assertTrue(count($got->Animals) === 1);
     $this->assertTrue($got->Animals[0]->Name === "Elephant");
 }
コード例 #2
0
 public static function Map($data, $scheme, $vars = null)
 {
     if (is_null($data) || !file_exists($scheme)) {
         return null;
     }
     $scheme = json_decode(file_get_contents($scheme));
     $result = new \stdClass();
     if (is_null($scheme)) {
         return null;
     }
     $propMode = false;
     if (is_object($data)) {
         $propMode = true;
     }
     foreach ($scheme as $propertyName => $propertyValue) {
         if (!$propMode) {
             $value = isset($data[$propertyName]) ? $data[$propertyName] : null;
         } else {
             $value = property_exists($data, $propertyName) ? $data->{$propertyName} : null;
         }
         $name = $propertyName;
         if (strtolower($propertyValue->Type) === "string") {
             $result->{$name} = \BTRSchemeMap::ConvertString($value);
         } else {
             if (strtolower($propertyValue->Type) === "boolean") {
                 $result->{$name} = \BTRSchemeMap::ConvertBoolean($value);
             } else {
                 if (strtolower($propertyValue->Type) === "int") {
                     $result->{$name} = intval($value);
                 } else {
                     if (strtolower($propertyValue->Type) === "list") {
                         $haystack = !is_null($vars) && isset($vars[$propertyValue->Source]) ? $vars[$propertyValue->Source] : null;
                         if (is_null($haystack)) {
                             $result->{$name} = array();
                         } else {
                             $got = array();
                             $currentCount = 0;
                             $maximum = -1;
                             if (property_exists($propertyValue, "Max")) {
                                 $maximum = $propertyValue->Max;
                             }
                             foreach ($value as $needle) {
                                 if (is_numeric($needle)) {
                                     $hayResult = \BTRSchemeMap::Pick($haystack, intval($needle));
                                     if (!is_null($hayResult)) {
                                         $got[] = $hayResult;
                                     }
                                     $currentCount++;
                                     if ($maximum !== -1 && $currentCount === $maximum) {
                                         break;
                                     }
                                 }
                             }
                             $result->{$name} = $got;
                         }
                     }
                 }
             }
         }
     }
     return $result;
 }