getItemOperationAttribute() public method

Gets an item operation attribute, optionally fallback to a resource attribute.
public getItemOperationAttribute ( string $operationName, string $key, mixed $defaultValue = null, boolean $resourceFallback = false ) : mixed
$operationName string
$key string
$defaultValue mixed
$resourceFallback boolean
return mixed
Exemplo n.º 1
0
 public function testValueObject()
 {
     $metadata = new ResourceMetadata('shortName', 'desc', 'http://example.com/foo', ['iop1' => ['foo' => 'a'], 'iop2' => ['bar' => 'b']], ['cop1' => ['foo' => 'c'], 'cop2' => ['bar' => 'd']], ['baz' => 'bar']);
     $this->assertEquals('shortName', $metadata->getShortName());
     $this->assertEquals('desc', $metadata->getDescription());
     $this->assertEquals('http://example.com/foo', $metadata->getIri());
     $this->assertEquals(['iop1' => ['foo' => 'a'], 'iop2' => ['bar' => 'b']], $metadata->getItemOperations());
     $this->assertEquals('a', $metadata->getItemOperationAttribute('iop1', 'foo', 'z', false));
     $this->assertEquals('bar', $metadata->getItemOperationAttribute('iop1', 'baz', 'z', true));
     $this->assertEquals('z', $metadata->getItemOperationAttribute('iop1', 'notExist', 'z', true));
     $this->assertEquals('z', $metadata->getItemOperationAttribute('notExist', 'notExist', 'z', true));
     $this->assertEquals(['cop1' => ['foo' => 'c'], 'cop2' => ['bar' => 'd']], $metadata->getCollectionOperations());
     $this->assertEquals('c', $metadata->getCollectionOperationAttribute('cop1', 'foo', 'z', false));
     $this->assertEquals('bar', $metadata->getCollectionOperationAttribute('cop1', 'baz', 'z', true));
     $this->assertEquals('z', $metadata->getCollectionOperationAttribute('cop1', 'notExist', 'z', true));
     $this->assertEquals('z', $metadata->getCollectionOperationAttribute('notExist', 'notExist', 'z', true));
     $this->assertEquals(['baz' => 'bar'], $metadata->getAttributes());
     $this->assertEquals('bar', $metadata->getAttribute('baz'));
     $this->assertEquals('z', $metadata->getAttribute('notExist', 'z'));
     $newMetadata = $metadata->withShortName('name');
     $this->assertNotSame($metadata, $newMetadata);
     $this->assertEquals('name', $newMetadata->getShortName());
     $newMetadata = $metadata->withDescription('description');
     $this->assertNotSame($metadata, $newMetadata);
     $this->assertEquals('description', $newMetadata->getDescription());
     $newMetadata = $metadata->withIri('foo:bar');
     $this->assertNotSame($metadata, $newMetadata);
     $this->assertEquals('foo:bar', $newMetadata->getIri());
     $newMetadata = $metadata->withItemOperations(['a' => ['b' => 'c']]);
     $this->assertNotSame($metadata, $newMetadata);
     $this->assertEquals(['a' => ['b' => 'c']], $newMetadata->getItemOperations());
     $newMetadata = $metadata->withCollectionOperations(['a' => ['b' => 'c']]);
     $this->assertNotSame($metadata, $newMetadata);
     $this->assertEquals(['a' => ['b' => 'c']], $newMetadata->getCollectionOperations());
     $newMetadata = $metadata->withAttributes(['a' => ['b' => 'c']]);
     $this->assertNotSame($metadata, $newMetadata);
     $this->assertEquals(['a' => ['b' => 'c']], $newMetadata->getAttributes());
 }
Exemplo n.º 2
0
 /**
  * Gets the route name or null if not defined.
  *
  * @param ResourceMetadata $resourceMetadata
  * @param string           $operationName
  * @param bool             $collection
  *
  * @return string|null
  */
 private function getRouteName(ResourceMetadata $resourceMetadata, string $operationName, bool $collection)
 {
     if ($collection) {
         return $resourceMetadata->getCollectionOperationAttribute($operationName, 'route_name');
     }
     return $resourceMetadata->getItemOperationAttribute($operationName, 'route_name');
 }
Exemplo n.º 3
0
 private function getGroupsContext(ResourceMetadata $resourceMetadata, string $operationName, bool $isNormalization)
 {
     $groupsContext = $isNormalization ? 'normalization_context' : 'denormalization_context';
     $itemOperationAttribute = $resourceMetadata->getItemOperationAttribute($operationName, $groupsContext, ['groups' => []], true)['groups'];
     $collectionOperationAttribute = $resourceMetadata->getCollectionOperationAttribute($operationName, $groupsContext, ['groups' => []], true)['groups'];
     return [$groupsContext => ['groups' => array_merge(isset($itemOperationAttribute) ? $itemOperationAttribute : [], isset($collectionOperationAttribute) ? $collectionOperationAttribute : [])]];
 }
Exemplo n.º 4
0
 /**
  * @param \ArrayObject     $definitions
  * @param bool             $collection
  * @param bool             $parameter
  * @param ResourceMetadata $resourceMetadata
  * @param string           $resourceClass
  * @param string           $operationName
  *
  * @return string
  */
 private function getDefinition(\ArrayObject $definitions, bool $collection, bool $parameter, ResourceMetadata $resourceMetadata, string $resourceClass, string $operationName) : string
 {
     $contextKey = $parameter ? 'denormalization_context' : 'normalization_context';
     $serializerContext = $collection ? $resourceMetadata->getCollectionOperationAttribute($operationName, $contextKey, null, true) : $resourceMetadata->getItemOperationAttribute($operationName, $contextKey, null, true);
     if (isset($serializerContext['groups'])) {
         $definitionKey = sprintf('%s_%s', $resourceMetadata->getShortName(), md5(serialize($serializerContext['groups'])));
     } else {
         $definitionKey = $resourceMetadata->getShortName();
     }
     $definitions[$definitionKey] = $this->getDefinitionSchema($resourceClass, $resourceMetadata, $serializerContext);
     return $definitionKey;
 }