ArgumentParser.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2009 by Denis M. Gabaidulin                             *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00012     final class ArgumentParser extends Singleton
00013     {
00014         private $form = null;
00015         private $result = null;
00016         
00020         public static function me()
00021         {
00022             return Singleton::getInstance(__CLASS__);
00023         }
00024         
00028         public function setForm(Form $form)
00029         {
00030             $this->form = $form;
00031             
00032             return $this;
00033         }
00034         
00038         public function getForm()
00039         {
00040             return $this->form;
00041         }
00042         
00046         public function parse()
00047         {
00048             Assert::isNotNull($this->form);
00049             
00050             $long = FormToArgumentsConverter::getLong($this->form);
00051             
00052             // NOTE: stupid php, see man about long params
00053             if (empty($long))
00054                 $this->result = getopt(
00055                     FormToArgumentsConverter::getShort($this->form)
00056                 );
00057             else
00058                 $this->result = getopt(
00059                     FormToArgumentsConverter::getShort($this->form),
00060                     $long
00061                 );
00062             
00063             return $this;
00064         }
00065         
00069         public function validate()
00070         {
00071             Assert::isNotNull($this->result);
00072             
00073             $this->form->import($this->result);
00074             
00075             if ($errors = $this->form->getErrors())
00076                 throw new WrongArgumentException(
00077                     "\nArguments wrong:\n"
00078                     .print_r($errors, true)
00079                 );
00080             
00081             return $this;
00082         }
00083     }
00084 ?>