Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 880 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
class Versions {
        private $parametres = null;
        private $ressources = null;
        private $bdd = null;
        private $projetNom = null;
        private $versionsDispo = array();
        private $versionCourrante = array();

        public function __construct(Parametres $parametres, Ressources $ressources, Bdd $bdd) {
                $this->parametres = $parametres;
                $this->ressources = $ressources;
                $this->bdd = $bdd;
                $this->projetNom = $this->ressources->getProjetNom();
                $this->versionsDispo = $this->chargerVersionsDisponibles();
        }

        private function chargerVersionsDisponibles() {
                $tableMeta = $this->projetNom.'_meta';
                $requete = "SELECT version FROM $tableMeta";
                $resultat = $this->bdd->recupererTous($requete);
                if ($resultat == '') {
                        //cas ou la requete comporte des erreurs
                        $message = "La requête SQL de versionnage formée comporte une erreur : $requete";
                        $code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
                        throw new Exception($message, $code);
                } elseif ($resultat === false) {
                        $message = "Versions introuvables dans la table des méta-données";
                        $code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
                        throw new Exception($message, $code);
                } else {
                        foreach ($resultat as $version) {
                                $versionsDispo[] = $version['version'];
                        }
                }

                return $versionsDispo;
        }

        public function getVersions() {
                $versions = array();
                $versionDemandee = $this->parametres->get('version.projet');
                if ($versionDemandee == '+') {
                        $versions[] = end($this->versionsDispo);
                } else if (is_numeric($versionDemandee)) {
                        if (in_array($versionDemandee, $this->versionsDispo)) {
                                $versions[] = $versionDemandee;
                        }
                } else if ($versionDemandee == '*') {
                        $versions = $this->versionsDispo;
                }
                $versions = $this->remplacerPointParUnderscore($versions);
                return $versions;
        }

        private function remplacerPointParUnderscore($versions) {
                if (count($versions) > 0) {
                        foreach ($versions as $cle => $valeur) {
                                $versions[$cle] = str_replace('.', '_', $valeur);
                        }
                }
                return $versions;
        }
}
?>