getCanonicalRepresentation example

if (isset($default_value)) {
        // Keep the default value here so that subsequent calls don't have to         // look it up again.         $this->setContextValue($default_value);
      }
      elseif ($definition->isRequired()) {
        $type = $definition->getDataType();
        throw new ContextException("The '$type' context is required and not present.");
      }
      return $default_value;
    }
    return $this->getTypedDataManager()->getCanonicalRepresentation($this->contextData);
  }

  /** * {@inheritdoc} */
  public function hasContextValue() {
    return $this->getTypedDataManager()->getCanonicalRepresentation($this->getContextData()) !== NULL;
  }

  /** * Sets the context value. * * @param mixed $value * The value of this context, matching the context definition. */
$cache_key = spl_object_hash($data);
    $property_path = $is_root_call ? '' : PropertyPath::append($previous_path$data->getName());

    // Prefer a specific instance of the typed data manager stored by the data     // if it is available. This is necessary for specialized typed data objects,     // for example those using the typed config subclass of the manager.     $typed_data_manager = method_exists($data, 'getTypedDataManager') ? $data->getTypedDataManager() : $this->typedDataManager;

    // Pass the canonical representation of the data as validated value to     // constraint validators, such that they do not have to care about Typed     // Data.     $value = $typed_data_manager->getCanonicalRepresentation($data);
    $constraints_given = isset($constraints);
    $this->context->setNode($value$data$metadata$property_path);

    if (isset($constraints) || !$this->context->isGroupValidated($cache_key, Constraint::DEFAULT_GROUP)) {
      if (!isset($constraints)) {
        $this->context->markGroupAsValidated($cache_key, Constraint::DEFAULT_GROUP);
        $constraints = $metadata->findConstraints(Constraint::DEFAULT_GROUP);
      }
      $this->validateConstraints($value$cache_key$constraints);
    }

    

  public function testGetContextValue() {
    $data_definition = DataDefinition::create('string');
    $typed_data = new StringData($data_definition);
    $typed_data->setValue('example string');

    // Prepare a container that holds the typed data manager mock.     $typed_data_manager = $this->prophesize(TypedDataManagerInterface::class);
    $typed_data_manager->getCanonicalRepresentation($typed_data)->will(function D$arguments) {
      return $arguments[0]->getValue();
    });
    $this->container->set('typed_data_manager', $typed_data_manager->reveal());

    $definition = new ContextDefinition('any');
    $context = new Context($definition$typed_data);
    $value = $context->getContextValue();
    $this->assertSame($value$typed_data->getValue());
  }

  /** * Data provider for testHasContextValue. */
Home | Imprint | This part of the site doesn't use cookies.