Traits
1 month ago
Either.php
1 month ago
Fns.php
1 month ago
Invoker.php
1 month ago
Logic.php
1 month ago
Obj.php
1 month ago
Relation.php
1 month ago
Str.php
1 month ago
Undefined.php
1 month ago
functions.php
1 month ago
Obj.php
83 lines
| 1 | <?php |
| 2 | |
| 3 | namespace OTGS\Installer\FP; |
| 4 | |
| 5 | use OTGS\Installer\Collection; |
| 6 | use OTGS\Installer\NullCollection; |
| 7 | use OTGS\Installer\Collect\Support\Macroable; |
| 8 | |
| 9 | class Obj { |
| 10 | use Macroable; |
| 11 | |
| 12 | public static function init() { |
| 13 | self::macro( 'prop', curryN( 2, function ( $key, $item ) { |
| 14 | return self::propOr( null, $key, $item ); |
| 15 | } ) ); |
| 16 | |
| 17 | self::macro( 'propOr', curryN( 3, function ( $default, $key, $item ) { |
| 18 | if ( $item instanceof Collection && $item->get( $key ) instanceof NullCollection ) { |
| 19 | return $default; |
| 20 | } |
| 21 | if ( is_array( $item ) ) { |
| 22 | return array_key_exists( $key, $item ) ? $item[ $key ] : $default; |
| 23 | } |
| 24 | if ( is_object( $item ) ) { |
| 25 | if ( property_exists( $item, $key ) || isset( $item->$key ) ) { |
| 26 | return $item->$key; |
| 27 | } elseif ( is_numeric( $key ) ) { |
| 28 | return self::propOr( $default, $key, (array) $item ); |
| 29 | } else { |
| 30 | return $default; |
| 31 | } |
| 32 | } |
| 33 | if ( is_null( $item ) ) { |
| 34 | return null; |
| 35 | } |
| 36 | throw( new \InvalidArgumentException( 'item should be a Collection or an array or an object' ) ); |
| 37 | } ) ); |
| 38 | |
| 39 | self::macro( 'path', curryN( 2, function ( $path, $item ) { |
| 40 | return array_reduce( $path, flip( self::prop() ), $item ); |
| 41 | } ) ); |
| 42 | |
| 43 | self::macro( 'pathOr', curryN( 3, function ( $default, $path, $item ) { |
| 44 | $result = Either::of( $item ) |
| 45 | ->tryCatch( Obj::path( $path ) ) |
| 46 | ->getOrElse( null ); |
| 47 | |
| 48 | return is_null( $result ) ? $default : $result; |
| 49 | } ) ); |
| 50 | |
| 51 | self::macro( 'hasPath', curryN( 2, function ( $path, $item ) { |
| 52 | $undefinedValue = new Undefined(); |
| 53 | $currentElement = $item; |
| 54 | |
| 55 | foreach ( $path as $pathProp ) { |
| 56 | $currentElement = Either::of( $currentElement ) |
| 57 | ->tryCatch( self::propOr( $undefinedValue, $pathProp ) ) |
| 58 | ->getOrElse( $undefinedValue ); |
| 59 | |
| 60 | if ( $undefinedValue === $currentElement ) { |
| 61 | return false; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return true; |
| 66 | } ) ); |
| 67 | |
| 68 | self::macro( 'has', curryN( 2, function ( $prop, $item ) { |
| 69 | if ( $item instanceof Collection ) { |
| 70 | return $item->has( $prop ); |
| 71 | } |
| 72 | if ( is_array( $item ) ) { |
| 73 | return isset( $item[ $prop ] ); |
| 74 | } |
| 75 | if ( is_object( $item ) ) { |
| 76 | return property_exists( $item, $prop ); |
| 77 | } |
| 78 | throw( new \InvalidArgumentException( 'item should be a Collection or an array or an object' ) ); |
| 79 | } ) ); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | Obj::init(); |