Coder Social home page Coder Social logo

hi-folks / array Goto Github PK

View Code? Open in Web Editor NEW
11.0 11.0 9.0 346 KB

Arr class is built on top of the PHP array functions. Arr exposes methods for creating, managing, accessing to the array data structure.

Home Page: https://packagist.org/packages/hi-folks/array

License: MIT License

PHP 100.00%
array hacktoberfest php

array's People

Contributors

ashermoshav avatar dependabot[bot] avatar devatreides avatar github-actions[bot] avatar leovie avatar martijnengler avatar nuernbergera avatar roadsigns avatar roberto-butti avatar tautvydaskarvelis avatar tharun634 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

array's Issues

Improve documentation for groupBy(), transform() and OrderBy()

Improving documentation examples for Table methods:

  • groupBy(): grouping data
  • transform(): transforms a specific field with the provided function
  • orderBy(): sorting data (ascending or descending)

Where to improve:

  • README.md file
  • doc/table.md file
  • examples/cheatsheet_table.php

Add Table method: toArray()

What

Add new method to Table class, in src/Table.php file

Description

The toArray() method returns a bidimensional PHP array from Table class.

In Table class, you could have something like this:

    /**
     * @return array
     */
    public function toArray(): array
    {
        $result = [];
        foreach ($this->rows() as $key => $row ) {
            $result[$key] = $row->arr();
        }
        return $result;
    }

Checklist

  • implement method
  • write phpdoc for the function
  • write test in tests/TableTest.php
  • execute composer format
  • execute composer test-coverage
  • check if your method has full test coverage
  • add method in changelog
  • update doc/table.md adding a use case of the new method
  • update cheatsheet (examples/cheatsheet-table.php)

Implements ArrayAccess

In order to access to Arr object like an array:

  • Add implements ArrayAccess
  • implements new methods specific for ArrayAccess:
public offsetExists(mixed $offset): bool
public offsetGet(mixed $offset): mixed
public offsetSet(mixed $offset, mixed $value): void
public offsetUnset(mixed $offset): void

Parse error:

I received this error using wampserver 3.2.8 with php 7.2.1
Parse error: syntax error, unexpected '|', expecting ';' or '{' in C:\wamp\www\stats\statistics-main\src\Stat.php on line 34
just thought you should know.

introduce Rector

composer require rector/rector --dev

For starting in a easy way, we can start using these directives:

        LevelSetList::UP_TO_PHP_80,
        SetList::DEAD_CODE,

Add Arr method: includes()

What

Add new method to Arr class, in src/Arr.php file

Description

The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.

See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

Checklist

  • implement method
  • write phpdoc for the function
  • write test in tests/ArrTest.php
  • execute composer format
  • execute composer test-coverage
  • check if your method has full test coverage
  • add method in changelog
  • update doc/arr.md adding a use case of the new method
  • update cheatsheet (examples/cheatsheet.php)

Add Arr method: find()

What

Add new method to Arr class, in src/Arr.php file

Description

The find() method returns the value of the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.
See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

Checklist

  • implement method
  • write phpdoc for the function
  • write test in tests/ArrTest.php
  • execute composer format
  • execute composer test-coverage
  • check if your method has full test coverage
  • add method in changelog
  • update doc/arr.md adding a use case of the new method
  • update cheatsheet (examples/cheatsheet.php)

Add Arr method: values()

What

Add new method to Arr class, in src/Arr.php file

Description

The values() method returns a new Arr iterator object that contains the values for each index in the array.
Arr already implements Iterator, so you can call next() method directly on Arr object. values() creates a new Arr object just with values, and the keys are not preserved

See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/values

Checklist

  • implement method
  • write phpdoc for the function
  • write test in tests/ArrTest.php
  • execute composer format
  • execute composer test-coverage
  • check if your method has full test coverage
  • add method in changelog
  • update doc/arr.md adding a use case of the new method
  • update cheatsheet (examples/cheatsheet.php)

Add Arr method: from()

What

Add new method to Arr class, in src/Arr.php file

Description

The Array.from() static method creates a new, shallow-copied Array instance from an array-like or iterable object.

See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from

Checklist

  • implement method
  • write phpdoc for the function
  • write test in tests/ArrTest.php
  • execute composer format
  • execute composer test-coverage
  • check if your method has full test coverage
  • add method in changelog
  • update doc/arr.md adding a use case of the new method
  • update cheatsheet (examples/cheatsheet.php)

Add Arr method: copyWithin()

What

Add new method to Arr class, in src/Arr.php file

Description

The copyWithin() method shallow copies part of an array to another location in the same array and returns it without modifying its length

See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/copyWithin

Checklist

  • implement method
  • write phpdoc for the function
  • write test in tests/ArrTest.php
  • execute composer format
  • execute composer test-coverage
  • check if your method has full test coverage
  • add method in changelog
  • update doc/arr.md adding a use case of the new method
  • update cheatsheet (examples/cheatsheet.php)

Add Arr method: findIndex()

What

Add new method to Arr class, in src/Arr.php file

Description

The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test.
See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex

Checklist

  • implement method
  • write phpdoc for the function
  • write test in tests/ArrTest.php
  • execute composer format
  • execute composer test-coverage
  • check if your method has full test coverage
  • add method in changelog
  • update doc/arr.md adding a use case of the new method
  • update cheatsheet (examples/cheatsheet.php)

Add Arr method: entries()

What

Add new method to Arr class, in src/Arr.php file

Description

The entries() method returns a new Arr object that contains the key/value pairs for each index in the array.
See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/entries

Checklist

  • implement method
  • write phpdoc for the function
  • write test in tests/ArrTest.php
  • execute composer format
  • execute composer test-coverage
  • check if your method has full test coverage
  • add method in changelog
  • update doc/arr.md adding a use case of the new method
  • update cheatsheet (examples/cheatsheet.php)

Add Table method: except

What

Add new method to Table class, in src/Table.php file

Description

The except method excludes fields from the table. It is the inverse select().

Checklist

  • implement method
  • write phpdoc for the function
  • write test in tests/TableTest.php
  • execute composer format
  • execute composer test-coverage
  • check if your method has full test coverage
  • add method in changelog
  • update doc/table.md adding a use case of the new method
  • update cheatsheet (examples/cheatsheet-table.php)

PHP stan Level 4

Now we have phpstan executed with level 3.
The request is for raising the level to 4.
in the file phpstan.neon set level to 4, and then execute composer run phpstan:

 parameters:
     level: 4
     paths:
         - src

Executing phpstan with level 4 you will receive this warning:

  7      Property HiFolks\DataType\Table::$schema is never read, only written.
         ๐Ÿ’ก See:
         https://phpstan.org/developing-extensions/always-read-written-propert
         ies

If this warning is solved we could increase the level of phpstan to 4.

Table class: refactoring group and related methods

Consider this suggestion: https://twitter.com/marekgibney/status/1519938757893439488

$table->group('product')->transform(Operation::aggregate(['total'=>['price', 'array_sum']]));

Table::group -> Groups rows by field
Table::transform -> Transforms rows by func
Operation::aggregate -> Aggregates fields by funcs

Checklist

  • implement method
  • write phpdoc for the function
  • write test in tests/TableTest.php
  • execute composer format
  • execute composer test-coverage
  • check if your method has full test coverage
  • add method in changelog
  • update doc/table.md adding a use case of the new method
  • update cheatsheet (examples/cheatsheet-table.php)

Fix parameter type for strval function in Table.php

Executing PHP stan you can see the error:

Error: Parameter #1 $value of function strval expects bool|float|int|resource|string|null, mixed given.
 ------ --------------------------------------------------- 
  Line   Table.php                                          
 ------ --------------------------------------------------- 
  181    Parameter #1 $value of function strval expects     
         bool|float|int|resource|string|null, mixed given.  
 ------ --------------------------------------------------- 

Change the type according to the PHPstan suggestion

Add Table method: groupThenApply

What

Add new method to Table class, in src/Table.php file

Description

groupThenApply($field, $calcField, $function)
Where $field is the field to group data,
$function is the function to apply to each group (for each row) in $calcField

Example, for this data:

$dataTable = [
    ['product' => 'Desk', 'price' => 200, 'active' => true],
    ['product' => 'Chair', 'price' => 100, 'active' => true],
    ['product' => 'Door', 'price' => 300, 'active' => false],
    ['product' => 'Bookcase', 'price' => 150, 'active' => true],
    ['product' => 'Door', 'price' => 100, 'active' => true],
];

and the function sum on price, and $calcField is "total", the result will be

[
'Desk' => ['product' => 'Desk', 'total' => 200],
'Chair' => ['product' => 'Chair', 'total' => 100],
'Door' => ['product' => 'Door', 'total' => 400],
'Bookcase' => ['product' => 'Bookcase', 'total' => 150]
]

Checklist

  • implement method
  • write phpdoc for the function
  • write test in tests/TableTest.php
  • execute composer format
  • execute composer test-coverage
  • check if your method has full test coverage
  • add method in changelog
  • update doc/table.md adding a use case of the new method
  • update cheatsheet (examples/cheatsheet-table.php)

Add Arr method: toLocaleString()

What

Add new method to Arr class, in src/Arr.php file

Description

The toLocaleString() method returns a string representing the elements of the array. The elements are converted to Strings using their toLocaleString methods and these Strings are separated by a locale-specific String (such as a comma โ€œ,โ€).
In PHP as far as I know there is no toLocaleString. My suggestion is to try to convert into locale string detecting the type

  • number_format() if number
  • date_format if it is date

see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toLocaleString

Checklist

  • implement method
  • write phpdoc for the function
  • write test in tests/ArrTest.php
  • execute composer format
  • execute composer test-coverage
  • check if your method has full test coverage
  • add method in changelog
  • update doc/arr.md adding a use case of the new method
  • update cheatsheet (examples/cheatsheet.php)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.