ZipArchive implements countable, added ZipArchive::count() method
authorRemi Collet <remi@php.net>
Mon, 10 Jul 2017 09:31:55 +0000 (11:31 +0200)
committerRemi Collet <remi@php.net>
Mon, 10 Jul 2017 09:31:55 +0000 (11:31 +0200)
NEWS
UPGRADING
ext/zip/php_zip.c
ext/zip/php_zip.h
ext/zip/tests/oo_count.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index d9bd26e0bdee8f1d07409459619ca59765ed5c77..5cb625bc66bad3c75fa7ebe5069e54b2ed6673e8 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -49,6 +49,9 @@ PHP                                                                        NEWS
   . Fixed bug #74883 (SQLite3::__construct() produces "out of memory" exception
     with invalid flags). (Anatol)
 
+- ZIP:
+  . ZipArchive implements countable, added ZipArchive::count() method. (Remi)
+
 06 Jul 2017, PHP 7.2.0alpha3
 
 - Core:
index 49d499b881fc9768cef5c2ee8bdf8eac01567921..d0d2081623e5e5b709d6dd453667f26fdc449a1b 100644 (file)
--- a/UPGRADING
+++ b/UPGRADING
@@ -143,6 +143,8 @@ PHP 7.2 UPGRADE NOTES
       ZipArchive::EM_AES_192
       ZipArchive::EM_AES_256
   . accept 'password' from zip stream context
+  . ZipArchive implements countable, added ZipArchive::count() method.
+
 
 ========================================
 3. Changes in SAPI modules
index 394819cf341af53d4afba9436f0286c4f54426ee..2353519be4bd7b2c9780834db0d048ad63fb8803 100644 (file)
 #include "ext/standard/php_string.h"
 #include "ext/pcre/php_pcre.h"
 #include "ext/standard/php_filestat.h"
+#if PHP_VERSION_ID >= 70200
+#include "zend_interfaces.h"
+#elif defined(HAVE_SPL)
+#include "ext/spl/spl_iterators.h"
+#endif
 #include "php_zip.h"
 
 /* zip_open is a macro for renaming libzip zipopen, so we need to use PHP_NAMED_FUNCTION */
@@ -1548,6 +1553,23 @@ static ZIPARCHIVE_METHOD(close)
 }
 /* }}} */
 
+/* {{{ proto bool ZipArchive::count()
+close the zip archive */
+static ZIPARCHIVE_METHOD(count)
+{
+       struct zip *intern;
+       zval *self = getThis();
+
+       if (!self) {
+               RETURN_FALSE;
+       }
+
+       ZIP_FROM_OBJECT(intern, self);
+
+       RETVAL_LONG(zip_get_num_files(intern));
+}
+/* }}} */
+
 /* {{{ proto string ZipArchive::getStatusString()
  * Returns the status error message, system and/or zip messages */
 static ZIPARCHIVE_METHOD(getStatusString)
@@ -3069,6 +3091,7 @@ static const zend_function_entry zip_class_functions[] = {
        ZIPARCHIVE_ME(open,                                     arginfo_ziparchive_open, ZEND_ACC_PUBLIC)
        ZIPARCHIVE_ME(setPassword,                      arginfo_ziparchive_setpassword, ZEND_ACC_PUBLIC)
        ZIPARCHIVE_ME(close,                            arginfo_ziparchive__void, ZEND_ACC_PUBLIC)
+       ZIPARCHIVE_ME(count,                            arginfo_ziparchive__void, ZEND_ACC_PUBLIC)
        ZIPARCHIVE_ME(getStatusString,          arginfo_ziparchive__void, ZEND_ACC_PUBLIC)
        ZIPARCHIVE_ME(addEmptyDir,                      arginfo_ziparchive_addemptydir, ZEND_ACC_PUBLIC)
        ZIPARCHIVE_ME(addFromString,            arginfo_ziparchive_addfromstring, ZEND_ACC_PUBLIC)
@@ -3141,6 +3164,11 @@ static PHP_MINIT_FUNCTION(zip)
        php_zip_register_prop_handler(&zip_prop_handlers, "numFiles",  php_zip_get_num_files, NULL, NULL, IS_LONG);
        php_zip_register_prop_handler(&zip_prop_handlers, "filename", NULL, NULL, php_zipobj_get_filename, IS_STRING);
        php_zip_register_prop_handler(&zip_prop_handlers, "comment", NULL, php_zipobj_get_zip_comment, NULL, IS_STRING);
+#if PHP_VERSION_ID >= 70200
+       zend_class_implements(zip_class_entry, 1, zend_ce_countable);
+#elif defined(HAVE_SPL)
+       zend_class_implements(zip_class_entry, 1, spl_ce_Countable);
+#endif
 
        REGISTER_ZIP_CLASS_CONST_LONG("CREATE", ZIP_CREATE);
        REGISTER_ZIP_CLASS_CONST_LONG("EXCL", ZIP_EXCL);
index 123c064a33086c1470fd305bc08c8c4e27d2fb0e..d874c8f691c2f2a693e388e9d155ec3fbdef5312 100644 (file)
@@ -37,7 +37,7 @@ extern zend_module_entry zip_module_entry;
 #define ZIP_OVERWRITE ZIP_TRUNCATE
 #endif
 
-#define PHP_ZIP_VERSION "1.14.0"
+#define PHP_ZIP_VERSION "1.15.0"
 
 #define ZIP_OPENBASEDIR_CHECKPATH(filename) php_check_open_basedir(filename)
 
diff --git a/ext/zip/tests/oo_count.phpt b/ext/zip/tests/oo_count.phpt
new file mode 100644 (file)
index 0000000..f6d1613
--- /dev/null
@@ -0,0 +1,26 @@
+--TEST--
+ziparchive::count()
+--SKIPIF--
+<?php
+/* $Id$ */
+if(!extension_loaded('zip')) die('skip');
+?>
+--FILE--
+<?php
+
+$dirname = dirname(__FILE__) . '/';
+$file = $dirname . 'test.zip';
+
+$zip = new ZipArchive;
+if (!$zip->open($file)) {
+       exit('failed');
+}
+
+var_dump($zip->numFiles, count($zip), $zip->numFiles == count($zip));
+?>
+Done
+--EXPECTF--
+int(4)
+int(4)
+bool(true)
+Done