* @author Jean-Pascal MILCENT * @author Aurelien PERONNET * @license GPL v3 * @license CECILL v2 * @copyright 1999-2014 Tela Botanica */ class Observations extends RestService { private $parametres = array(); private $ressources = array(); private $methode = null; private $serviceNom = 'observations'; private $sousServiceNom = null; private $cheminCourant = null; private $conteneur; /** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */ protected $utilisationParametresBruts = true; public function __construct() { $this->cheminCourant = dirname(__FILE__).DS; } public function consulter($ressources, $parametres) { $this->methode = 'consulter'; $this->initialiserRessourcesEtParametres($ressources, $parametres); return $this->executerService(); } public function ajouter($ressources, $requeteDonnees) { $this->methode = 'ajouter'; $this->initialiserRessourcesEtParametres($ressources, $requeteDonnees); return $this->executerService(); } public function modifier($ressources, $requeteDonnees) { $this->methode = 'modifier'; $this->initialiserRessourcesEtParametres($ressources, $requeteDonnees); return $this->executerService(); } private function executerService() { $reponseHttp = new ReponseHttp(); try { $this->conteneur = new Conteneur($this->parametres); $resultat = $this->traiterRessources(); $reponseHttp->setResultatService($resultat); } catch (Exception $e) { $reponseHttp->ajouterErreur($e); } $reponseHttp->emettreLesEntetes(); $corps = $reponseHttp->getCorps(); return $corps; } private function initialiserRessourcesEtParametres($ressources, $parametres = array()) { $this->ressources = $ressources; $this->parametres = $parametres; } private function traiterRessources() { $this->analyserRessources(); $retour = $this->initialiserService(); return $retour; } private function analyserRessources() { if ($this->methode == 'consulter') { $this->analyserRessoucesConsultation(); } else if ($this->methode == 'modifier' || $this->methode == 'ajouter') { $this->analyserRessoucesModification(); } } private function analyserRessoucesConsultation() { if (count($this->ressources) == 0) { // http://localhost/service:del:0.1/observations $this->sousServiceNom = 'liste-observations'; } else if (count($this->ressources) == 1) { if ($this->etreRessourceIdentifiant(0)) { // http://localhost/service:del:0.1/observations/#idObs $this->sousServiceNom = 'observation-details'; } } else if (count($this->ressources) == 3) { if ($this->etreRessourceIdentifiant(0) && $this->etreRessourceIdentifiant(1) && $this->verifierRessourceValeur(2, 'vote')) { // http://localhost/service:del:0.1/observations/#idObs/#idProposition/vote/ $this->sousServiceNom = 'vote-observation'; } } if ($this->sousServiceNom == null) { $this->lancerMessageErreurRessource(); } } private function analyserRessoucesModification() { if (count($this->ressources) == 1) { if ($this->methode == 'modifier' && $this->etreRessourceIdentifiant(0)) { // http://localhost/service:del:0.1/observations/#idObs $this->sousServiceNom = 'observation-details'; } } else if (count($this->ressources) == 3) { if ($this->etreRessourceIdentifiant(0) && $this->etreRessourceIdentifiant(1) && $this->verifierRessourceValeur(2, 'vote')) { // http://localhost/service:del:0.1/observations/#idObs/#idProposition/vote/ $this->sousServiceNom = 'vote-observation'; } } if ($this->sousServiceNom == null) { $this->lancerMessageErreurRessource(); } } private function etreRessourceIdentifiant($num) { $presenceId = false; if (isset($this->ressources[$num]) && is_numeric($this->ressources[$num])) { $presenceId = true; } return $presenceId; } private function verifierRessourceValeur($num, $valeur) { $ok = false; if (isset($this->ressources[$num]) && $this->ressources[$num] == $valeur) { $ok = true; } return $ok; } private function verifierParametreValeur($cle, $valeur) { $ok = false; if (isset($this->parametres[$cle]) && $this->ressources[$cle] == $valeur) { $ok = true; } return $ok; } private function lancerMessageErreurRessource() { $ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources); $message = "La ressource demandée '$ressource' ". "n'est pas disponible pour le service ".$this->serviceNom." !\n". "Les URLs disponibles sont : \n". " - en GET : observations, observations/#idObs/#idProposition/vote \n". " - en POST : observations/#id, observations/#idObs/#idProposition/vote \n". " - en PUT : observations/#idObs/#idProposition/vote \n"; $code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE; throw new Exception($message, $code); } private function initialiserService() { $classe = $this->obtenirNomClasseService($this->sousServiceNom); $chemins = array(); $chemins[] = $this->cheminCourant.$this->serviceNom.DS.$classe.'.php'; $chemins[] = $this->cheminCourant.'commun'.DS.$classe.'.php'; $retour = ''; $service = null; foreach ($chemins as $chemin) { if (file_exists($chemin)) { require_once $chemin; $service = new $classe($this->conteneur); if ($this->methode == 'consulter') { $retour = $service->consulter($this->ressources, $this->parametres); } elseif ($this->methode == 'ajouter') { $retour = $service->ajouter($this->ressources, $this->parametres); } elseif ($this->methode == 'modifier') { $retour = $service->modifier($this->ressources, $this->parametres); } else { $message = "Le sous-service '{$this->sousServiceNom}' du service '{$this->serviceNom}' ". "ne possède pas de méthode '{$this->methode}' !"; $code = RestServeur::HTTP_NON_IMPLEMENTE; throw new Exception($message, $code); } } } if (is_null($service)) { $ressource = $this->sousServiceNom.'/'.implode('/', $this->ressources); $message = "Le classe '$classe' correspondant à la ressource '$ressource' ". "n'existe pas dans le service '{$this->serviceNom}' !"; $code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE; throw new Exception($message, $code); } return $retour; } private function obtenirNomClasseService($mot) { $classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot)))); return $classeNom; } }