|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace LDAPi; |
| 4 | + |
| 5 | +use ArrayObject; |
| 6 | + |
| 7 | +/** |
| 8 | + * Class Modification |
| 9 | + * @package LDAPi |
| 10 | + * @property string $attributeName |
| 11 | + * @property int $operation |
| 12 | + * @property \ArrayObject|array $data |
| 13 | + * @property mixed $value |
| 14 | + */ |
| 15 | +class Modification |
| 16 | +{ |
| 17 | + const OP_ADD = LDAP_MODIFY_BATCH_ADD; |
| 18 | + const OP_REMOVE = LDAP_MODIFY_BATCH_REMOVE; |
| 19 | + const OP_REMOVE_ALL = LDAP_MODIFY_BATCH_REMOVE_ALL; |
| 20 | + const OP_REPLACE = LDAP_MODIFY_BATCH_REPLACE; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var array |
| 24 | + */ |
| 25 | + private $data = [ |
| 26 | + 'attributeName' => null, |
| 27 | + 'operation' => null, |
| 28 | + 'values' => null, |
| 29 | + ]; |
| 30 | + |
| 31 | + public function __construct() |
| 32 | + { |
| 33 | + $this->data['values'] = new ArrayObject; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @param string $name |
| 38 | + * @param mixed $value |
| 39 | + */ |
| 40 | + public function __set($name, $value) |
| 41 | + { |
| 42 | + switch ($name) { |
| 43 | + case 'attributeName': |
| 44 | + $this->data['attributeName'] = (string)$value; |
| 45 | + break; |
| 46 | + |
| 47 | + case 'operation': |
| 48 | + if (!in_array($value, [self::OP_ADD, self::OP_REMOVE, self::OP_REMOVE_ALL, self::OP_REPLACE])) { |
| 49 | + throw new InvalidModeException('Operation must be one of the Modification::OP_* constants'); |
| 50 | + } |
| 51 | + |
| 52 | + $this->data['operation'] = (int)$value; |
| 53 | + |
| 54 | + if ($this->data['operation'] === self::OP_REMOVE_ALL) { |
| 55 | + $this->data['values'] = new ArrayObject; |
| 56 | + } |
| 57 | + break; |
| 58 | + |
| 59 | + case 'values': |
| 60 | + if ($this->data['operation'] === self::OP_REMOVE_ALL) { |
| 61 | + throw new InvalidModeException('REMOVE_ALL operations cannot include a value set'); |
| 62 | + } |
| 63 | + |
| 64 | + if ($value instanceof ArrayObject) { |
| 65 | + $this->data['values'] = $value; |
| 66 | + } else if (is_array($value)) { |
| 67 | + $this->data['values'] = new ArrayObject($value); |
| 68 | + } else { |
| 69 | + throw new InvalidValueSetException('Value set must be specified as an array or an ArrayObject'); |
| 70 | + } |
| 71 | + break; |
| 72 | + |
| 73 | + default: |
| 74 | + throw new NonExistentPropertyException('Property ' . $name . ' not defined for ' . get_class($this)); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @param string $name |
| 80 | + * @return mixed |
| 81 | + */ |
| 82 | + public function __get($name) |
| 83 | + { |
| 84 | + if (!array_key_exists($name, $this->data)) { |
| 85 | + throw new NonExistentPropertyException('Property ' . $name . ' not defined for ' . get_class($this)); |
| 86 | + } |
| 87 | + |
| 88 | + return $this->data[$name]; |
| 89 | + } |
| 90 | +} |
0 commit comments