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 CFDictionary extends CFType implements Iterator
  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      * List of Keys for numerical iterator access {@link http://php.net/manual/en/class.iterator.php}
  63      * @var array
  64      */
  65      protected $iteratorKeys = null;
  66     /**
  67      * Create new CFType.
  68      * @param array $value Value of CFType
  69      */
  70      public function __construct($value = array())
  71      {
  72          $this->value = $value;
  73      }
  74     /**
  75      * Set the CFType's value
  76      * <b>Note:</b> this dummy does nothing
  77      * @return void
  78      */
  79      public function setValue($value)
  80      {
  81      }
  82     /**
  83      * Add CFType to collection.
  84      * @param string $key Key to add to collection
  85      * @param CFType $value CFType to add to collection, defaults to null which results in an empty {@link CFString}
  86      * @return void
  87      * @uses $value for adding $key $value pair
  88      */
  89      public function add($key, CFType $value = null)
  90      {
  91        // anything but CFType is null, null is an empty string - sad but true
  92          if (!$value) {
  93              $value = new CFString();
  94          }
  95          $this->value[$key] = $value;
  96      }
  97     /**
  98      * Get CFType from collection.
  99      * @param string $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      * Generic getter (magic)
 112      * @param integer $key Key of CFType to retrieve from collection
 113      * @return CFType CFType found at $key, null else
 114      * @link http://php.net/oop5.overloading
 115      * @uses get() to retrieve the key's value
 116      * @author Sean Coates <sean@php.net>
 117      */
 118      public function __get($key)
 119      {
 120          return $this->get($key);
 121      }
 122     /**
 123      * Remove CFType from collection.
 124      * @param string $key Key of CFType to removes from collection
 125      * @return CFType removed CFType, null else
 126      * @uses $value for removing CFType of $key
 127      */
 128      public function del($key)
 129      {
 130          if (isset($this->value[$key])) {
 131              unset($this->value[$key]);
 132          }
 133      }
 134     /************************************************************************************************
 135      *    S E R I A L I Z I N G
 136      ************************************************************************************************/
 137     /**
 138      * Get XML-Node.
 139      * @param DOMDocument $doc DOMDocument to create DOMNode in
 140      * @param string $nodeName For compatibility reasons; just ignore it
 141      * @return DOMNode &lt;dict&gt;-Element
 142      */
 143      public function toXML(DOMDocument $doc, $nodeName = "")
 144      {
 145          $node = $doc->createElement('dict');
 146          foreach ($this->value as $key => $value) {
 147              $node->appendChild($doc->createElement('key', $key));
 148              $node->appendChild($value->toXML($doc));
 149          }
 150          return $node;
 151      }
 152     /**
 153      * convert value to binary representation
 154      * @param CFBinaryPropertyList The binary property list object
 155      * @return The offset in the object table
 156      */
 157      public function toBinary(CFBinaryPropertyList &$bplist)
 158      {
 159          return $bplist->dictToBinary($this);
 160      }
 161     /**
 162      * Get CFType's value.
 163      * @return array primitive value
 164      * @uses $value for retrieving primitive of CFType
 165      */
 166      public function toArray()
 167      {
 168          $a = array();
 169          foreach ($this->value as $key => $value) {
 170              $a[$key] = $value->toArray();
 171          }
 172          return $a;
 173      }
 174     /************************************************************************************************
 175      *    I T E R A T O R   I N T E R F A C E
 176      ************************************************************************************************/
 177     /**
 178      * Rewind {@link $iteratorPosition} to first position (being 0)
 179      * @link http://php.net/manual/en/iterator.rewind.php
 180      * @return void
 181      * @uses $iteratorPosition set to 0
 182      * @uses $iteratorKeys store keys of {@link $value}
 183      */
 184      public function rewind(): void
 185      {
 186          $this->iteratorPosition = 0;
 187          $this->iteratorKeys = array_keys($this->value);
 188      }
 189     /**
 190      * Get Iterator's current {@link CFType} identified by {@link $iteratorPosition}
 191      * @link http://php.net/manual/en/iterator.current.php
 192      * @return mixed current Item
 193      * @uses $iteratorPosition identify current key
 194      * @uses $iteratorKeys identify current value
 195      */
 196      #[\ReturnTypeWillChange]
 197      public function current()
 198      {
 199          return $this->value[$this->iteratorKeys[$this->iteratorPosition]];
 200      }
 201     /**
 202      * Get Iterator's current key identified by {@link $iteratorPosition}
 203      * @link http://php.net/manual/en/iterator.key.php
 204      * @return mixed key of the current Item
 205      * @uses $iteratorPosition identify current key
 206      * @uses $iteratorKeys identify current value
 207      */
 208      #[\ReturnTypeWillChange]
 209      public function key()
 210      {
 211          return $this->iteratorKeys[$this->iteratorPosition];
 212      }
 213     /**
 214      * Increment {@link $iteratorPosition} to address next {@see CFType}
 215      * @link http://php.net/manual/en/iterator.next.php
 216      * @return void
 217      * @uses $iteratorPosition increment by 1
 218      */
 219      public function next(): void
 220      {
 221          $this->iteratorPosition++;
 222      }
 223     /**
 224      * Test if {@link $iteratorPosition} addresses a valid element of {@link $value}
 225      * @link http://php.net/manual/en/iterator.valid.php
 226      * @return bool true if current position is valid, false else
 227      * @uses $iteratorPosition test if within {@link $iteratorKeys}
 228      * @uses $iteratorPosition test if within {@link $value}
 229      */
 230      public function valid(): bool
 231      {
 232          return isset($this->iteratorKeys[$this->iteratorPosition]) && isset($this->value[$this->iteratorKeys[$this->iteratorPosition]]);
 233      }
 234  }