AthenaeumAthenaeum
Packages
  • next
  • current
  • v9.x
  • v8.x
  • v7.x
  • v6.x
  • v5.x
  • v4.x
  • v3.x
  • v2.x
  • v1.x
Changelog
GitHub
Packages
  • next
  • current
  • v9.x
  • v8.x
  • v7.x
  • v6.x
  • v5.x
  • v4.x
  • v3.x
  • v2.x
  • v1.x
Changelog
GitHub
  • Version 6.x

    • Release Notes
    • Upgrade Guide
    • New to this...
    • Contribution Guide
    • Security Policy
    • Code of Conduct
    • Origin
  • ACL

    • Introduction
    • How to install
    • Setup
    • Permissions
    • Roles
    • Users
    • Cached Permissions
  • Audit

    • Audit
    • How to install
    • Setup
    • Recording
    • Events
  • Circuits

    • Circuits
    • How to install
    • Setup
    • Usage
    • Events
  • Collections

    • Collections
    • How to install
    • Summation

      • Summation Collection
      • Items Processor
  • Config

    • Configuration Loader
    • How to install
    • Setup
    • Load Configuration Files
    • Custom File Parsers
  • Console

    • Command and Schedule Registration
    • How to install
    • Setup
    • Commands
    • Schedules
  • Container

    • IoC Service Container
    • How to install
    • Container
    • List Resolver
  • Core

    • Athenaeum Core Application
    • How to install
    • Setup
    • Usage

      • Configuration
      • Service Providers
      • Service Container
      • Events
      • Caching
      • Logging
      • Console
      • Task Scheduling
      • Exception Handling
      • Extending Core Application
      • Testing
  • Database

    • Introduction
    • How to install
    • Models

      • Instantiatable
      • Sluggable
    • Query

      • Criteria (Query Filter)
  • Dto

    • Data Transfer Object (DTO)
    • How to install
    • Create Interface
    • Implement DTO
    • How to use
    • Populate
    • Export
    • Json
    • Serialization
    • Nested DTOs
    • Array DTO
  • ETags

    • ETags
    • How to install
    • Setup
    • Usage
    • Generators

      • Default Generator
      • Custom Generator
    • Eloquent Models
    • Macros
  • Events

    • Register Listeners and Subscribers
    • How to install
    • Setup
    • Listeners
    • Subscribers
  • Filters

    • Search Filter Utilities
    • Prerequisites
    • How to install
    • Setup
    • Processor
    • Filters Builder
    • Predefined Resources

      • Search Processor
      • Sorting Processor
      • Constraints Processor
      • Matching Processor
    • Tip: Create a base builder
  • Flysystem

    • Introduction
    • Database Adapter

      • Introduction
      • How to install
      • Setup
      • Data Deduplication
      • MIME-Type Detection
  • Http

    • Api

      • Http API
      • How to install
      • Setup
      • Resources

        • Introduction
        • Timestamps
        • Self-Link
        • Relations
        • Registrar
      • Middleware

        • Introduction
        • Request Must Be Json
        • Capture Fields To Select
    • Clients

      • Http Clients
      • How to install
      • Setup
      • Basic Usage
      • Available Methods

        • Fluent Api
        • Protocol Version
        • Base Uri
        • Http Method and Uri
        • Headers
        • Accept & Content-Type
        • Authentication
        • Http Query
        • Payload Format
        • Payload
        • Attachments
        • Cookies
        • Response Expectations
        • Middleware
        • Conditions
        • Criteria
        • Redirects
        • Timeout
        • Debugging
        • Logging
        • Driver Options
        • Driver
      • Http Query Builder

        • Introduction
        • Select
        • Where
        • Dates
        • Include
        • Pagination
        • Sorting
        • Raw Expressions
        • Custom Grammar
    • Cookies

      • Http Cookies
      • How to install
      • Usage
    • Messages

      • Http Messages
      • How to install
      • Serializers
  • Maintenance

    • Modes

      • Maintenance Modes
      • How to install
      • Setup
      • Basic Usage
      • Available Drivers
  • Mime Types

    • MIME-Types
    • How to install
    • Setup
    • Usage
    • Drivers

      • Available Drivers
      • File Info
  • Properties

    • Properties Overload
    • How to install
    • Usage
    • Naming Convention
    • Properties Visibility
  • Redmine

    • Redmine Api Client
    • How to install
    • Setup
    • General Usage

      • Supported Operations
      • Fetch list of resources
      • Find
      • Fetch
      • Create new record
      • Update existing record
      • Delete existing record
      • Relations
    • Available Resources

      • Predefined Resources
      • Attachments
      • Enumerations
      • Issue Relations
      • Users
      • User Groups
      • Roles
      • Project Memberships
      • Versions (Milestones)
      • Issue Categories
      • Trackers
  • Service

    • Service Registrar
    • How to install
    • How to use
  • Streams

    • Streams
    • How to install
    • Setup
    • How to use

      • Introduction
      • Open and Close
      • Raw Resource
      • Seeking
      • Reading
      • Writing
      • Size
      • Truncate
      • Flush
      • Hash
      • MIME-Type
      • Output
      • Locking
      • Transactions
      • Meta
      • Misc
  • Support

    • Introduction
    • How to install
    • Laravel Aware-of Helpers

      • How to use
      • Enforce Via Interface
      • Custom Default
      • Pros and Cons
      • Available Helpers
    • Aware-of Properties

      • Generator
      • Available Aware-of Helpers
    • Live Templates
  • Testing

    • Introduction
    • How to install
    • Test Cases
    • Testing Aware-of Helpers
  • Utils

    • Introduction
    • How to install
    • Array
    • Duration
    • Json
    • Math
    • Memory
    • Method Helper
    • Invoker
    • Populatable
    • String
    • Version
  • Validation

    • Introduction
    • How to install
    • Setup
    • Rules

      • Alpha-Dash-Dot
      • Semantic Version
You are viewing documentation for an outdated version. It is no longer supported!

Summation Collection

A collection of numeric values, which are typically a product of processing multiple items, e.g. database records. Most commonly, you would use this component in combination with an Items Processor.

Within this context, the term "summation" is referred to as a "[...] cumulative action or effect [...]" (Merriam Webster).

  • Basic Example
  • Setting initial values
  • Processing values
    • Increase
    • Decrease
    • Multiply
    • Divide
    • Apply Callback
  • Dot notation
  • Determine keys and values
    • Has key
    • Has value
  • Removing keys
  • Exporting
    • To Array
    • To Json
  • Debugging

Basic Example

use Aedart\Collections\Summation;

$collection = Summation::make([
    'points' => 0
]);

$result = $collection
    ->increase('points', 5)
    ->decrease('points', 1)
    ->get('points');

echo $result; // 4

Setting initial values

To work with a Summation collection, you must first set the initial value(s), which you later wish to process in some way. You can either state them when you create a new instance (e.g. via the make() method), or by using the set() method.

$collection = Summation::make([
    'strength' => 15,
    'agility' => 22,
    'intelligence' => 19,
]);

// Or...

$collection = Summation::make()
    ->set('strength', 15)
    ->set('agility', 22)
    ->set('intelligence', 19);

Processing values

Increase

Use the increase() or add() method to increase a key's value with a given amount.

$result = Summation::make()
    ->set('points', 0)
    
    ->increase('points', 5)
    ->add('points', 1)
    
    ->get('points');

echo $result; // 6

Decrease

To decrease a key's value, you can use the decrease() or subtract() method.

$result = Summation::make()
    ->set('points', 25)
    
    ->decrease('points', 5)
    ->subtract('points', 1)
    
    ->get('points');

echo $result; // 19

Multiply

You can multiple an existing key's value using the multiply() method.

$result = Summation::make()
    ->set('points', 5)
    
    ->multiply('points', 5)
    
    ->get('points');

echo $result; // 25

Divide

To divide and existing key's value, use the divide() method.

$result = Summation::make()
    ->set('points', 25)
    
    ->divide('points', 3)
    
    ->get('points');

echo $result; // 8.3333

Apply Callback

All processing methods accept a callable as a value, in which you can process a given key's value, however you want. The result of the given callback is the used as the key's new value.

$result = Summation::make()
    ->set('points', 25)
    ->set('amount', 8)
    
    ->set('points', function($value, Summation $summation) {
        return $value / $summation->get('amount');
    })
    
    ->get('points');

echo $result; // 3.125

Dot notation

Laravel's "dot notation" is also supported by the Summation collection component.

$result = Summation::make()
    ->set('player.strength', 16)
    
    ->increase('player.strength', 2)
    
    ->get('player.strength');

echo $result; // 18

Determine keys and values

Has key

The has() method can be used to determine whether a key exists in the collection or not.

$collection->has('score'); // true or false

Note

The has() will return true if a key exists, even if the key's value is zero (empty).

Has value

To determine if a key exists and has a value (nonempty value), use the hasValue() method.

$collection->hasValue('score'); // true if key exists and has a nonempty value

Removing keys

A key and it's associated value can be deleted by using the remove() method.

$collection->remove('player.intelligence');

Exporting

To Array

The toArray() method will export all keys and values to a native php array.

$arr = $collection->toArray();

To Json

If you wish to retrieve a Json representation of the collection, you can either use the toJson() method or invoke json_encode() directly on the collection.

$result = $collection->toJson();

// Or...

$result = json_encode($collection);

Debugging

Prerequisite

To use the debugging methods, you must have symfony/var-dumper installed.

The dd() method will dump the collection's keys and values and stop further script execution.

$collection->dd(); // Dumps keys and values and STOPS script execution!

The dump() method, on the other hand, will only dump the collection's keys and values.

$result = $collection
    ->set('points', 5)
    ->increase('points', 1)
    ->dump() // Dumps keys and values
    ->decrease('points', 2)
    ->get('points');

echo $result; // 4
Edit page
Last Updated: 16/02/2023, 09:10
Contributors: Alin Eugen Deac, alin
Next
Items Processor