0

我读到 foreach 适用于数组或任何实现 Traversable 接口的对象。

由于 Traversable 只是一个抽象,我使用了 Iterator。

class EPubHub_PageCollection implements Iterator
{
    protected $index0 = 0;
    protected $index = 1;
    protected $pages = array();

    public function __construct()
    {
        $this->index0 = 0;
        $this->index = 1;
        $this->pages = array();
    }

    public function updateIndex()
    {
        $this->index = $this->index0 + 1;
    }

    public function rewind()
    {
        $this->index0 = 0;
        $this->updateIndex();
    }

    public function current()
    {
        return $this->pages[$this->index0];
    }

    public function key() 
    {
        return $this->index0;
    }

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

    public function next() 
    {
        ++$this->index0;
        $this->updateIndex();
    }

    public function valid() 
    {
        return isset($this->pages[$this->index0]);
    }

    public function length()
    {
        return count($this->pages);
    }

    public function prefixZero($index, $lengthOfKey = 3)
    {
        return str_pad($index, $lengthOfKey, '0', STR_PAD_LEFT);
    }

    public function getCurrentPageNumber()
    {
        $pageNumber = $this->prefixZero($this->index);
        return $pageNumber;
    }

    public function getCurrentPageFilename($pagenumber = '')
    {
        if ($pagenumber === '')
        {
            $pagenumber = $this->getCurrentPageNumber();
        }
        $pageFileName = 'page' . $pagenumber . '.xhtml' ;
        return $pageFileName;
    }

    public function getNth($index)
    {
        return $this->pages[$index - 1];
    }

    public function getCurrentPageItemId($pagenumber = '')
    {
        if ($pagenumber === '')
        {
            $pagenumber = $this->getCurrentPageNumber();
        }
        $pageItemId = 'pg' . $pagenumber;
        return $pageItemId;
    }

    public function add(EPubHub_PageInterface $page, $index = null)
    {
        if ($index === null) {
            // we just append to the pages from the end directly
            $this->pages[] = $page;
        } else {
            $this->pages = array_splice( $this->pages, $index - 1, 0, $page);
        }
    }

    /**
     *
     * delete either by page or by index
     * @param mixed $pageOrIndex If $pageOrIndex is instance of EPubHub_PageInterface
     * then delete by value. Else if integer delete by index
     */
    public function delete($pageOrIndex)
    {

        if ($pageOrIndex instanceof EPubHub_PageInterface)
        {
            $page = $pageOrIndex;
            if(($key = array_search($page, $this->pages)) !== false)
            {
                unset($this->pages[$key]);
            }
        } 

        if (is_numeric($pageOrIndex)) 
        {
            $key = $pageOrIndex;

            if((array_key_exists($key - 1, $this->pages)))
            {
                unset($this->pages[$key - 1]);
            }

        }

    }

    public function getImages()
    {
        $images = new EPubHub_ImageCollection();

        foreach($this->pages as $page)
        {
            $image = $page->getImage();
            $images->add($image);
        }
        return $images;
    }

    public function getPages()
    {
        return $this->pages;
    }


}

这个没用。

我想明白为什么。我尝试运行转储(),但实例上的转储破坏了一些东西。

目前,我的解决方法是使用此类的内部数组。

我问这个问题是为了满足我的好奇心。

4

1 回答 1

0

您需要配置调试:

services:
    acme_hello.twig.extension.debug:
        class:        Twig_Extension_Debug
        tags:
            - { name: 'twig.extension' }

看这里:调试。而且:for 循环在 twig 中

例如:

{{ dump(users) }}

{% for user in users %}
    {{ user.username }}
{% endfor %}
于 2013-03-20T13:46:13.527 回答