1 <?php 2 /* 3 * Copyright 2015 Google Inc. All rights reserved. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 // To run, use the `php_sample.sh` script. 19 20 // It is recommended that you use PSR autoload when using FlatBuffers. 21 function __autoload($class_name) { 22 $class = substr($class_name, strrpos($class_name, "\\") + 1); 23 $root_dir = join(DIRECTORY_SEPARATOR, array(dirname(dirname(__FILE__)))); // `flatbuffers` root. 24 $paths = array(join(DIRECTORY_SEPARATOR, array($root_dir, "php")), 25 join(DIRECTORY_SEPARATOR, array($root_dir, "samples", "MyGame", "Sample"))); 26 foreach ($paths as $path) { 27 $file = join(DIRECTORY_SEPARATOR, array($path, $class . ".php")); 28 if (file_exists($file)) { 29 require($file); 30 break; 31 } 32 } 33 } 34 35 // Example how to use FlatBuffers to create and read binary buffers. 36 function main() { 37 $builder = new Google\FlatBuffers\FlatbufferBuilder(0); 38 39 // Create some weapons for our Monster using the `createWeapon()` helper function. 40 $weapon_one = $builder->createString("Sword"); 41 $sword = \MyGame\Sample\Weapon::CreateWeapon($builder, $weapon_one, 3); 42 $weapon_two = $builder->createString("Axe"); 43 $axe = \MyGame\Sample\Weapon::CreateWeapon($builder, $weapon_two, 5); 44 45 // Serialize the FlatBuffer data. 46 $name = $builder->createString("Orc"); 47 48 $treasure = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); 49 $inv = \MyGame\Sample\Monster::CreateInventoryVector($builder, $treasure); 50 51 $weaps = array($sword, $axe); 52 $weapons = \MyGame\Sample\Monster::CreateWeaponsVector($builder, $weaps); 53 54 $pos = \MyGame\Sample\Vec3::CreateVec3($builder, 1.0, 2.0, 3.0); 55 56 \MyGame\Sample\Monster::StartMonster($builder); 57 \MyGame\Sample\Monster::AddPos($builder, $pos); 58 \MyGame\Sample\Monster::AddHp($builder, 300); 59 \MyGame\Sample\Monster::AddName($builder, $name); 60 \MyGame\Sample\Monster::AddInventory($builder, $inv); 61 \MyGame\Sample\Monster::AddColor($builder, \MyGame\Sample\Color::Red); 62 \MyGame\Sample\Monster::AddWeapons($builder, $weapons); 63 \MyGame\Sample\Monster::AddEquippedType($builder, \MyGame\Sample\Equipment::Weapon); 64 \MyGame\Sample\Monster::AddEquipped($builder, $weaps[1]); 65 $orc = \MyGame\Sample\Monster::EndMonster($builder); 66 67 $builder->finish($orc); // You may also call `\MyGame\Sample\Monster::FinishMonsterBuffer($builder, $orc);`. 68 69 // We now have a FlatBuffer that can be stored on disk or sent over a network. 70 71 // ...Code to store to disk or send over a network goes here... 72 73 // Instead, we are going to access it right away, as if we just received it. 74 75 $buf = $builder->dataBuffer(); 76 77 // Get access to the root: 78 $monster = \MyGame\Sample\Monster::GetRootAsMonster($buf); 79 80 $success = true; // Tracks if an assert occurred. 81 82 // Note: We did not set the `mana` field explicitly, so we get back the default value. 83 $success &= assert($monster->getMana() == 150); 84 $success &= assert($monster->getHp() == 300); 85 $success &= assert($monster->getName() == "Orc"); 86 $success &= assert($monster->getColor() == \MyGame\Sample\Color::Red); 87 $success &= assert($monster->getPos()->getX() == 1.0); 88 $success &= assert($monster->getPos()->getY() == 2.0); 89 $success &= assert($monster->getPos()->getZ() == 3.0); 90 91 // Get and test the `inventory` FlatBuffer `vector`. 92 for ($i = 0; $i < $monster->getInventoryLength(); $i++) { 93 $success &= assert($monster->getInventory($i) == $i); 94 } 95 96 // Get and test the `weapons` FlatBuffer `vector` of `table`s. 97 $expected_weapon_names = array("Sword", "Axe"); 98 $expected_weapon_damages = array(3, 5); 99 for ($i = 0; $i < $monster->getWeaponsLength(); $i++) { 100 $success &= assert($monster->getWeapons($i)->getName() == $expected_weapon_names[$i]); 101 $success &= assert($monster->getWeapons($i)->getDamage() == $expected_weapon_damages[$i]); 102 } 103 104 // Get and test the `equipped` FlatBuffer `union`. 105 $success &= assert($monster->getEquippedType() == \MyGame\Sample\Equipment::Weapon); 106 $success &= assert($monster->getEquipped(new \MyGame\Sample\Weapon())->getName() == "Axe"); 107 $success &= assert($monster->getEquipped(new \MyGame\Sample\Weapon())->getDamage() == 5); 108 109 if ($success) { 110 print("The FlatBuffer was successfully created and verified!\n"); 111 } 112 } 113 114 main(); 115 ?> 116