CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Team
    • Issues (Github)
    • YouTube Channel
    • Get Involved
    • Bakery
    • Featured Resources
    • Newsletter
    • Certification
    • My CakePHP
    • CakeFest
    • Facebook
    • Twitter
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 3.8 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.8
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Cake
    • Auth
      • Storage
    • Cache
      • Engine
    • Collection
      • Iterator
    • Command
    • Console
      • Exception
    • Controller
      • Component
      • Exception
    • Core
      • Configure
        • Engine
      • Exception
      • Retry
    • Database
      • Driver
      • Exception
      • Expression
      • Schema
      • Statement
      • Type
    • Datasource
      • Exception
    • Error
      • Middleware
    • Event
      • Decorator
    • Filesystem
    • Form
    • Http
      • Client
        • Adapter
        • Auth
      • Cookie
      • Exception
      • Middleware
      • Session
    • I18n
      • Formatter
      • Middleware
      • Parser
    • Log
      • Engine
    • Mailer
      • Exception
      • Transport
    • Network
      • Exception
    • ORM
      • Association
      • Behavior
        • Translate
      • Exception
      • Locator
      • Rule
    • Routing
      • Exception
      • Filter
      • Middleware
      • Route
    • Shell
      • Helper
      • Task
    • TestSuite
      • Fixture
      • Stub
    • Utility
      • Exception
    • Validation
    • View
      • Exception
      • Form
      • Helper
      • Widget
  • None

Classes

  • BufferedIterator
  • ExtractIterator
  • FilterIterator
  • InsertIterator
  • MapReduce
  • NestIterator
  • NoChildrenIterator
  • ReplaceIterator
  • SortIterator
  • TreeIterator
  • TreePrinter
  • ZipIterator
 1: <?php
 2: /**
 3:  * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
 4:  * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 5:  *
 6:  * Licensed under The MIT License
 7:  * For full copyright and license information, please see the LICENSE.txt
 8:  * Redistributions of files must retain the above copyright notice.
 9:  *
10:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
11:  * @link          https://cakephp.org CakePHP(tm) Project
12:  * @since         3.0.0
13:  * @license       https://opensource.org/licenses/mit-license.php MIT License
14:  */
15: namespace Cake\Collection\Iterator;
16: 
17: use Cake\Collection\Collection;
18: use DateTimeInterface;
19: 
20: /**
21:  * An iterator that will return the passed items in order. The order is given by
22:  * the value returned in a callback function that maps each of the elements.
23:  *
24:  * ### Example:
25:  *
26:  * ```
27:  * $items = [$user1, $user2, $user3];
28:  * $sorted = new SortIterator($items, function ($user) {
29:  *  return $user->age;
30:  * });
31:  *
32:  * // output all user name order by their age in descending order
33:  * foreach ($sorted as $user) {
34:  *  echo $user->name;
35:  * }
36:  * ```
37:  *
38:  * This iterator does not preserve the keys passed in the original elements.
39:  */
40: class SortIterator extends Collection
41: {
42:     /**
43:      * Wraps this iterator around the passed items so when iterated they are returned
44:      * in order.
45:      *
46:      * The callback will receive as first argument each of the elements in $items,
47:      * the value returned in the callback will be used as the value for sorting such
48:      * element. Please note that the callback function could be called more than once
49:      * per element.
50:      *
51:      * @param array|\Traversable $items The values to sort
52:      * @param callable|string $callback A function used to return the actual value to
53:      * be compared. It can also be a string representing the path to use to fetch a
54:      * column or property in each element
55:      * @param int $dir either SORT_DESC or SORT_ASC
56:      * @param int $type the type of comparison to perform, either SORT_STRING
57:      * SORT_NUMERIC or SORT_NATURAL
58:      */
59:     public function __construct($items, $callback, $dir = \SORT_DESC, $type = \SORT_NUMERIC)
60:     {
61:         if (!is_array($items)) {
62:             $items = iterator_to_array((new Collection($items))->unwrap(), false);
63:         }
64: 
65:         $callback = $this->_propertyExtractor($callback);
66:         $results = [];
67:         foreach ($items as $key => $val) {
68:             $val = $callback($val);
69:             if ($val instanceof DateTimeInterface && $type === \SORT_NUMERIC) {
70:                 $val = $val->format('U');
71:             }
72:             $results[$key] = $val;
73:         }
74: 
75:         $dir === SORT_DESC ? arsort($results, $type) : asort($results, $type);
76: 
77:         foreach (array_keys($results) as $key) {
78:             $results[$key] = $items[$key];
79:         }
80:         parent::__construct($results);
81:     }
82: 
83:     /**
84:      * {@inheritDoc}
85:      *
86:      * @return \Iterator
87:      */
88:     public function unwrap()
89:     {
90:         return $this->getInnerIterator();
91:     }
92: }
93: 
Follow @CakePHP
#IRC
OpenHub
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Logos & Trademarks
  • Community
  • Team
  • Issues (Github)
  • YouTube Channel
  • Get Involved
  • Bakery
  • Featured Resources
  • Newsletter
  • Certification
  • My CakePHP
  • CakeFest
  • Facebook
  • Twitter
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs