micro = $micro; } public static function zero(): self { return new self( 0 ); } public static function fromMicro( int $micro ): self { return new self( $micro ); } public static function fromCents( int $cents ): self { return new self( $cents * 10000 ); } /** * Lit un montant décimal tel que WooCommerce le stocke (« 12.5 », « -3.333333 », 7, 1.2). * * @param string|int|float $value Montant en euros. * @throws \InvalidArgumentException Si la valeur n'est pas un nombre. */ public static function fromDecimal( $value ): self { if ( is_int( $value ) ) { return new self( $value * self::SCALE ); } if ( is_float( $value ) ) { if ( ! is_finite( $value ) ) { throw new \InvalidArgumentException( 'Montant non fini.' ); } $value = number_format( $value, 8, '.', '' ); } $text = trim( (string) $value ); if ( '' === $text ) { return self::zero(); } if ( ! preg_match( '/^([+-])?(\d*)(?:\.(\d*))?$/', $text, $m ) || ( '' === $m[2] && ( ! isset( $m[3] ) || '' === $m[3] ) ) ) { throw new \InvalidArgumentException( 'Montant invalide : ' . $text ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- message technique, jamais affiché. } $sign = ( isset( $m[1] ) && '-' === $m[1] ) ? -1 : 1; $integer = '' === $m[2] ? '0' : $m[2]; $decimals = isset( $m[3] ) ? $m[3] : ''; // Arrondi au micro-euro le plus proche (la 7e décimale décide). $six = str_pad( substr( $decimals, 0, 6 ), 6, '0' ); $micro = (int) $integer * self::SCALE + (int) $six; if ( strlen( $decimals ) > 6 && (int) $decimals[6] >= 5 ) { ++$micro; } return new self( $sign * $micro ); } public function micro(): int { return $this->micro; } /** * Arrondi au centime : au plus proche, la moitié s'éloignant de zéro (0,005 → 0,01 ; -0,005 → -0,01). */ public function toCents(): int { $abs = abs( $this->micro ); $cents = intdiv( $abs + 5000, 10000 ); return $this->micro < 0 ? -$cents : $cents; } public function add( Amount $other ): self { return new self( $this->micro + $other->micro ); } public function subtract( Amount $other ): self { return new self( $this->micro - $other->micro ); } public function negate(): self { return new self( -$this->micro ); } public function isZero(): bool { return 0 === $this->micro; } public function isNegative(): bool { return $this->micro < 0; } /** * Répartit le montant selon des poids, sans perdre ni créer un seul micro-euro * (méthode du plus fort reste). Les poids négatifs sont traités comme nuls. * * @param array $weights Poids entiers (par exemple des montants en micro-euros). * @return array Mêmes clés que $weights. */ public function allocate( array $weights ): array { $keys = array_keys( $weights ); $total = 0; foreach ( $weights as $w ) { $total += max( 0, (int) $w ); } $result = array(); if ( 0 === $total ) { // Aucun poids : tout sur la première clé. foreach ( $keys as $i => $key ) { $result[ $key ] = 0 === $i ? $this : self::zero(); } return $result; } $sign = $this->micro < 0 ? -1 : 1; $abs = abs( $this->micro ); $parts = array(); $remainders = array(); $given = 0; foreach ( $keys as $key ) { // La part exacte peut être fractionnaire : on garde la partie entière, puis on // distribue les micro-euros restants aux plus grandes parties fractionnaires. // (Calcul en virgule flottante : précis au micro-euro pour des montants < 9 milliards d'euros.) $exact = $abs * ( max( 0, (int) $weights[ $key ] ) / $total ); $floor = (int) floor( $exact ); $parts[ $key ] = $floor; $remainders[ $key ] = $exact - $floor; $given += $floor; } $left = $abs - $given; arsort( $remainders ); foreach ( array_keys( $remainders ) as $key ) { if ( $left <= 0 ) { break; } ++$parts[ $key ]; --$left; } foreach ( $keys as $key ) { $result[ $key ] = new self( $sign * $parts[ $key ] ); } return $result; } }