Location: PHPKode > scripts > Walker > walker.class.php
<?php

    /**
     * Walker
     * 
     * Copyright (c) 2010 BarsMaster
     * e-mail: hide@address.com, hide@address.com
     * 
     * Permission is hereby granted, free of charge, to any person obtaining a copy
     * of this software and associated documentation files (the "Software"), to deal
     * in the Software without restriction, including without limitation the rights
     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     * copies of the Software, and to permit persons to whom the Software is
     * furnished to do so, subject to the following conditions:
     * 
     * The above copyright notice and this permission notice shall be included in
     * all copies or substantial portions of the Software.
     * 
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     * THE SOFTWARE.
     * 
     * @author BarsMaster
     * @copyright 2010
     * @version 1.0
     * @access public
     */
     
    abstract class Walker {

        private $_source;
        private $_output;

        private $_objects = false;

        private $_uniqueId;
        private $_parentUniqueId;

        public function __construct($cats, $uniqueIdName = 'id', $parentUniqueIdName = 'parentId') {
            list($el) = $cats;
            $this->_objects = (bool)is_object($el);
            $this->_source = $cats;
            $this->_uniqueId = $uniqueIdName;
            $this->_parentUniqueId = $parentUniqueIdName;
            return $this;
        }

        abstract protected function _element($element, $depth);
        
        protected function _beforeElements() {
            return '';
        }
        
        protected function _afterElements() {
            return '';
        }

        private function _walk($parentId = 0, $depth = 0) {
            $return = '';
            if ($this->_source) {
                foreach ($this->_source as $key => $el) {
                    if ($el[$this->_parentUniqueId] == $parentId) {
                        unset($this->_source[$key]);
                        $return .= $this->_element($el, $depth);
                        if (count($this->_source) > 0) {
                            $return .= $this->_walk($el[$this->_uniqueId], $depth + 1);
                        }
                    }
                }
            }
            return $return;
        }

        private function _walkO($parentId = 0, $depth = 0) {
            $return = '';
            if ($this->_source) {
                foreach ($this->_source as $key => $el) {
                    if ($el->{$this->_parentUniqueId} == $parentId) {
                        unset($this->_source[$key]);
                        $return .= $this->_element($el, $depth);
                        if (count($this->_source) > 0) {
                            $return .= $this->_walkO($el->{$this->_uniqueId}, $depth + 1);
                        }
                    }
                }
            }
            return $return;
        }

        public function result() {
            if (!$this->_output) {
                if ($this->_objects) {
                    $this->_output = $this->_walkO();
                } else {
                    $this->_output = $this->_walk();
                }
            }

            return $this->_beforeElements() . $this->_output . $this->_afterElements();
        }

        public function __toString() {
            return $this->result();
        }
    }

?>
Return current item: Walker