Bolt\Storage\Entity\ContentValuesTrait::setValue PHP Метод

setValue() публичный Метод

Set a ContentType record's individual value.
public setValue ( string $key, mixed $value )
$key string
$value mixed
    public function setValue($key, $value)
    {
        // Don't set templateFields if not a real contenttype
        if ($key === 'templatefields' && !$this->isRootType) {
            return;
        }
        // Check if the value need to be unserialized.
        if (is_string($value) && substr($value, 0, 2) === 'a:') {
            try {
                $unserdata = Lib::smartUnserialize($value);
            } catch (\Exception $e) {
                $unserdata = false;
            }
            if ($unserdata !== false) {
                $value = $unserdata;
            }
        }
        if ($key === 'id') {
            $this->id = $value;
        }
        // Set the user in the object.
        if ($key === 'ownerid' && !empty($value)) {
            $this->user = $this->app['users']->getUser($value);
        }
        // Only set values if they have are actually a field.
        $allowedcolumns = self::getBaseColumns();
        $allowedcolumns[] = 'taxonomy';
        if (!isset($this->contenttype['fields'][$key]) && !in_array($key, $allowedcolumns)) {
            return;
        }
        /**
         * This Block starts introducing new-style hydration into the legacy content object.
         * To do this we fetch the new field from the manager and hydrate a temporary entity.
         *
         * We don't return at this point so continue to let other transforms happen below so the
         * old behaviour will still happen where adjusted.
         */
        if (isset($this->contenttype['fields'][$key]['type']) && $this->app['storage.field_manager']->hasCustomHandler($this->contenttype['fields'][$key]['type'])) {
            $newFieldType = $this->app['storage.field_manager']->getFieldFor($this->contenttype['fields'][$key]['type']);
            $newFieldType->mapping['fieldname'] = $key;
            $entity = new Content();
            $newFieldType->hydrate([$key => $value], $entity);
            $value = $entity->{$key};
        }
        if (in_array($key, ['datecreated', 'datechanged', 'datepublish', 'datedepublish'])) {
            if (!preg_match("/(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2})/", $value)) {
                // @todo Try better date-parsing, instead of just setting it to
                // 'now' (or 'the past' for datedepublish)
                if ($key === 'datedepublish') {
                    $value = null;
                } else {
                    $value = date('Y-m-d H:i:s');
                }
            }
        }
        if ($key === 'templatefields') {
            if (is_string($value) || is_array($value)) {
                if (is_string($value)) {
                    try {
                        $unserdata = Lib::smartUnserialize($value);
                    } catch (\Exception $e) {
                        $unserdata = false;
                    }
                } else {
                    $unserdata = $value;
                }
                if (is_array($unserdata)) {
                    if (is_a($this, Legacy\Content::class)) {
                        $value = new static($this->app, $this->getTemplateFieldsContentType(), [], false);
                    } else {
                        $value = new Legacy\Content($this->app, $this->getTemplateFieldsContentType(), [], false);
                    }
                    $value->setValues($unserdata);
                } else {
                    $value = null;
                }
            }
        }
        if (!isset($this->values['datechanged']) || !preg_match("/(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2})/", $this->values['datechanged'])) {
            $this->values['datechanged'] = date('Y-m-d H:i:s');
        }
        $this->values[$key] = $value;
    }