Skip to content

Commit

Permalink
Trivial draft support for Entity types. ultra-alpha, development only!
Browse files Browse the repository at this point in the history
  • Loading branch information
twalder-docnet committed Dec 7, 2015
1 parent 7475bd6 commit 6df3a40
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/GDS/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ protected function extractPropertyValue($int_type, $obj_property)
case Schema::PROPERTY_STRING_LIST:
return $this->extractStringListValue($obj_property);

case Schema::PROPERTY_ENTITY:
return $this->extractEntityValue($obj_property);

case Schema::PROPERTY_DETECT:
return $this->extractAutoDetectValue($obj_property);

Expand Down
28 changes: 28 additions & 0 deletions src/GDS/Mapper/ProtoBuf.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,14 @@ private function configureGooglePropertyValue(Value $obj_val, array $arr_field_d
}
break;

case Schema::PROPERTY_ENTITY:
$obj_val->setIndexed(false);
$obj_entity = $obj_val->mutableEntityValue();
foreach($mix_value as $str_field_name => $mix_field_value) {
$obj_entity->addProperty()->setName($str_field_name)->mutableValue()->setStringValue($mix_field_value);
}
break;

default:
throw new \RuntimeException('Unable to process field type: ' . $arr_field_def['type']);
}
Expand Down Expand Up @@ -271,6 +279,26 @@ protected function extractStringListValue($obj_property)
return null;
}

/**
* Extract an Entity property
*
* @todo expand auto detect types
*
* @param Value $obj_property
* @return mixed
*/
protected function extractEntityValue($obj_property)
{
$obj_response = (object)[];
if($obj_property->hasEntityValue()) {
$obj_google_entity = $obj_property->getEntityValue();
foreach($obj_google_entity->getPropertyList() as $obj_property) {
$obj_response->{$obj_property->getName()} = $this->extractAutoDetectValue($obj_property->getValue());
}
}
return $obj_response;
}

/**
* Auto detect & extract a value
*
Expand Down
14 changes: 14 additions & 0 deletions src/GDS/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Schema
const PROPERTY_FLOAT = 4; // FLOAT === DOUBLE
const PROPERTY_BOOLEAN = 10; // 10 types of people...
const PROPERTY_STRING_LIST = 20;
const PROPERTY_ENTITY = 30;
const PROPERTY_DETECT = 99; // used for auto-detection

/**
Expand Down Expand Up @@ -157,6 +158,19 @@ public function addStringList($str_name, $bol_index = true)
return $this->addProperty($str_name, self::PROPERTY_STRING_LIST, $bol_index);
}

/**
* Add an entity field to the schema.
*
* Entity properties cannot be indexed
*
* @param $str_name
* @return Schema
*/
public function addEntity($str_name)
{
return $this->addProperty($str_name, self::PROPERTY_ENTITY, false);
}

/**
* Get the Kind
*
Expand Down

0 comments on commit 6df3a40

Please sign in to comment.