Submission::__set( string $property, mixed $value )

Magic setter.

Description

Sets a property value.

See also

Parameters

$property

(string) (Required) Property to set.

$value

(mixed) (Required) Property value.

Source

File: src/db-objects/submissions/submission.php

	public function __set( $property, $value ) {
		if ( 'values' === $property ) {
			$this->set_submission_values_data( $value );
			return;
		}

		if ( preg_match( '/^element_([0-9]+)_([a-z_]+)_value$/U', $property, $matches ) ) {
			if ( ! isset( $this->values ) ) {
				$this->values = $this->get_submission_values_data();
			}

			$original_value = $value;

			$value = (array) $value;

			$indexes_to_remove = array();

			foreach ( $this->values as $index => $item ) {
				$item['field'] = ! empty( $item['field'] ) ? $item['field'] : '_main';

				if ( (int) $matches[1] !== (int) $item['element_id'] || $matches[2] !== $item['field'] ) {
					continue;
				}

				if ( ! empty( $value ) ) {
					$this->values[ $index ]['value'] = array_shift( $value );
				} else {
					$indexes_to_remove[] = $index;
				}
			}

			foreach ( $indexes_to_remove as $index_to_remove ) {
				unset( $this->values[ $index_to_remove ] );
			}

			foreach ( $value as $single_value ) {
				$this->values[] = array(
					'id'         => 0,
					'element_id' => (int) $matches[1],
					'field'      => $matches[2],
					'value'      => $single_value,
				);
			}

			$this->get_element_values_data();

			$this->element_values[ $matches[1] ][ $matches[2] ] = $original_value;
		}

		parent::__set( $property, $value );
	}

Changelog

Changelog
Version Description
1.0.0 Introduced.