Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

   1  <?php
   2  /**
   3   * LICENSE
   4   *
   5   * This file is part of CFPropertyList.
   6   *
   7   * The PHP implementation of Apple's PropertyList can handle XML PropertyLists
   8   * as well as binary PropertyLists. It offers functionality to easily convert
   9   * data between worlds, e.g. recalculating timestamps from unix epoch to apple
  10   * epoch and vice versa. A feature to automagically create (guess) the plist
  11   * structure from a normal PHP data structure will help you dump your data to
  12   * plist in no time.
  13   *
  14   * Copyright (c) 2018 Teclib'
  15   *
  16   * Permission is hereby granted, free of charge, to any person obtaining a copy
  17   * of this software and associated documentation files (the "Software"), to deal
  18   * in the Software without restriction, including without limitation the rights
  19   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  20   * copies of the Software, and to permit persons to whom the Software is
  21   * furnished to do so, subject to the following conditions:
  22   *
  23   * The above copyright notice and this permission notice shall be included in all
  24   * copies or substantial portions of the Software.
  25   *
  26   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  29   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  30   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  31   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32   * SOFTWARE.
  33   *
  34   * ------------------------------------------------------------------------------
  35   * @author    Rodney Rehm <rodney.rehm@medialize.de>
  36   * @author    Christian Kruse <cjk@wwwtech.de>
  37   * @copyright Copyright © 2018 Teclib
  38   * @package   plist
  39   * @license   MIT
  40   * @link      https://github.com/TECLIB/CFPropertyList/
  41   * @link      http://developer.apple.com/documentation/Darwin/Reference/ManPages/man5/plist.5.html Property Lists
  42   * ------------------------------------------------------------------------------
  43   */
  44  
  45  namespace CFPropertyList;
  46  
  47  use \DOMDocument;
  48  use \Iterator;
  49  use \ArrayAccess;
  50  
  51  /**
  52   * Array Type of CFPropertyList
  53   */
  54  class CFArray extends CFType implements Iterator, ArrayAccess
  55  {
  56    /**
  57     * Position of iterator {@link http://php.net/manual/en/class.iterator.php}
  58     * @var integer
  59     */
  60      protected $iteratorPosition = 0;
  61  
  62  
  63    /**
  64     * Create new CFType.
  65     * @param array $value Value of CFType
  66     */
  67      public function __construct($value = array())
  68      {
  69          $this->value = $value;
  70      }
  71  
  72    /**
  73     * Set the CFType's value
  74     * <b>Note:</b> this dummy does nothing
  75     * @return void
  76     */
  77      public function setValue($value)
  78      {
  79      }
  80  
  81    /**
  82     * Add CFType to collection.
  83     * @param CFType $value CFType to add to collection, defaults to null which results in an empty {@link CFString}
  84     * @return void
  85     * @uses $value for adding $value
  86     */
  87      public function add(CFType $value = null)
  88      {
  89        // anything but CFType is null, null is an empty string - sad but true
  90          if (!$value) {
  91              $value = new CFString();
  92          }
  93  
  94          $this->value[] = $value;
  95      }
  96  
  97    /**
  98     * Get CFType from collection.
  99     * @param integer $key Key of CFType to retrieve from collection
 100     * @return CFType CFType found at $key, null else
 101     * @uses $value for retrieving CFType of $key
 102     */
 103      public function get($key)
 104      {
 105          if (isset($this->value[$key])) {
 106              return $this->value[$key];
 107          }
 108          return null;
 109      }
 110  
 111    /**
 112     * Remove CFType from collection.
 113     * @param integer $key Key of CFType to removes from collection
 114     * @return CFType removed CFType, null else
 115     * @uses $value for removing CFType of $key
 116     */
 117      public function del($key)
 118      {
 119          if (isset($this->value[$key])) {
 120              unset($this->value[$key]);
 121          }
 122      }
 123  
 124  
 125    /************************************************************************************************
 126     *    S E R I A L I Z I N G
 127     ************************************************************************************************/
 128  
 129    /**
 130     * Get XML-Node.
 131     * @param DOMDocument $doc DOMDocument to create DOMNode in
 132     * @param string $nodeName For compatibility reasons; just ignore it
 133     * @return DOMNode &lt;array&gt;-Element
 134     */
 135      public function toXML(DOMDocument $doc, $nodeName = "")
 136      {
 137          $node = $doc->createElement('array');
 138  
 139          foreach ($this->value as $value) {
 140              $node->appendChild($value->toXML($doc));
 141          }
 142          return $node;
 143      }
 144  
 145    /**
 146     * convert value to binary representation
 147     * @param CFBinaryPropertyList The binary property list object
 148     * @return The offset in the object table
 149     */
 150      public function toBinary(CFBinaryPropertyList &$bplist)
 151      {
 152          return $bplist->arrayToBinary($this);
 153      }
 154  
 155    /**
 156     * Get CFType's value.
 157     * @return array primitive value
 158     * @uses $value for retrieving primitive of CFType
 159     */
 160      public function toArray()
 161      {
 162          $a = array();
 163          foreach ($this->value as $value) {
 164              $a[] = $value->toArray();
 165          }
 166          return $a;
 167      }
 168  
 169  
 170    /************************************************************************************************
 171     *    I T E R A T O R   I N T E R F A C E
 172     ************************************************************************************************/
 173  
 174    /**
 175     * Rewind {@link $iteratorPosition} to first position (being 0)
 176     * @link http://php.net/manual/en/iterator.rewind.php
 177     * @return void
 178     * @uses $iteratorPosition set to 0
 179     */
 180      public function rewind()
 181      {
 182          $this->iteratorPosition = 0;
 183      }
 184  
 185    /**
 186     * Get Iterator's current {@link CFType} identified by {@link $iteratorPosition}
 187     * @link http://php.net/manual/en/iterator.current.php
 188     * @return CFType current Item
 189     * @uses $iteratorPosition identify current key
 190     */
 191      public function current()
 192      {
 193          return $this->value[$this->iteratorPosition];
 194      }
 195  
 196    /**
 197     * Get Iterator's current key identified by {@link $iteratorPosition}
 198     * @link http://php.net/manual/en/iterator.key.php
 199     * @return string key of the current Item
 200     * @uses $iteratorPosition identify current key
 201     */
 202      public function key()
 203      {
 204          return $this->iteratorPosition;
 205      }
 206  
 207    /**
 208     * Increment {@link $iteratorPosition} to address next {@see CFType}
 209     * @link http://php.net/manual/en/iterator.next.php
 210     * @return void
 211     * @uses $iteratorPosition increment by 1
 212     */
 213      public function next()
 214      {
 215          $this->iteratorPosition++;
 216      }
 217  
 218    /**
 219     * Test if {@link $iteratorPosition} addresses a valid element of {@link $value}
 220     * @link http://php.net/manual/en/iterator.valid.php
 221     * @return boolean true if current position is valid, false else
 222     * @uses $iteratorPosition test if within {@link $iteratorKeys}
 223     * @uses $iteratorPosition test if within {@link $value}
 224     */
 225      public function valid()
 226      {
 227          return isset($this->value[$this->iteratorPosition]);
 228      }
 229  
 230    /************************************************************************************************
 231     *    ArrayAccess   I N T E R F A C E
 232     ************************************************************************************************/
 233  
 234    /**
 235     * Determine if the array's key exists
 236     * @param string $key the key to check
 237     * @return bool true if the offset exists, false if not
 238     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
 239     * @uses $value to check if $key exists
 240     * @author Sean Coates <sean@php.net>
 241     */
 242      public function offsetExists($key)
 243      {
 244          return isset($this->value[$key]);
 245      }
 246  
 247    /**
 248     * Fetch a specific key from the CFArray
 249     * @param string $key the key to check
 250     * @return mixed the value associated with the key; null if the key is not found
 251     * @link http://php.net/manual/en/arrayaccess.offsetget.php
 252     * @uses get() to get the key's value
 253     * @author Sean Coates <sean@php.net>
 254     */
 255      public function offsetGet($key)
 256      {
 257          return $this->get($key);
 258      }
 259  
 260    /**
 261     * Set a value in the array
 262     * @param string $key the key to set
 263     * @param string $value the value to set
 264     * @return void
 265     * @link http://php.net/manual/en/arrayaccess.offsetset.php
 266     * @uses setValue() to set the key's new value
 267     * @author Sean Coates <sean@php.net>
 268     */
 269      public function offsetSet($key, $value)
 270      {
 271          return $this->setValue($value);
 272      }
 273  
 274    /**
 275     * Unsets a value in the array
 276     * <b>Note:</b> this dummy does nothing
 277     * @param string $key the key to set
 278     * @return void
 279     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
 280     * @author Sean Coates <sean@php.net>
 281     */
 282      public function offsetUnset($key)
 283      {
 284      }
 285  }