Skip to content
Snippets Groups Projects
Select Git revision
1 result Searching

arrayparser.php

Blame
  • arrayparser.php 4.78 KiB
    <?php
    
    /**
     * @author Robin Appelman
     * @copyright 2013 Robin Appelman icewind@owncloud.com
     *
     * This library is free software; you can redistribute it and/or
     * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
     * License as published by the Free Software Foundation; either
     * version 3 of the License, or any later version.
     *
     * This library is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
     *
     * You should have received a copy of the GNU Affero General Public
     * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
     *
     */
    
    namespace OC;
    
    class SyntaxException extends \Exception {
    }
    
    class ArrayParser {
    	const TYPE_NUM = 1;
    	const TYPE_BOOL = 2;
    	const TYPE_STRING = 3;
    	const TYPE_ARRAY = 4;
    
    	function parsePHP($string) {
    		$string = $this->stripPHPTags($string);
    		$string = $this->stripAssignAndReturn($string);
    		return $this->parse($string);
    	}
    
    	function stripPHPTags($string) {
    		$string = trim($string);
    		if (substr($string, 0, 5) === '<?php') {
    			$string = substr($string, 5);
    		}
    		if (substr($string, -2) === '?>') {
    			$string = substr($string, 0, -2);
    		}
    		return $string;
    	}
    
    	function stripAssignAndReturn($string) {
    		$string = trim($string);
    		if (substr($string, 0, 6) === 'return') {
    			$string = substr($string, 6);
    		}
    		if (substr($string, 0, 1) === '$') {
    			list(, $string) = explode('=', $string, 2);
    		}
    		return $string;
    	}
    
    	function parse($string) {
    		$string = trim($string);
    		$string = trim($string, ';');
    		switch ($this->getType($string)) {
    			case self::TYPE_NUM:
    				return $this->parseNum($string);
    			case self::TYPE_BOOL:
    				return $this->parseBool($string);
    			case self::TYPE_STRING:
    				return $this->parseString($string);