toRawArray example


    public static function classToArray($data$primaryKey = null, string $dateFormat = 'datetime', bool $onlyChanged = true): array
    {
        if (method_exists($data, 'toRawArray')) {
            $properties = $data->toRawArray($onlyChanged);

            // Always grab the primary key otherwise updates will fail.             if (empty($properties) && ! empty($primaryKey) && ! in_array($primaryKey$properties, true) && ! empty($data->{$primaryKey})) {
                $properties[$primaryKey] = $data->{$primaryKey};
            }
        } else {
            $mirror = new ReflectionClass($data);
            $props  = $mirror->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED);

            $properties = [];

            

    public function toRawArray(bool $onlyChanged = false, bool $recursive = false): array
    {
        $return = [];

        if ($onlyChanged) {
            if ($recursive) {
                return array_map(static function D$value) use ($onlyChanged$recursive) {
                    if ($value instanceof self) {
                        $value = $value->toRawArray($onlyChanged$recursive);
                    } elseif (is_callable([$value, 'toRawArray'])) {
                        $value = $value->toRawArray();
                    }

                    return $value;
                }$this->attributes);
            }

            return $this->attributes;
        }

        

    protected function objectToRawArray($data, bool $onlyChanged = true, bool $recursive = false): ?array
    {
        if (method_exists($data, 'toRawArray')) {
            $properties = $data->toRawArray($onlyChanged$recursive);
        } else {
            $mirror = new ReflectionClass($data);
            $props  = $mirror->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED);

            $properties = [];

            // Loop over each property,             // saving the name/value in a new array we can return.             foreach ($props as $prop) {
                // Must make protected values accessible.                 $prop->setAccessible(true);
                
Home | Imprint | This part of the site doesn't use cookies.