Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 311 and 401] [Versions 400 and 401]

   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(): void
 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 mixed current Item
 189     * @uses $iteratorPosition identify current key
 190     */
 191     #[\ReturnTypeWillChange]
 192      public function current()
 193      {
 194          return $this->value[$this->iteratorPosition];
 195      }
 196  
 197    /**
 198     * Get Iterator's current key identified by {@link $iteratorPosition}
 199     * @link http://php.net/manual/en/iterator.key.php
 200     * @return mixed key of the current Item: mixed
 201     * @uses $iteratorPosition identify current key
 202     */
 203      #[\ReturnTypeWillChange]
 204      public function key()
 205      {
 206          return $this->iteratorPosition;
 207      }
 208  
 209    /**
 210     * Increment {@link $iteratorPosition} to address next {@see CFType}
 211     * @link http://php.net/manual/en/iterator.next.php
 212     * @return void
 213     * @uses $iteratorPosition increment by 1
 214     */
 215      public function next(): void
 216      {
 217          $this->iteratorPosition++;
 218      }
 219  
 220    /**
 221     * Test if {@link $iteratorPosition} addresses a valid element of {@link $value}
 222     * @link http://php.net/manual/en/iterator.valid.php
 223     * @return bool true if current position is valid, false else
 224     * @uses $iteratorPosition test if within {@link $iteratorKeys}
 225     * @uses $iteratorPosition test if within {@link $value}
 226     */
 227      public function valid(): bool
 228      {
 229          return isset($this->value[$this->iteratorPosition]);
 230      }
 231  
 232    /************************************************************************************************
 233     *    ArrayAccess   I N T E R F A C E
 234     ************************************************************************************************/
 235  
 236    /**
 237     * Determine if the array's key exists
 238     * @param string $offset the key to check
 239     * @return bool true if the offset exists, false if not
 240     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
 241     * @uses $value to check if $key exists
 242     * @author Sean Coates <sean@php.net>
 243     */
 244      public function offsetExists($offset): bool
 245      {
 246          return isset($this->value[$offset]);
 247      }
 248  
 249    /**
 250     * Fetch a specific key from the CFArray
 251     * @param mixed $offset the key to check
 252     * @return mixed the value associated with the key; null if the key is not found
 253     * @link http://php.net/manual/en/arrayaccess.offsetget.php
 254     * @uses get() to get the key's value
 255     * @author Sean Coates <sean@php.net>
 256     */
 257      #[\ReturnTypeWillChange]
 258      public function offsetGet($offset)
 259      {
 260          return $this->get($offset);
 261      }
 262  
 263    /**
 264     * Set a value in the array
 265     * @param mixed $offset the key to set
 266     * @param mixed $value the value to set
 267     * @return void
 268     * @link http://php.net/manual/en/arrayaccess.offsetset.php
 269     * @uses setValue() to set the key's new value
 270     * @author Sean Coates <sean@php.net>
 271     */
 272      public function offsetSet($offset, $value): void
 273      {
 274          $this->setValue($value);
 275      }
 276  
 277    /**
 278     * Unsets a value in the array
 279     * <b>Note:</b> this dummy does nothing
 280     * @param mixed $offset the key to set
 281     * @return void
 282     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
 283     * @author Sean Coates <sean@php.net>
 284     */
 285      public function offsetUnset($offset): void
 286      {
 287      }
 288  }