Your IP : 216.73.216.68


Current Path : /home/jeromecohp/www/tmp/yooessentials/modules/core/src/
Upload File :
Current File : /home/jeromecohp/www/tmp/yooessentials/modules/core/src/UpdateTransform.php

<?php
/**
 * @package   Essentials YOOtheme Pro (Freemium) 1.6.8
 * @author    ZOOlanders https://www.zoolanders.com
 * @copyright Copyright (C) Joolanders, SL
 * @license   http://www.gnu.org/licenses/gpl.html GNU/GPL
 */

namespace ZOOlanders\YOOessentials;

class UpdateTransform
{
    /**
     * @var string
     */
    protected $version;

    /**
     * @var array
     */
    protected $updates = [];

    /**
     * @var array
     */
    protected $globals = [];

    /**
     * Constructor.
     */
    public function __construct(string $version)
    {
        $this->version = $version;
    }

    /**
     * Transform callback.
     */
    public function __invoke(object $node, array &$params)
    {
        if (isset($node->transient)) {
            return;
        }

        $nodeYooessentialsVersion = $node->yooessentialsVersion ?? $params['yooessentialsVersion'] ?? '1.0.0';

        if ($node->type === 'layout') {
            $node->yooessentialsVersion = $this->version;
        } else {
            unset($node->yooessentialsVersion);
        }

        $type = $params['type'];

        // check node version and dev
        if (version_compare($nodeYooessentialsVersion, $this->version, '>=')) {
            return;
        }

        // apply update callbacks
        foreach ($this->resolveUpdates($type, $nodeYooessentialsVersion) as $update) {
            $update($node, $params);
        }
    }

    /**
     * Adds global updates for any type.
     */
    public function addGlobals(array $globals): self
    {
        $this->globals[] = $globals['nodes'] ?? [];

        return $this;
    }

    /**
     * Resolves updates for a type and current version.
     */
    protected function resolveUpdates(object $type, string $version): array
    {
        if (isset($this->updates[$type->name][$version])) {
            return $this->updates[$type->name][$version];
        }

        $updates = $this->globals;
        ;

        // get only yooessentials scoped updates
        if (isset($type->yooessentialsUpdates)) {
            $updates[] = $type->yooessentialsUpdates;
        }

        $resolved = [];

        foreach ($updates as $update) {
            foreach ($update as $ver => $func) {
                if (version_compare($ver, $version, '>') && is_callable($func)) {
                    $resolved[$ver][] = $func;
                }
            }
        }

        uksort($resolved, 'version_compare');

        return $this->updates[$type->name][$version] = $resolved ? array_merge(...array_values($resolved)) : [];
    }
}