Commit e0cd5319 authored by Laurent Lecluse's avatar Laurent Lecluse
Browse files

Gestion d'extract pour convertir rapidement en Json un objet

parent 82a0b79e
Loading
Loading
Loading
Loading
+49 −0
Changes for module/Application/src/Controller/Plugin/Axios.php: 49 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -2,9 +2,11 @@

namespace Application\Controller\Plugin;

use Application\Constants;
use Laminas\Mvc\Controller\Plugin\AbstractPlugin;
use Laminas\Mvc\Plugin\FlashMessenger\FlashMessenger;
use Laminas\View\Model\JsonModel;
use UnicaenApp\Util;

/**
 * Description of Axios
@@ -30,6 +32,53 @@ class Axios extends AbstractPlugin



    public function extract($object, array $properties = []): ?array
    {
        if (!$object) {
            return null;
        }

        $result = [];

        if (method_exists($object, 'getId')) {
            $result['id'] = $object->getId();
        }
        if (!empty($properties)) {
            foreach ($properties as $property) {
                if ($property == 'id') continue;
                $method     = 'get' . ucfirst($property);
                $foundValue = false;
                if (method_exists($object, $method)) {
                    $value      = $object->$method();
                    $foundValue = true;
                } else {
                    $method = 'is' . ucfirst($property);
                    if (method_exists($object, $method)) {
                        $value      = $object->$method();
                        $foundValue = true;
                    }
                }

                if ($foundValue) {
                    if (is_object($value)) {
                        if ($value instanceof \DateTime) {
                            $value = $value->format(Util::DATE_FORMAT);
                        } else {
                            $value = $this->extract($value);
                        }
                    }
                    $result[$property] = $value;
                }
            }
        } else {
            $result['libelle'] = (string)$object;
        }

        return $result;
    }



    public function send(array $data): JsonModel
    {
        /** @var FlashMessenger $flashMessenger */