Example #1
0
 /**
  * Hydrate all value objects within a JSON-LD node object.
  *
  * The node object represents a resource entity.
  *
  * Parses value objects according to the existence of certain properties, in
  * order of priority:
  *
  * - property_id: all value types must contain a property ID
  * - @value: hydrate a literal
  * - value_resource_id: hydrate a resource value
  * - @id: hydrate a URI value
  *
  * A value object that contains none of the above combinations is ignored.
  *
  * @param array $nodeObject A JSON-LD node object representing a resource
  * @param Resource $resource The owning resource entity instance
  * @param bool $append Whether to simply append instead of replacing
  *  existing values
  */
 public function hydrate(array $nodeObject, $resource, $append = false)
 {
     $newValues = [];
     $valueCollection = $resource->getValues();
     $existingValues = $valueCollection->toArray();
     // Iterate all properties in a node object. Note that we ignore terms.
     foreach ($nodeObject as $property => $valueObjects) {
         // Value objects must be contained in lists
         if (!is_array($valueObjects)) {
             continue;
         }
         // Iterate a node object list
         foreach ($valueObjects as $valueObject) {
             // Value objects must be lists and contain a property ID.
             if (!(is_array($valueObject) && isset($valueObject['property_id']))) {
                 continue;
             }
             // Value objects must be mapped to a hydrate method.
             $hydrateMethod = $this->getHydrateMethod($valueObject);
             if (!$hydrateMethod) {
                 continue;
             }
             $value = current($existingValues);
             if ($value === false || $append) {
                 $value = new Value();
                 $newValues[] = $value;
             } else {
                 // Null out values as we re-use them
                 $existingValues[key($existingValues)] = null;
                 next($existingValues);
             }
             // Hydrate a single JSON-LD value object
             $property = $this->adapter->getEntityManager()->getReference('Omeka\\Entity\\Property', $valueObject['property_id']);
             $value->setResource($resource);
             $value->setProperty($property);
             $this->{$hydrateMethod}($valueObject, $value);
         }
     }
     // Remove any values that weren't reused
     if (!$append) {
         foreach ($existingValues as $key => $existingValue) {
             if ($existingValue !== null) {
                 $valueCollection->remove($key);
             }
         }
     }
     // Add any new values that had to be created
     foreach ($newValues as $newValue) {
         $valueCollection->add($newValue);
     }
 }
Example #2
0
 public function __construct()
 {
     parent::__construct();
     $this->media = new ArrayCollection();
     $this->siteBlockAttachments = new ArrayCollection();
     $this->itemSets = new ArrayCollection();
 }
Example #3
0
 public function __construct()
 {
     parent::__construct();
     $this->items = new ArrayCollection();
 }