當前位置: 首頁>>代碼示例>>PHP>>正文


PHP Diff::setStatus方法代碼示例

本文整理匯總了PHP中Diff::setStatus方法的典型用法代碼示例。如果您正苦於以下問題:PHP Diff::setStatus方法的具體用法?PHP Diff::setStatus怎麽用?PHP Diff::setStatus使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Diff的用法示例。


在下文中一共展示了Diff::setStatus方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: compare

 /**
  * Generate a diff beetween to variable
  *
  * @param mixed  $old    An object
  * @param mixed  $new    An object
  * @param string $identifier   A name for the variable
  * @param string $status Force a status
  *
  * @return Diff
  */
 public function compare($old, $new, $identifier = null)
 {
     $diff = new Diff($old, $new, $identifier);
     if (null === $new) {
         if (null === $old) {
             //same type, same value
             //nothing to do
         } else {
             //new variable is null but not the old
             $diff->setStatus(Diff::STATUS_DELETED);
         }
     } else {
         if (is_object($new)) {
             if (null === $old) {
                 //the new variable is an object but the old is null
                 $diff->setStatus(Diff::STATUS_CREATED);
             } else {
                 if (is_object($old)) {
                     //the both variables are objects
                     $reflectionOld = new \ReflectionClass($old);
                     $reflectionNew = new \ReflectionClass($new);
                     if ($reflectionNew->getName() !== $reflectionOld->getName()) {
                         //not the same class
                         $diff->setStatus(Diff::STATUS_TYPE_CHANGED);
                     } else {
                         //same class
                         //check if a compare closure exists
                         if (isset($this->compares[$reflectionNew->getName()])) {
                             $this->compares[$reflectionNew->getName()]($diff);
                         } else {
                             //this is a map of properties to check when comparing
                             $map = array_merge($this->buildMap($reflectionOld), $this->buildMap($reflectionNew));
                             $done = array();
                             //parse new object
                             if (isset($map[$reflectionNew->getName()])) {
                                 foreach ($map[$reflectionNew->getName()] as $propertyName) {
                                     if (isset($this->excludes[$reflectionNew->getName()])) {
                                         if (in_array($propertyName, $this->excludes[$reflectionNew->getName()])) {
                                             //be sure to not do the test again
                                             unset($map[$reflectionNew->getName()][$propertyName]);
                                             continue;
                                         }
                                     }
                                     $property = $reflectionNew->getProperty($propertyName);
                                     $property->setAccessible(true);
                                     if ($reflectionOld->hasProperty($propertyName)) {
                                         $oldProperty = $reflectionOld->getProperty($propertyName);
                                         $oldProperty->setAccessible(true);
                                         $subdiff = $this->compare($oldProperty->getValue($old), $property->getValue($new), $propertyName);
                                         if ($subdiff->isModified()) {
                                             $diff->setStatus(Diff::STATUS_MODIFIED);
                                         }
                                     } else {
                                         $subdiff = $this->compare(null, $property->getValue($new), $propertyName);
                                         $subdiff->setStatus(Diff::STATUS_CREATED);
                                         $diff->setStatus(Diff::STATUS_MODIFIED);
                                     }
                                     $diff->addProperty($propertyName, $subdiff);
                                     $done[$propertyName] = true;
                                 }
                             }
                         }
                     }
                 } else {
                     //array or scalar variable (integer, boolean, string)
                     $diff->setStatus(Diff::STATUS_TYPE_CHANGED);
                 }
             }
         } else {
             if (is_array($new)) {
                 if (null === $old) {
                     //the new variable is an array but the old is null
                     $diff->setStatus(Diff::STATUS_CREATED);
                 } else {
                     if (is_object($old)) {
                         //the new variable is an array but the old is an object
                         $diff->setStatus(Diff::STATUS_TYPE_CHANGED);
                     } else {
                         if (is_array($old)) {
                             //the both variables are arrays
                             $done = array();
                             //parse new array
                             foreach ($new as $key => $value) {
                                 if (isset($old[$key])) {
                                     //an old value exists
                                     $subdiff = $this->compare($old[$key], $value, $key);
                                     if ($subdiff->isModified() || $subdiff->isCreated() || $subdiff->isDeleted()) {
                                         $diff->setStatus(Diff::STATUS_MODIFIED);
                                     }
                                 } else {
//.........這裏部分代碼省略.........
開發者ID:pitpit,項目名稱:php-diff,代碼行數:101,代碼來源:DiffEngine.php


注:本文中的Diff::setStatus方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。