Skip to content
Snippets Groups Projects
Select Git revision
  • 63e25879c1e21a4f79f04443f2947e58b09b6c7b
  • master default protected
  • cleanup_fixtures
  • add-openvox
  • freebsd-14
  • remove-legacy-top-scope-syntax
  • rel430
  • tests
  • revert-363-augeas-module-cleanup
  • release-4.1.0
  • puppet8
  • relax-dependencies
  • rel400
  • mode
  • puppet7
  • release-3.1.0
  • freebsd13
  • freebsd11
  • stdlib
  • centos
  • fedora
  • v5.1.0
  • v5.0.0
  • v4.5.0
  • v4.4.0
  • v4.3.0
  • v4.2.1
  • v4.2.0
  • v4.1.0
  • v4.0.0
  • v3.1.0
  • v3.0.0
  • v2.0.0
  • 1.12.0
  • 1.11.0
  • 1.10.0
  • 1.9.0
  • 1.8.0
  • 1.7.0
  • 1.6.0
  • 1.5.0
41 results

Modulefile

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    AxiosExtractor.php 4.58 KiB
    <?php
    
    namespace UnicaenVue\Axios;
    
    use Doctrine\Common\Collections\Collection;
    use Doctrine\ORM\Query;
    
    class AxiosExtractor
    {
        public static $loopControl = 50;
    
        const DATETIME_FORMAT = 'Y-m-d\TH:i:s.u\Z'; // timestamp ISO 8601 pour HTML5
    
        protected array $triggers = [];
    
    
    
        public static function extract($data, array $properties = [], array $triggers = [])
        {
            $axios = new self;
            $axios->triggers = $triggers;
    
            return $axios->extractData($data, $properties);
        }
    
    
    
        protected function extractData($data, array $properties = [], string $path = '')
        {
            $triggerPath = $path ? $path : '/';
            if ($data instanceof Query) {
                $result = $this->extractData($data->getResult(), $properties, '');
            } elseif ($this->isList($data)) {
                $result = $this->extractList($data, $properties, $path);
                $triggerPath .= '[]';
            } elseif (is_array($data)) {
                $result = $this->extractArray($data, $properties, $path);
            } elseif ($data instanceof \DateTime) {
                $result = $data->format(self::DATETIME_FORMAT);
            } elseif (is_object($data)) {
                $result = $this->extractObject($data, $properties, $path);
            } else {
                $result = $data;
            }
    
            if (array_key_exists($triggerPath, $this->triggers) && !$data instanceof Query) {
                // trigger est un callable qui accepte deux arguments : le premier les la donnée originale, le second la donnée extraite
                // il doit retourner une donnée qui remplacera la donnée extraite
                $result = $this->triggers[$triggerPath]($data, $result);
            }
    
            return $result;
        }
    
    
    
        protected function extractObject($data, array $properties, string $path = ''): array
        {
            // contrôle de boucle récursive, afin de ne pas saturer la mémoire...
            if (substr_count($path, '/') >= self::$loopControl) {
                //return [];
                throw new \Exception("AxiosExtractor has detected a possible infinite loop, and aborted your script with a stack depth of '" . self::$loopControl . "' frames");
            }
    
            $result = [];
    
            if (empty($properties) && $data instanceof AxiosExtractorInterface) {
                $properties = $data->axiosDefinition();
            }