diff --git a/en/appendices/3-5-migration-guide.rst b/en/appendices/3-5-migration-guide.rst index 0635f8bd01..f27bca12e1 100644 --- a/en/appendices/3-5-migration-guide.rst +++ b/en/appendices/3-5-migration-guide.rst @@ -175,6 +175,12 @@ cache configuration to fall back to if the engine is misconfigured (or unavailable). See :ref:`cache-configuration-fallback` for more information on configuring fallbacks. +Collection +---------- + +* ``Cake\Collection\Collection::avg()`` was added. +* ``Cake\Collection\Collection::median()`` was added. + Core ---- diff --git a/en/core-libraries/collections.rst b/en/core-libraries/collections.rst index e8cd107d00..7ea383bc95 100644 --- a/en/core-libraries/collections.rst +++ b/en/core-libraries/collections.rst @@ -53,27 +53,27 @@ List of Methods .. table:: :class: docutils internal-toc - +-----------------------+---------------------------+----------------------+---------------------+ - | :php:meth:`append` | :php:meth:`buffered` | :php:meth:`combine` | :php:meth:`compile` | - +-----------------------+---------------------------+----------------------+---------------------+ - | :php:meth:`contains` | :php:meth:`countBy` | :php:meth:`chunk` | :php:meth:`each` | - +-----------------------+---------------------------+----------------------+---------------------+ - | :php:meth:`every` | :php:meth:`extract` | :php:meth:`filter` | :php:meth:`first` | - +-----------------------+---------------------------+----------------------+---------------------+ - | :php:meth:`groupBy` | :php:meth:`indexBy` | :php:meth:`insert` | :php:meth:`isEmpty` | - +-----------------------+---------------------------+----------------------+---------------------+ - | :php:meth:`last` | :php:meth:`listNested` | :php:meth:`map` | :php:meth:`match` | - +-----------------------+---------------------------+----------------------+---------------------+ - | :php:meth:`max` | :php:meth:`min` | :php:meth:`nest` | :php:meth:`reduce` | - +-----------------------+---------------------------+----------------------+---------------------+ - | :php:meth:`reject` | :php:meth:`sample` | :php:meth:`shuffle` | :php:meth:`skip` | - +-----------------------+---------------------------+----------------------+---------------------+ - | :php:meth:`some` | :php:meth:`sortBy` | :php:meth:`stopWhen` | :php:meth:`sumOf` | - +-----------------------+---------------------------+----------------------+---------------------+ - | :php:meth:`take` | :php:meth:`through` | :php:meth:`unfold` | :php:meth:`zip` | - +-----------------------+---------------------------+----------------------+---------------------+ - | :php:meth:`transpose` | :php:meth:`chunkWithKeys` | | | - +-----------------------+---------------------------+----------------------+---------------------+ + +---------------------------------------------------+----------------------+------------------------+ + | :php:meth:`append` | :php:meth:`avg` | :php:meth:`buffered` | :php:meth:`combine` | + +---------------------------+-----------------------+----------------------+------------------------+ + | :php:meth:`compile` | :php:meth:`contains` | :php:meth:`countBy` | :php:meth:`chunk` | + +---------------------------+-----------------------+----------------------+------------------------+ + | :php:meth:`chunkWithKeys` | :php:meth:`each` | :php:meth:`every` | :php:meth:`extract` | + +---------------------------+-----------------------+----------------------+------------------------+ + | :php:meth:`filter` | :php:meth:`first` | :php:meth:`groupBy` | :php:meth:`indexBy` | + +---------------------------+-----------------------+----------------------+------------------------+ + | :php:meth:`insert` | :php:meth:`isEmpty` | :php:meth:`last` | :php:meth:`listNested` | + +---------------------------+-----------------------+----------------------+------------------------+ + | :php:meth:`map` | :php:meth:`match` | :php:meth:`max` | :php:meth:`median` | + +---------------------------+-----------------------+----------------------+------------------------+ + | :php:meth:`min` | :php:meth:`nest` | :php:meth:`reduce` | :php:meth:`reject` | + +---------------------------+-----------------------+----------------------+------------------------+ + | :php:meth:`sample` | :php:meth:`shuffle` | :php:meth:`skip` | :php:meth:`some` | + +---------------------------+-----------------------+----------------------+------------------------+ + | :php:meth:`sortBy` | :php:meth:`stopWhen` | :php:meth:`sumOf` | :php:meth:`take` | + +---------------------------+-----------------------+----------------------+------------------------+ + | :php:meth:`through` | :php:meth:`transpose` | :php:meth:`unfold` | :php:meth:`zip` | + +---------------------------+-----------------------+----------------------+------------------------+ Iterating ========= @@ -470,6 +470,40 @@ elements:: $sumOfDadAges = $collection->sumOf('dad.age'); +.. php:method:: avg($matcher = null) + +Calculate the average value of the elements in the collection. Optionally +provide a matcher path, or function to extract values to generate the average +for:: + + $items = [ + ['invoice' => ['total' => 100]], + ['invoice' => ['total' => 200]], + ]; + + // Average: 150 + $average = (new Collection($items))->avg('invoice.total'); + +.. versionadded:: 3.5.0 + +.. php:method:: median($matcher = null) + +Calculate the median value of a set of elements. Optionally provide a matcher +path, or function to extract values to generate the median for:: + + $items = [ + ['invoice' => ['total' => 400]], + ['invoice' => ['total' => 500]], + ['invoice' => ['total' => 100]], + ['invoice' => ['total' => 333]], + ['invoice' => ['total' => 200]], + ]; + + // Median: 333 + $median = (new Collection($items))->median('invoice.total'); + +.. versionadded:: 3.5.0 + Grouping and Counting ---------------------