Ejemplo n.º 1
0
 /**
  * @see Patcher::patch
  *
  * Applies the provided diff to the provided array and returns the result.
  * The provided diff needs to be non-associative. In other words, calling
  * isAssociative on it should return false.
  *
  * Note that remove operations can introduce gaps into the input array $base.
  * For instance, when the input is [ 0 => 'a', 1 => 'b', 2 => 'c' ], and there
  * is one remove operation for 'b', the result will be [ 0 => 'a', 2 => 'c' ].
  *
  * @since 0.4
  *
  * @param array $base
  * @param Diff $diff
  *
  * @return array
  * @throws PatcherException
  */
 public function patch(array $base, Diff $diff)
 {
     if ($diff->looksAssociative()) {
         $this->handleError('ListPatcher can only patch using non-associative diffs');
     }
     foreach ($diff as $diffOp) {
         if ($diffOp instanceof DiffOpAdd) {
             $base[] = $diffOp->getNewValue();
         } elseif ($diffOp instanceof DiffOpRemove) {
             $key = array_search($diffOp->getOldValue(), $base, true);
             if ($key === false) {
                 $this->handleError('Cannot remove an element from a list if it is not present');
                 continue;
             }
             unset($base[$key]);
         }
     }
     return $base;
 }
Ejemplo n.º 2
0
 /**
  * @dataProvider looksAssociativeProvider
  */
 public function testLooksAssociative(Diff $diff, $looksAssoc)
 {
     $this->assertEquals($looksAssoc, $diff->looksAssociative());
     if (!$diff->looksAssociative()) {
         $this->assertFalse($diff->hasAssociativeOperations());
     }
 }
Ejemplo n.º 3
0
 private function patchMapOrList(array $base, Diff $diff)
 {
     if ($diff->looksAssociative()) {
         $base = $this->patch($base, $diff);
     } else {
         $base = $this->listPatcher->patch($base, $diff);
     }
     return $base;
 }