Adldap\Objects\BatchModification::build PHP Method

build() public method

Builds the type of modification automatically based on the current and original values.
public build ( ) : BatchModification
return BatchModification
    public function build()
    {
        $filtered = array_diff(array_map('trim', $this->values), ['']);
        if (is_null($this->original)) {
            // If the original value is null, we'll assume
            // that the attribute doesn't exist yet.
            if (!empty($filtered)) {
                // If the filtered array is not empty, we'll
                // assume the developer is looking to
                // add attributes to the model.
                $this->setType(LDAP_MODIFY_BATCH_ADD);
            }
            // If the filtered array is empty and there is no original
            // value, then we can ignore this attribute since
            // we can't push null values to AD.
        } else {
            if (empty($filtered)) {
                // If there's an original value and the array is
                // empty then we can assume the developer is
                // looking to completely remove all values
                // of the specified attribute.
                $this->setType(LDAP_MODIFY_BATCH_REMOVE_ALL);
            } else {
                // If the array isn't empty then we can assume the
                // developer is trying to replace all attributes.
                $this->setType(LDAP_MODIFY_BATCH_REPLACE);
            }
        }
        return $this;
    }

Usage Example

Example #1
0
 /**
  * Sets and returns the models modifications.
  *
  * @return array
  */
 public function getModifications()
 {
     $dirty = $this->getDirty();
     foreach ($dirty as $attribute => $values) {
         if (!is_array($values)) {
             // Make sure values is always an array.
             $values = [$values];
         }
         $modification = new BatchModification();
         if (array_key_exists($attribute, $this->original)) {
             $modification->setOriginal($this->original[$attribute]);
         }
         $modification->setAttribute($attribute);
         $modification->setValues($values);
         $modification->build();
         $this->addModification($modification);
     }
     return $this->modifications;
 }
All Usage Examples Of Adldap\Objects\BatchModification::build